Class MetadataCharsetDetector

java.lang.Object
org.apache.tika.detect.MetadataCharsetDetector
All Implemented Interfaces:
Serializable, SelfConfiguring, EncodingDetector

public class MetadataCharsetDetector extends Object implements EncodingDetector
Encoding detector that extracts a declared charset from Tika metadata without reading any bytes from the stream. Returns a single EncodingResult.ResultType.DECLARATIVE result when a charset is found.

Two metadata keys are consulted in order:

  1. HttpHeaders.CONTENT_TYPE — the charset parameter of the HTTP/MIME Content-Type header (e.g. text/html; charset=UTF-8).
  2. HttpHeaders.CONTENT_ENCODING — a bare charset label set by parsers such as RFC822Parser, which splits Content-Type into a bare media-type key and a separate charset key.

This detector is SPI-loaded in tika-core and therefore always present in the default encoding-detector chain. Its DECLARATIVE result is visible to JunkFilterEncodingDetector, which honours it when its decoding is byte-identical to at least one other candidate's.

Since:
Apache Tika 4.0
See Also:
  • Constructor Details

    • MetadataCharsetDetector

      public MetadataCharsetDetector()
  • Method Details

    • detect

      public List<EncodingResult> detect(TikaInputStream tis, Metadata metadata, ParseContext context) throws IOException
      Description copied from interface: EncodingDetector
      Detects the character encoding of the given text document.

      Returns an empty list if the encoding cannot be determined. Returns a ranked list of candidates in descending confidence order otherwise. The first entry is always the best guess.

      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 encoding 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 EncodingDetector
      Parameters:
      tis - text document input stream, or null
      metadata - input metadata for the document
      context - the parse context
      Returns:
      ranked list of encoding results, empty if unknown; never null
      Throws:
      IOException - if the document input stream could not be read
    • charsetFromContentType

      public static Charset charsetFromContentType(Metadata metadata)
      Returns the charset named in the charset parameter of the HttpHeaders.CONTENT_TYPE value, or null if absent or unparseable.
    • charsetFromContentEncoding

      public static Charset charsetFromContentEncoding(Metadata metadata)
      Returns the charset named in HttpHeaders.CONTENT_ENCODING, or null if absent or unparseable. This key is used by RFC822Parser to expose the charset declared in MIME body-part headers when the bare media type is stored separately in HttpHeaders.CONTENT_TYPE.