ByteArrayUtil { public static int find(byte[] a, int fromIndex, int toIndex, byte key) { int result = -1; if (fromIndex < 0) { fromIndex = 0; } toIndex = Math.min(toIndex, a.length); for (int i = fromIndex; fromIndex < toIndex && result == -1 && i < toIndex; i++) { if (a[i] == key) { result = i; } } return result; }  static void put(byte[] source, byte[] target, int offset); static byte[] get(byte[] array, int offset); static byte[] get(byte[] array, int offset, int length); static void putInt(int value, byte[] array, int offset); static int getInt(byte[] array, int offset); static void putLong(long value, byte[] array, int offset); static long getLong(byte[] array, int offset); static int find(byte[] a, int fromIndex, int toIndex, byte key); static int find(byte[] a, int fromIndex, int toIndex, byte[] key); static boolean matchesPattern(byte[] value, byte[] mask, byte[] pattern); static boolean regionMatches(byte[] subValue, byte[] superValue, int offset); static int compareRegion(byte[] array1, int startIdx1, byte[] array2, int startIdx2, int length); static BitSet toBitSet(byte[] array); static byte[] toByteArray(BitSet bitSet); static String toHexString(byte[] array);  }
@Test public void testFind() { assertEquals(-1, ByteArrayUtil.find(new byte[] {}, 0, 1, (byte) 0)); assertEquals(-1, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 20, (byte) 7)); assertEquals(-1, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, -5, 20, (byte) 7)); assertEquals(3, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 7, (byte) 3)); assertEquals(3, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, -10, 20, (byte) 3)); }
@Test public void testFindArray() { assertEquals(0, ByteArrayUtil.find(new byte[] {}, 0, 1, new byte[] {})); assertEquals(2, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 7, new byte[] { 2, 3 })); assertEquals(2, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 20, new byte[] { 2, 3 })); assertEquals(-1, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 7, new byte[] { 2, 4 })); assertEquals(-1, ByteArrayUtil.find(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 20, new byte[] { 7, 8 })); }