我如何从java中的InputStream中提取某些模式,然后放入byteArray中

发布于 2024-10-19 09:26:38 字数 637 浏览 4 评论 0原文

我正在寻找java中的一种方法,从输入流中提取某些字节。 例如 我将有这个数据流发生

0x01,0x02,0x00,0x01,0x00,0x01,0x03,0x04,0x00,0x01,0x09,0x08,0x00,0x01,0x00,0x01

我的编码方案是类型数据结束 首先我会检查第一个字节, 然后我想将所有数据存储在一个字节数组中,从 0x01 直到出现 0x00,0x01,0x00,0x01,除了 0x01 之外,

所以第一部分我将放入数组中的数据

0x01,0x02,0x00,0x00 

,然后放入下一个, 以 0x03 开头,以 0x00,0x01,0x00,0x01 结束 我希望将其放置在另一个字节数组中,因为

0x03,0x04,0x00,0x01,0x09,0x08,0x00,0x00

我将如何执行此操作,我开始使用

ByteArrayOutputStream 动态添加到字节数组,而不需要知道大小, 但我不知道如何提取每个模式并删除 0x00 之后的每个 0x01, 我还从输入流中输入一个字节,一次一个字节(这是我获取字节的唯一方法)

i am looking for a method in java, to extract certain bytes from an input stream.
for example
i will have this stream of data occuring

0x01,0x02,0x00,0x01,0x00,0x01,0x03,0x04,0x00,0x01,0x09,0x08,0x00,0x01,0x00,0x01

my encoding scheme is type data ending
firstly i will check the first byte,
then i will want to store all the data in a byte array from 0x01 untill the occurance of 0x00,0x01,0x00,0x01 except for the 0x01's

so the first piece of data i would place into the array

0x01,0x02,0x00,0x00 

and then onto the next ,
this begins with a 0x03 and ends with 0x00,0x01,0x00,0x01
i would like for this to be placed in another byte array as,

0x03,0x04,0x00,0x01,0x09,0x08,0x00,0x00

how would i go about doing this, i began with using

a ByteArrayOutputStream to add dynamically to the byte array, without needing to know the size,
but im lost on the logic on how would extract out each pattern and remove each 0x01 following a 0x00,
also im rading a byte in from an input stream , one byte at a time (its the only way i can get the bytes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-10-26 09:26:38

您需要一个有限状态识别器。对于简单的语法,以下伪代码应该可以解决问题:

state = 0;
while( (byte=input.read()) != EOF)
{
    switch(state)
    {
        case 0:     // "normal" state
            if (byte == 0x00)
            {
                state = 1;
                buf.append(byte);
            }
            else
                output.write(byte)
            break;
        case 1:     // We've seen a 0x00
            if (byte == 0x00)
            {
                state = 1;
                output.write(buf);
            }
            else if (byte == 0x01)
            {
                state = 2;
                buf.append(byte);
            }
            else
            {
                output.write(buf);
                buf.clear();
                state = 0;
            }
            break;
        case 2:     // We've seen 0x00,0x01
            if (byte == 0x00)
            {
                state = 3;
                buf.append(byte);
            }
            else if (byte == 0x01)
            {
                output.write(0x00);
                buf.clear();
                state = 0;
            }
            else
            {
                output.write(buf);
                buf.clear();
                state = 0;
            }
            break;
        case 3:     // We've seen 0x00,0x01,0x00
            if (byte == 0x00)
            {
                state = 1;
                output.write(buf);
                buf.clear();
                buf.append(byte);
            }
            else if (byte == 0x01)
            {
                // The last four input bytes were 0x00,0x01,0x00,0x01
                state = 0;
                output.write(0x00,0x00);
                buf.clear
            }
            else
            {
                output.write(buf);
                buf.clear();
                state = 0;
            }
            break;
    }
}
if (!buf.empty()) output.write(buf);

这通过一次读取一个字节来工作。

如果它检测到 0x00,我们需要开始寻找分隔符模式,但保存字节,以防稍后我们发现这是一个误报。 “state”变量跟踪我们迄今为止所看到的内容。在每个点,如果输入与下一个预期的分隔符字节匹配,我们将保存它,改变状态并继续。如果在任何时候我们没有得到下一个预期的分隔符字节,我们只需写出所有保存的数据,清除缓冲区并返回到“正常”状态。但是,如果我们最终看到整个分隔符字符串,我们会写出 0x00,0x00 并丢弃保存的字节(这将是 0x00,0x01,0x00,0x01)。

编辑:修改代码以处理来自OP的附加条件和来自@Shaded的注释

You need a finite-state recognizer. For your simple syntax the following pseudocode should do the trick:

state = 0;
while( (byte=input.read()) != EOF)
{
    switch(state)
    {
        case 0:     // "normal" state
            if (byte == 0x00)
            {
                state = 1;
                buf.append(byte);
            }
            else
                output.write(byte)
            break;
        case 1:     // We've seen a 0x00
            if (byte == 0x00)
            {
                state = 1;
                output.write(buf);
            }
            else if (byte == 0x01)
            {
                state = 2;
                buf.append(byte);
            }
            else
            {
                output.write(buf);
                buf.clear();
                state = 0;
            }
            break;
        case 2:     // We've seen 0x00,0x01
            if (byte == 0x00)
            {
                state = 3;
                buf.append(byte);
            }
            else if (byte == 0x01)
            {
                output.write(0x00);
                buf.clear();
                state = 0;
            }
            else
            {
                output.write(buf);
                buf.clear();
                state = 0;
            }
            break;
        case 3:     // We've seen 0x00,0x01,0x00
            if (byte == 0x00)
            {
                state = 1;
                output.write(buf);
                buf.clear();
                buf.append(byte);
            }
            else if (byte == 0x01)
            {
                // The last four input bytes were 0x00,0x01,0x00,0x01
                state = 0;
                output.write(0x00,0x00);
                buf.clear
            }
            else
            {
                output.write(buf);
                buf.clear();
                state = 0;
            }
            break;
    }
}
if (!buf.empty()) output.write(buf);

This works by reading bytes one at a time.

If it detects a 0x00, we need to start looking for the delimiter pattern but save the bytes in case later we find it was a false alarm. The "state" variable keeps track of what we've seen so far. At each point if the input matches the next expected delimiter byte we save it, bump the state and keep going. If at any point we don't get the next expected delimiter byte we just write out all the saved data, clear the buffer and go back to "normal" state. However, if we eventually see the entire delimiter string, we write out 0x00,0x00 and discard the saved bytes (which would be 0x00,0x01,0x00,0x01).

EDIT: Code modified to handle additional condition from OP and comment from @Shaded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文