SimplePrefixMatcher extends AbstractRegisteredItem implements SignatureMatcher { @Override public boolean matches(byte[] data) { for (byte[] prefix : prefixes) { if (ByteUtil.matches(data, prefix, offset)) return true; } return false; } SimplePrefixMatcher(String theId, byte[]... thePrefixes);  SimplePrefixMatcher(String theId, int theOffset, byte[]... thePrefixes); @Override boolean matches(byte[] data); SignatureMatcher and(final String id, final SimplePrefixMatcher other); SignatureMatcher or(final String id, final SimplePrefixMatcher other); static final SimplePrefixMatcher PDF; static final SimplePrefixMatcher GIF; static final SimplePrefixMatcher PNG; static final SimplePrefixMatcher JavaClass; static final SimplePrefixMatcher JPG; static final SignatureMatcher JPG_WITH_EXIF; static final SimplePrefixMatcher JPG2000; }
@Test public void matches() { SimplePrefixMatcher spm = new SimplePrefixMatcher("dummy", new byte[] { 0, 1, 2} ); assertTrue(spm.matches(new byte[] { 0, 1, 2} )); assertTrue(spm.matches(new byte[] { 0, 1, 2, 3, 4} )); assertFalse(spm.matches(null)); assertFalse(spm.matches(new byte[] { 0, 0, 1, 2} )); }
@Test public void matchesPredefined() { byte[] data = AbstractConstraintTest.readResource("32px.png"); assertTrue(SimplePrefixMatcher.PNG.matches(data)); assertFalse(SimplePrefixMatcher.GIF.matches(data)); assertFalse(SimplePrefixMatcher.PDF.matches(data)); data = AbstractConstraintTest.readResource("throbber.gif"); assertTrue(SimplePrefixMatcher.GIF.matches(data)); assertFalse(SimplePrefixMatcher.PNG.matches(data)); assertFalse(SimplePrefixMatcher.PDF.matches(data)); }