Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a sequence of Bytes in a Binary File with Java

I have a sequence of bytes that I have to search for in a set of Binary files using Java.

Example: I'm searching for the byte sequence DEADBEEF (in hex) in a Binary file. How would I go about doing this in Java? Is there a built-in method, like String.contains() for Binary files?

like image 343
Bassam Avatar asked Oct 02 '09 04:10

Bassam


People also ask

Which Java method is used to read bytes from a standard file?

readAllBytes(Path path): reads all bytes from a file and returns an array of bytes.

How do you read and write binary files in Java explain with a sample Java program?

Reading and Writing Binary Files Using BufferedInputStream and BufferedOutputStream: The following examples use the BufferedInputStream and BufferedOutputStream classes to perform low level binary I/O: Using BufferedInputStream and BufferedOutputStream is as same as FileInputStream and FileOutputStream.

What is binary stream in Java?

Binary Stream is led by two classes: InputStream and OutputStream. Following two classes is a variety of affiliated classes. As for the balance of power, the relationship of binary stream is more diverse and sophisticated than that of character stream.


2 Answers

No, there is no built-in method to do that. But, directly copied from HERE (with two fixes applied to the original code):

/**  * Knuth-Morris-Pratt Algorithm for Pattern Matching  */ class KMPMatch {     /**      * Finds the first occurrence of the pattern in the text.      */     public static int indexOf(byte[] data, byte[] pattern) {         if (data.length == 0) return -1;          int[] failure = computeFailure(pattern);             int j = 0;          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 static 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;     } } 
like image 70
janko Avatar answered Oct 03 '22 07:10

janko


private int bytesIndexOf(byte[] source, byte[] search, int fromIndex) {     boolean find = false;     int i;     for (i = fromIndex; i < (source.length - search.length); i++) {         if (source[i] == search[0]) {             find = true;             for (int j = 0; j < search.length; j++) {                 if (source[i + j] != search[j]) {                     find = false;                 }             }         }         if (find) {             break;         }     }     if (!find) {         return -1;     }     return i; } 
like image 31
joseluisbz Avatar answered Oct 03 '22 08:10

joseluisbz