Package org.apache.tika.detect
Class MagicDetector
java.lang.Object
org.apache.tika.detect.MagicDetector
- All Implemented Interfaces:
Serializable,SelfConfiguring,Detector
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" / "stringignorecase"
- Since:
- Apache Tika 0.3
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMagicDetector(MediaType type, byte[] pattern) Creates a detector for input documents that have the exact given byte pattern at the beginning of the document stream.MagicDetector(MediaType type, byte[] pattern, byte[] mask, boolean isRegex, boolean isStringIgnoreCase, int offsetRangeBegin, int offsetRangeEnd) Creates a detector for input documents that meet the specified magic match.MagicDetector(MediaType type, byte[] pattern, byte[] mask, boolean isRegex, int offsetRangeBegin, int offsetRangeEnd) Creates a detector for input documents that meet the specified magic match.MagicDetector(MediaType type, byte[] pattern, byte[] mask, int offsetRangeBegin, int offsetRangeEnd) Creates a detector for input documents that meet the specified magic match.MagicDetector(MediaType type, byte[] pattern, int offset) Creates a detector for input documents that have the exact given byte pattern at the given offset of the document stream. -
Method Summary
Modifier and TypeMethodDescriptiondetect(TikaInputStream tis, Metadata metadata, ParseContext parseContext) Detects the content type of the given input document.intbooleanmatches(byte[] data) Checks if the given byte array matches this magic pattern.static MagicDetectortoString()Returns a string representation of the Detection Rule.
-
Constructor Details
-
MagicDetector
Creates a detector for input documents that have the exact given byte pattern at the beginning of the document stream.- Parameters:
type- matching media typepattern- magic match pattern
-
MagicDetector
Creates a detector for input documents that have the exact given byte pattern at the given offset of the document stream.- Parameters:
type- matching media typepattern- magic match patternoffset- offset of the pattern match
-
MagicDetector
public MagicDetector(MediaType type, byte[] pattern, byte[] mask, int offsetRangeBegin, int offsetRangeEnd) Creates a detector for input documents that meet the specified magic match.patternmust NOT be a regular expression. Constructor maintained for legacy reasons. -
MagicDetector
public MagicDetector(MediaType type, byte[] pattern, byte[] mask, boolean isRegex, int offsetRangeBegin, int offsetRangeEnd) Creates a detector for input documents that meet the specified magic match. -
MagicDetector
public MagicDetector(MediaType type, byte[] pattern, byte[] mask, boolean isRegex, boolean isStringIgnoreCase, int offsetRangeBegin, int offsetRangeEnd) Creates a detector for input documents that meet the specified magic match.
-
-
Method Details
-
parse
-
detect
public MediaType detect(TikaInputStream tis, Metadata metadata, ParseContext parseContext) throws IOException Description copied from interface:DetectorDetects the content type of the given input document. Returnsapplication/octet-streamif the type of the document can not be detected.If the document input stream is not available, then the first argument may be
null. Otherwise the detector may read bytes from the start of the stream to help in type detection. The detector is expected to mark the stream before reading any bytes from it, and to reset the stream before returning. The stream must not be closed by the detector.The given input metadata is only read, not modified, by the detector.
- Specified by:
detectin interfaceDetector- Parameters:
tis- document input stream, ornullmetadata- ignoredparseContext- the parse context- Returns:
- detected media type, or
application/octet-stream - Throws:
IOException- if the document input stream could not be read
-
getLength
public int getLength() -
matches
public boolean matches(byte[] data) Checks if the given byte array matches this magic pattern. This is a more efficient alternative todetect(TikaInputStream, Metadata, ParseContext)when you already have the bytes in memory.- Parameters:
data- the byte array to check- Returns:
- true if the data matches this magic pattern, false otherwise
-
toString
Returns a string representation of the Detection Rule. Should sort nicely by type and details, as we sometimes compare these.
-