UTF8ByteArrayUtils { public static int findBytes(byte [] utf, int start, int end, byte[] b) { int matchEnd = end - b.length; for(int i=start; i<=matchEnd; i++) { boolean matched = true; for(int j=0; j<b.length; j++) { if (utf[i+j] != b[j]) { matched = false; break; } } if (matched) { return i; } } return -1; }  static int findByte(byte [] utf, int start, int end, byte b); static int findBytes(byte [] utf, int start, int end, byte[] b); static int findNthByte(byte [] utf, int start, int length, byte b, int n); static int findNthByte(byte [] utf, byte b, int n);  }
@Test public void testFindBytes() { byte[] data = "Hello, world!".getBytes(); assertEquals("Did not find first occurrence of pattern 'ello'", 1, UTF8ByteArrayUtils.findBytes(data, 0, data.length, "ello".getBytes())); assertEquals( "Substring starting at position 2 does not contain pattern 'ello'", -1, UTF8ByteArrayUtils.findBytes(data, 2, data.length, "ello".getBytes())); }