捕获流中特定字节的最有效方法是什么?

时间:2016-01-12 16:38:39

标签: java android stream byte

我正在从tcp套接字读取数据流。所有这些数据都被发送到一个字节数组:

DataInputStream in = new DataInputStream(mysource.getInputStream());
FileOutputStream output = new FileOutputStream(path);
int len;
byte buffer[] = new byte [8192];

while(len = in.read(buffer)) !=-1){
    output.write(buffer);
}

output.close();

当正在读取流时,我想检测一个随机重复的特定4字节patern。

我尝试使用for语句在保存后查看所有数据,但此解决方案效率极低。

有没有办法实时做到这一点?

1 个答案:

答案 0 :(得分:0)

/**
 * Knuth-Morris-Pratt Algorithm for Pattern Matching
 */
class KMPMatch {
    /**
     * Finds the first occurrence of the pattern in the text.
     */
    public int indexOf(byte[] data, byte[] pattern) {
        int[] failure = computeFailure(pattern);

        int j = 0;
        if (data.length == 0) return -1;

        for (int i = 0; i < data.length; i++) {
            while (j > 0 && pattern[j] != data[i]) {
                j = failure[j - 1];
            }
            if (pattern[j] == data[i]) { j++; }
            if (j == pattern.length) {
                return i - pattern.length + 1;
            }
        }
        return -1;
    }

    /**
     * Computes the failure function using a boot-strapping process,
     * where the pattern is matched against itself.
     */
    private int[] computeFailure(byte[] pattern) {
        int[] failure = new int[pattern.length];

        int j = 0;
        for (int i = 1; i < pattern.length; i++) {
            while (j > 0 && pattern[j] != pattern[i]) {
                j = failure[j - 1];
            }
            if (pattern[j] == pattern[i]) {
                j++;
            }
            failure[i] = j;
        }

        return failure;
    }
}

从这里: Searching for a sequence of Bytes in a Binary File with Java