@Test public void testMandocBOMDocument() throws IOException { InputStream res = getClass().getClassLoader().getResourceAsStream( "analysis/document/utf16le.1m"); assertNotNull("despite inclusion locally,", res); byte[] buf = readSignature(res); AnalyzerFactory fac; fac = MandocAnalyzerFactory.MATCHER.isMagic(buf, res); assertNotNull("though utf16le.1m is mandoc(5),", fac); assertSame("though utf16le.1m is mandoc(5)", MandocAnalyzerFactory.DEFAULT_INSTANCE, fac); }
@Override public AnalyzerFactory isMagic(byte[] contents, InputStream in) throws IOException { if (!in.markSupported()) { return null; } in.mark(MARK_READ_LIMIT); int bomLength = 0; String encoding = IOUtils.findBOMEncoding(contents); if (encoding == null) { encoding = "UTF-8"; } else { bomLength = IOUtils.skipForBOM(contents); if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } boolean foundLF = hasLineFeed(in, encoding); in.reset(); if (!foundLF) { return null; } if (bomLength > 0) { if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } BufferedReader rdr = new BufferedReader(new InputStreamReader( in, encoding)); boolean foundContent = false; int numFirstChars = 0; int numLines = 0; String line; while ((line = rdr.readLine()) != null) { for (String lineStarter : lineStarters) { if (line.startsWith(lineStarter)) { in.reset(); return factory; } } if (++numLines >= LINE_LIMIT) { in.reset(); return null; } if (!foundContent) { for (int i = 0; i < line.length() && numFirstChars < FIRST_LOOK_WIDTH; ++i, ++numFirstChars) { char c = line.charAt(i); if (c == '.' || c == '\'') { foundContent = true; break; } else if (!Character.isWhitespace(c)) { in.reset(); return null; } } if (!foundContent && numFirstChars >= FIRST_LOOK_WIDTH) { in.reset(); return null; } } } in.reset(); return null; }
DocumentMatcher implements Matcher { @Override public AnalyzerFactory isMagic(byte[] contents, InputStream in) throws IOException { if (!in.markSupported()) { return null; } in.mark(MARK_READ_LIMIT); int bomLength = 0; String encoding = IOUtils.findBOMEncoding(contents); if (encoding == null) { encoding = "UTF-8"; } else { bomLength = IOUtils.skipForBOM(contents); if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } boolean foundLF = hasLineFeed(in, encoding); in.reset(); if (!foundLF) { return null; } if (bomLength > 0) { if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } BufferedReader rdr = new BufferedReader(new InputStreamReader( in, encoding)); boolean foundContent = false; int numFirstChars = 0; int numLines = 0; String line; while ((line = rdr.readLine()) != null) { for (String lineStarter : lineStarters) { if (line.startsWith(lineStarter)) { in.reset(); return factory; } } if (++numLines >= LINE_LIMIT) { in.reset(); return null; } if (!foundContent) { for (int i = 0; i < line.length() && numFirstChars < FIRST_LOOK_WIDTH; ++i, ++numFirstChars) { char c = line.charAt(i); if (c == '.' || c == '\'') { foundContent = true; break; } else if (!Character.isWhitespace(c)) { in.reset(); return null; } } if (!foundContent && numFirstChars >= FIRST_LOOK_WIDTH) { in.reset(); return null; } } } in.reset(); return null; } }
DocumentMatcher implements Matcher { @Override public AnalyzerFactory isMagic(byte[] contents, InputStream in) throws IOException { if (!in.markSupported()) { return null; } in.mark(MARK_READ_LIMIT); int bomLength = 0; String encoding = IOUtils.findBOMEncoding(contents); if (encoding == null) { encoding = "UTF-8"; } else { bomLength = IOUtils.skipForBOM(contents); if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } boolean foundLF = hasLineFeed(in, encoding); in.reset(); if (!foundLF) { return null; } if (bomLength > 0) { if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } BufferedReader rdr = new BufferedReader(new InputStreamReader( in, encoding)); boolean foundContent = false; int numFirstChars = 0; int numLines = 0; String line; while ((line = rdr.readLine()) != null) { for (String lineStarter : lineStarters) { if (line.startsWith(lineStarter)) { in.reset(); return factory; } } if (++numLines >= LINE_LIMIT) { in.reset(); return null; } if (!foundContent) { for (int i = 0; i < line.length() && numFirstChars < FIRST_LOOK_WIDTH; ++i, ++numFirstChars) { char c = line.charAt(i); if (c == '.' || c == '\'') { foundContent = true; break; } else if (!Character.isWhitespace(c)) { in.reset(); return null; } } if (!foundContent && numFirstChars >= FIRST_LOOK_WIDTH) { in.reset(); return null; } } } in.reset(); return null; } DocumentMatcher(AnalyzerFactory factory, String[] lineStarters); }
DocumentMatcher implements Matcher { @Override public AnalyzerFactory isMagic(byte[] contents, InputStream in) throws IOException { if (!in.markSupported()) { return null; } in.mark(MARK_READ_LIMIT); int bomLength = 0; String encoding = IOUtils.findBOMEncoding(contents); if (encoding == null) { encoding = "UTF-8"; } else { bomLength = IOUtils.skipForBOM(contents); if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } boolean foundLF = hasLineFeed(in, encoding); in.reset(); if (!foundLF) { return null; } if (bomLength > 0) { if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } BufferedReader rdr = new BufferedReader(new InputStreamReader( in, encoding)); boolean foundContent = false; int numFirstChars = 0; int numLines = 0; String line; while ((line = rdr.readLine()) != null) { for (String lineStarter : lineStarters) { if (line.startsWith(lineStarter)) { in.reset(); return factory; } } if (++numLines >= LINE_LIMIT) { in.reset(); return null; } if (!foundContent) { for (int i = 0; i < line.length() && numFirstChars < FIRST_LOOK_WIDTH; ++i, ++numFirstChars) { char c = line.charAt(i); if (c == '.' || c == '\'') { foundContent = true; break; } else if (!Character.isWhitespace(c)) { in.reset(); return null; } } if (!foundContent && numFirstChars >= FIRST_LOOK_WIDTH) { in.reset(); return null; } } } in.reset(); return null; } DocumentMatcher(AnalyzerFactory factory, String[] lineStarters); @Override AnalyzerFactory isMagic(byte[] contents, InputStream in); @Override AnalyzerFactory forFactory(); }
DocumentMatcher implements Matcher { @Override public AnalyzerFactory isMagic(byte[] contents, InputStream in) throws IOException { if (!in.markSupported()) { return null; } in.mark(MARK_READ_LIMIT); int bomLength = 0; String encoding = IOUtils.findBOMEncoding(contents); if (encoding == null) { encoding = "UTF-8"; } else { bomLength = IOUtils.skipForBOM(contents); if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } boolean foundLF = hasLineFeed(in, encoding); in.reset(); if (!foundLF) { return null; } if (bomLength > 0) { if (in.skip(bomLength) != bomLength) { in.reset(); return null; } } BufferedReader rdr = new BufferedReader(new InputStreamReader( in, encoding)); boolean foundContent = false; int numFirstChars = 0; int numLines = 0; String line; while ((line = rdr.readLine()) != null) { for (String lineStarter : lineStarters) { if (line.startsWith(lineStarter)) { in.reset(); return factory; } } if (++numLines >= LINE_LIMIT) { in.reset(); return null; } if (!foundContent) { for (int i = 0; i < line.length() && numFirstChars < FIRST_LOOK_WIDTH; ++i, ++numFirstChars) { char c = line.charAt(i); if (c == '.' || c == '\'') { foundContent = true; break; } else if (!Character.isWhitespace(c)) { in.reset(); return null; } } if (!foundContent && numFirstChars >= FIRST_LOOK_WIDTH) { in.reset(); return null; } } } in.reset(); return null; } DocumentMatcher(AnalyzerFactory factory, String[] lineStarters); @Override AnalyzerFactory isMagic(byte[] contents, InputStream in); @Override AnalyzerFactory forFactory();  }