MCPcopy Create free account
hub / github.com/apache/tika / TextDetector

Class TextDetector

tika-core/src/main/java/org/apache/tika/detect/TextDetector.java:39–141  ·  view source on GitHub ↗

Content type detection of plain text documents. This detector looks at the beginning of the document input stream and considers the document to be a text document if no ASCII (ISO-Latin-1, UTF-8, etc.) control bytes are found. As a special case some control bytes (up to 2% of all characters) are als

Source from the content-addressed store, hash-verified

37 * @since Apache Tika 0.3
38 */
39public class TextDetector implements Detector {
40
41 /**
42 * Serial version UID
43 */
44 private static final long serialVersionUID = 4774601079503507765L;
45
46 /**
47 * The number of bytes from the beginning of the document stream
48 * to test for control bytes.
49 */
50 private static final int DEFAULT_NUMBER_OF_BYTES_TO_TEST = 512;
51
52 /**
53 * Lookup table for all the ASCII/ISO-Latin/UTF-8/etc. control bytes
54 * in the range below 0x20 (the space character). If an entry in this
55 * table is <code>true</code> then that byte is very unlikely to occur
56 * in a plain text document.
57 * <p>
58 * The contents of this lookup table are based on the following definition
59 * from section 4 of the "Content-Type Processing Model" Internet-draft
60 * (<a href="http://webblaze.cs.berkeley.edu/2009/mime-sniff/mime-sniff.txt"
61 * >draft-abarth-mime-sniff-01</a>).
62 * <pre>
63 * +-------------------------+
64 * | Binary data byte ranges |
65 * +-------------------------+
66 * | 0x00 -- 0x08 |
67 * | 0x0B |
68 * | 0x0E -- 0x1A |
69 * | 0x1C -- 0x1F |
70 * +-------------------------+
71 * </pre>
72 *
73 * @see <a href="https://issues.apache.org/jira/browse/TIKA-154">TIKA-154</a>
74 */
75 private static final boolean[] IS_CONTROL_BYTE = new boolean[0x20];
76
77 static {
78 Arrays.fill(IS_CONTROL_BYTE, true);
79 IS_CONTROL_BYTE[0x09] = false; // tabulator
80 IS_CONTROL_BYTE[0x0A] = false; // new line
81 IS_CONTROL_BYTE[0x0C] = false; // new page
82 IS_CONTROL_BYTE[0x0D] = false; // carriage return
83 IS_CONTROL_BYTE[0x1B] = false; // escape
84 }
85
86 private final int bytesToTest;
87
88 /**
89 * Constructs a {@link TextDetector} which will look at the default number
90 * of bytes from the beginning of the document.
91 */
92 public TextDetector() {
93 this(DEFAULT_NUMBER_OF_BYTES_TO_TEST);
94 }
95
96 /**

Callers

nothing calls this directly

Calls 1

fillMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…