Class MetadataCharsetDetector
- All Implemented Interfaces:
Serializable,SelfConfiguring,EncodingDetector
EncodingResult.ResultType.DECLARATIVE result when a charset is found.
Two metadata keys are consulted in order:
HttpHeaders.CONTENT_TYPE— thecharsetparameter of the HTTP/MIME Content-Type header (e.g.text/html; charset=UTF-8).HttpHeaders.CONTENT_ENCODING— a bare charset label set by parsers such asRFC822Parser, 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CharsetcharsetFromContentEncoding(Metadata metadata) Returns the charset named inHttpHeaders.CONTENT_ENCODING, ornullif absent or unparseable.static CharsetcharsetFromContentType(Metadata metadata) Returns the charset named in thecharsetparameter of theHttpHeaders.CONTENT_TYPEvalue, ornullif absent or unparseable.detect(TikaInputStream tis, Metadata metadata, ParseContext context) Detects the character encoding of the given text document.
-
Constructor Details
-
MetadataCharsetDetector
public MetadataCharsetDetector()
-
-
Method Details
-
detect
public List<EncodingResult> detect(TikaInputStream tis, Metadata metadata, ParseContext context) throws IOException Description copied from interface:EncodingDetectorDetects 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 themark featureand the detector is expected tomarkthe stream before reading any bytes from it, and toresetthe 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 interfaceEncodingDetector- Parameters:
tis- text document input stream, ornullmetadata- input metadata for the documentcontext- 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
Returns the charset named in thecharsetparameter of theHttpHeaders.CONTENT_TYPEvalue, ornullif absent or unparseable. -
charsetFromContentEncoding
Returns the charset named inHttpHeaders.CONTENT_ENCODING, ornullif absent or unparseable. This key is used byRFC822Parserto expose the charset declared in MIME body-part headers when the bare media type is stored separately inHttpHeaders.CONTENT_TYPE.
-