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

Class MagicDetector

tika-core/src/main/java/org/apache/tika/detect/MagicDetector.java:45–438  ·  view source on GitHub ↗

Content type detection based on magic bytes, i.e. type-specific patterns near the beginning of the document input stream. Because this works on bytes, not characters, by default any string matching is done as ISO_8859_1. To use an explicit different encoding, supply a type other than "string" /

Source from the content-addressed store, hash-verified

43 * @since Apache Tika 0.3
44 */
45public class MagicDetector implements Detector {
46
47 /**
48 * The matching media type. Returned by the
49 * {@link #detect(InputStream, Metadata)} method if a match is found.
50 */
51 private final MediaType type;
52 /**
53 * Length of the comparison window.
54 */
55 private final int length;
56 /**
57 * The magic match pattern. If this byte pattern is equal to the
58 * possibly bit-masked bytes from the input stream, then the type
59 * detection succeeds and the configured {@link #type} is returned.
60 */
61 private final byte[] pattern;
62 /**
63 * Length of the pattern, which in the case of regular expressions will
64 * not be the same as the comparison window length.
65 */
66 private final int patternLength;
67 /**
68 * True if pattern is a regular expression, false otherwise.
69 */
70 private final boolean isRegex;
71 /**
72 * True if we're doing a case-insensitive string match, false otherwise.
73 */
74 private final boolean isStringIgnoreCase;
75 /**
76 * Bit mask that is applied to the source bytes before pattern matching.
77 */
78 private final byte[] mask;
79 /**
80 * First offset (inclusive) of the comparison window within the
81 * document input stream. Greater than or equal to zero.
82 */
83 private final int offsetRangeBegin;
84 /**
85 * Last offset (inclusive) of the comparison window within the document
86 * input stream. Greater than or equal to the
87 * {@link #offsetRangeBegin first offset}.
88 * <p>
89 * Note that this is <em>not</em> the offset of the last byte read from
90 * the document stream. Instead, the last window of bytes to be compared
91 * starts at this offset.
92 */
93 private final int offsetRangeEnd;
94
95 /**
96 * Creates a detector for input documents that have the exact given byte
97 * pattern at the beginning of the document stream.
98 *
99 * @param type matching media type
100 * @param pattern magic match pattern
101 */
102 public MagicDetector(MediaType type, byte[] pattern) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…