1

I am reading a data stream from a tcp socket. All of this data is sent into a byte array :

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();

As the stream is being read, I would like to detect a specific 4 bytes patern that repeats itself randomly.

I tried using a for statement to go through all the data once it's been saved but this solution is highly inefficient.

Is there any way of doing this in real time ?

1 Answers1

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;
    }
}

From here: Searching for a sequence of Bytes in a Binary File with Java

Community
  • 1
  • 1
m.aibin
  • 3,528
  • 4
  • 28
  • 47