Find indexOf a byte array within another byte array Find indexOf a byte array within another byte array arrays arrays

Find indexOf a byte array within another byte array


The simpelst way would be to compare each element:

public int indexOf(byte[] outerArray, byte[] smallerArray) {    for(int i = 0; i < outerArray.length - smallerArray.length+1; ++i) {        boolean found = true;        for(int j = 0; j < smallerArray.length; ++j) {           if (outerArray[i+j] != smallerArray[j]) {               found = false;               break;           }        }        if (found) return i;     }   return -1;  }  

Some tests:

@Testpublic void testIndexOf() {  byte[] outer = {1, 2, 3, 4};  assertEquals(0, indexOf(outer, new byte[]{1, 2}));  assertEquals(1, indexOf(outer, new byte[]{2, 3}));  assertEquals(2, indexOf(outer, new byte[]{3, 4}));  assertEquals(-1, indexOf(outer, new byte[]{4, 4}));  assertEquals(-1, indexOf(outer, new byte[]{4, 5}));  assertEquals(-1, indexOf(outer, new byte[]{4, 5, 6, 7, 8}));}

As you updated your question: Java Strings are UTF-16 Strings, they do not care about the extended ASCII set, so you could use string.indexOf()


Google's Guava provides a Bytes.indexOf(byte[] array, byte[] target).


Is this what you are looking for?

public class KPM {    /**     * Search the data byte array for the first occurrence of the byte array pattern within given boundaries.     * @param data     * @param start First index in data     * @param stop Last index in data so that stop-start = length     * @param pattern What is being searched. '*' can be used as wildcard for "ANY character"     * @return     */    public static int indexOf( byte[] data, int start, int stop, byte[] pattern) {        if( data == null || pattern == null) return -1;        int[] failure = computeFailure(pattern);        int j = 0;        for( int i = start; i < stop; i++) {            while (j > 0 && ( pattern[j] != '*' && pattern[j] != data[i])) {                j = failure[j - 1];            }            if (pattern[j] == '*' || 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;    }}