Package org.apache.tika.detect
Class MagicDetector
- java.lang.Object
-
- org.apache.tika.detect.MagicDetector
-
- All Implemented Interfaces:
Serializable
,Detector
public class MagicDetector extends Object implements 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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MagicDetector(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MediaType
detect(InputStream input, Metadata metadata)
Detects the content type of the given input document.int
getLength()
static MagicDetector
parse(MediaType mediaType, String type, String offset, String value, String mask)
String
toString()
Returns a string representation of the Detection Rule.
-
-
-
Constructor Detail
-
MagicDetector
public MagicDetector(MediaType type, byte[] pattern)
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
public 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.- 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.pattern
must 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 Detail
-
parse
public static MagicDetector parse(MediaType mediaType, String type, String offset, String value, String mask)
-
detect
public MediaType detect(InputStream input, Metadata metadata) throws IOException
Description copied from interface:Detector
Detects the content type of the given input document. Returnsapplication/octet-stream
if 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 given stream is guaranteed to support themark feature
and the detector is expected tomark
the stream before reading any bytes from it, and toreset
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:
detect
in interfaceDetector
- Parameters:
input
- document input stream, ornull
metadata
- ignored- Returns:
- detected media type, or
application/octet-stream
- Throws:
IOException
- if the document input stream could not be read
-
getLength
public int getLength()
-
-