HtmlDetector implements Detector { @Override public Format detect(Metadata metadata, TikaInputStream inputStream) throws IOException { if (inputStream == null) { return null; } else { inputStream.mark(FormatUtils.META_TAG_BUFFER_SIZE); byte[] buffer = new byte[FormatUtils.META_TAG_BUFFER_SIZE]; int n = 0; for (int m = inputStream.read(buffer); m != -1 && n < buffer.length; m = inputStream.read(buffer, n, buffer.length - n)) { n += m; } inputStream.reset(); String head = FormatUtils.readFromBuffer(buffer, 0, n); try (InputStream stream = TikaInputStream.get(IOUtils.toInputStream(head))) { Charset charset = htmlEncodingDetector.detect(stream, metadata); if (charset != null) { return new Format(htmlFormatFamily, charset.name()); } } return null; } }  @Override Format detect(Metadata metadata, TikaInputStream inputStream);  }
@Test public void guess_html_format_success() throws Exception { String fileName = "sales-force.xls"; Format actual = htmlDetector.detect(this.getClass().getResourceAsStream(fileName)); assertTrue(actual.getFormatFamily() instanceof HtmlFormatFamily); assertEquals("ISO-8859-1", actual.getEncoding()); }
@Test public void guess_html_format_fail() throws Exception { String fileName = "foo.html"; Format actual = htmlDetector.detect(this.getClass().getResourceAsStream(fileName)); assertNull(actual); }