MagicDetector implements Detector { public MediaType detect(InputStream input, Metadata metadata) throws IOException { if (input == null) { return MediaType.OCTET_STREAM; } input.mark(offsetRangeEnd + length); try { int offset = 0; while (offset < offsetRangeBegin) { long n = input.skip(offsetRangeBegin - offset); if (n > 0) { offset += n; } else if (input.read() != -1) { offset += 1; } else { return MediaType.OCTET_STREAM; } } byte[] buffer = new byte[length + (offsetRangeEnd - offsetRangeBegin)]; int n = input.read(buffer); if (n > 0) { offset += n; } while (n != -1 && offset < offsetRangeEnd + length) { int bufferOffset = offset - offsetRangeBegin; n = input.read( buffer, bufferOffset, buffer.length - bufferOffset); if (n > 0) { offset += n; } } if (this.isRegex) { int flags = 0; if (this.isStringIgnoreCase) { flags = Pattern.CASE_INSENSITIVE; } Pattern p = Pattern.compile(new String(this.pattern, UTF_8), flags); ByteBuffer bb = ByteBuffer.wrap(buffer); CharBuffer result = ISO_8859_1.decode(bb); Matcher m = p.matcher(result); boolean match = false; for (int i = 0; i <= offsetRangeEnd - offsetRangeBegin; i++) { m.region(i, length+i); match = m.lookingAt(); if (match) { return type; } } } else { if (offset < offsetRangeBegin + length) { return MediaType.OCTET_STREAM; } for (int i = 0; i <= offsetRangeEnd - offsetRangeBegin; i++) { boolean match = true; int masked; for (int j = 0; match && j < length; j++) { masked = (buffer[i + j] & mask[j]); if (this.isStringIgnoreCase) { masked = Character.toLowerCase(masked); } match = (masked == pattern[j]); } if (match) { return type; } } } return MediaType.OCTET_STREAM; } finally { input.reset(); } } MagicDetector(MediaType type, byte[] pattern);  MagicDetector(MediaType type, byte[] pattern, int offset);  MagicDetector(
        MediaType type, byte[] pattern, byte[] mask,
        int offsetRangeBegin, int offsetRangeEnd);  MagicDetector(
            MediaType type, byte[] pattern, byte[] mask,
            boolean isRegex,
            int offsetRangeBegin, int offsetRangeEnd);  MagicDetector(
            MediaType type, byte[] pattern, byte[] mask,
            boolean isRegex, boolean isStringIgnoreCase,
            int offsetRangeBegin, int offsetRangeEnd); static MagicDetector parse(
            MediaType mediaType,
            String type, String offset, String value, String mask); MediaType detect(InputStream input, Metadata metadata); int getLength(); String toString();  }
@Test public void testDetectNull() throws Exception { MediaType html = new MediaType("text", "html"); Detector detector = new MagicDetector(html, "<html".getBytes(US_ASCII)); assertEquals( MediaType.OCTET_STREAM, detector.detect(null, new Metadata())); }
@Test public void testDetectStreamReadProblems() throws Exception { byte[] data = "abcdefghijklmnopqrstuvwxyz0123456789".getBytes(US_ASCII); MediaType testMT = new MediaType("application", "test"); Detector detector = new MagicDetector(testMT, data, null, false, 0, 0); InputStream stream = new RestrictiveInputStream(data); assertEquals(testMT, detector.detect(stream, new Metadata())); }
@Test public void testDetectApplicationEnviHdr() throws Exception { InputStream iStream = MagicDetectorTest.class.getResourceAsStream( "/test-documents/ang20150420t182050_corr_v1e_img.hdr"); byte[] data = IOUtils.toByteArray(iStream); MediaType testMT = new MediaType("application", "envi.hdr"); Detector detector = new MagicDetector(testMT, data, null, false, 0, 0); InputStream stream = new RestrictiveInputStream(data); assertEquals(testMT, detector.detect(stream, new Metadata())); }