SearchPattern { public int endsWith(byte[] data, int offset, int length) { validate(data, offset, length); int skip = (pattern.length <= length) ? (offset + length - pattern.length) : offset; while (skip < offset + length) { for (int i = (offset + length - 1) - skip; data[skip + i] == pattern[i]; --i) { if (i == 0) return (offset + length - skip); } if (skip + pattern.length - 1 < data.length) skip += table[0xff & data[skip + pattern.length - 1]]; else skip++; } return 0; } private  SearchPattern(byte[] pattern); static SearchPattern compile(byte[] pattern); static SearchPattern compile(String pattern); int match(byte[] data, int offset, int length); int endsWith(byte[] data, int offset, int length); int startsWith(byte[] data, int offset, int length, int matched); int getLength();  }
@Test public void testEndsWith() { byte[] p = "pneumonoultramicroscopicsilicovolcanoconiosis".getBytes(StandardCharsets.US_ASCII); byte[] d = "pneumonoultrami".getBytes(StandardCharsets.US_ASCII); SearchPattern sp = SearchPattern.compile(p); assertEquals(15, sp.endsWith(d, 0, d.length)); p = "abcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.US_ASCII); d = "abcdefghijklmnopqrstuvwxyzabcdefghijklmno".getBytes(StandardCharsets.US_ASCII); sp = SearchPattern.compile(p); assertEquals(0, sp.match(d, 0, d.length)); assertEquals(-1, sp.match(d, 1, d.length - 1)); assertEquals(15, sp.endsWith(d, 0, d.length)); p = "abcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.US_ASCII); d = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.US_ASCII); sp = SearchPattern.compile(p); assertEquals(0, sp.match(d, 0, d.length)); assertEquals(26, sp.match(d, 1, d.length - 1)); assertEquals(26, sp.endsWith(d, 0, d.length)); p = "hello world".getBytes(StandardCharsets.US_ASCII); d = "there is definitely no match in here".getBytes(StandardCharsets.US_ASCII); sp = SearchPattern.compile(p); assertEquals(0, sp.endsWith(d, 0, d.length)); }