### Question:
KMPMatch { public static int indexOf(InputStream stream, byte[] pattern) throws IOException { if (stream.available() < 1) { return -1; } stream.mark(Integer.MAX_VALUE); try { int[] failure = computeFailure(pattern); int i = 0; int j = 0; int last = 0; byte data; while (last != -1) { last = stream.read(); data = (byte) last; while (j > 0 && pattern[j] != data) { j = failure[j - 1]; } if (pattern[j] == data) { j++; } if (j == pattern.length) { return i - pattern.length + 1; } i++; } } finally { stream.reset(); } return -1; }  static int indexOf(InputStream stream, byte[] pattern); static int indexOf(byte[] data, byte[] pattern); static int indexOf(byte[] data, byte[] pattern, int off);  }### Answer:
@Test public void testIndexOfByteArrayByteArray() throws Exception { int actual = KMPMatch.indexOf(data, pattern); assertEquals(expected, actual); }
@Test public void testIndexOfByteArrayByteArray2() throws Exception { int actual = KMPMatch.indexOf(data, pattern, 2); assertEquals(expected, actual); }
@Test public void testIndexOfInputStreamByteArray() throws Exception { ByteSource bs = ByteSource.wrap(data); InputStream is = bs.openBufferedStream(); int actual = KMPMatch.indexOf(is, pattern); assertEquals(expected, actual); }
@Test public void testIndexOfInputStreamByteArray2() throws Exception { byte[] data2 = { 0,1,2,3,0,1,2,3,4,0,1,2,3,4,5,0,1,2,3,4,5,6,0,1,2,3,4,5,6,7,0,1,2 }; byte[] pattern2 = { 4,5,6,7 }; ByteSource bs = ByteSource.wrap(data2); InputStream is = bs.openBufferedStream(); int actual = KMPMatch.indexOf(is, pattern2); assertEquals(26, actual); }