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:
  • 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

    Modifier and Type
    Method
    Description
    detect(InputStream input, Metadata metadata)
    Detects the content type of the given input document.
    int
     
    parse(MediaType mediaType, String type, String offset, String value, String mask)
     
    Returns a string representation of the Detection Rule.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • 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 type
      pattern - 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 type
      pattern - magic match pattern
      offset - 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 Details

    • 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. Returns application/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 the mark feature and 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:
      detect in interface Detector
      Parameters:
      input - document input stream, or null
      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()
    • toString

      public String toString()
      Returns a string representation of the Detection Rule. Should sort nicely by type and details, as we sometimes compare these.
      Overrides:
      toString in class Object