public class CharsetDetector extends Object
CharsetDetector
provides a facility for detecting the
charset or encoding of character data in an unknown format.
The input data can either be from an input stream or an array of bytes.
The result of the detection operation is a list of possibly matching
charsets, or, for simple use, you can just ask for a Java Reader that
will will work over the input data.
Character set detection is at best an imprecise operation. The detection process will attempt to identify the charset that best matches the characteristics of the byte data, but the process is partly statistical in nature, and the results can not be guaranteed to always be correct.
For best accuracy in charset detection, the input data should be primarily in a single language, and a minimum of a few hundred bytes worth of plain text in the language are needed. The detection process will attempt to ignore html or xml style markup that could otherwise obscure the content.
Constructor and Description |
---|
CharsetDetector()
Constructor
|
CharsetDetector(int markLimit) |
Modifier and Type | Method and Description |
---|---|
CharsetMatch |
detect()
Return the charset that best matches the supplied input data.
|
CharsetMatch[] |
detectAll()
Return an array of all charsets that appear to be plausible
matches with the input data.
|
boolean |
enableInputFilter(boolean filter)
Enable filtering of input text.
|
static String[] |
getAllDetectableCharsets()
Get the names of all charsets supported by
CharsetDetector class. |
String[] |
getDetectableCharsets()
Deprecated.
This API is ICU internal only.
|
Reader |
getReader(InputStream in,
String declaredEncoding)
Autodetect the charset of an inputStream, and return a Java Reader
to access the converted input data.
|
String |
getString(byte[] in,
String declaredEncoding)
Autodetect the charset of an inputStream, and return a String
containing the converted input data.
|
boolean |
inputFilterEnabled()
Test whether or not input filtering is enabled.
|
CharsetDetector |
setDeclaredEncoding(String encoding)
Set the declared encoding for charset detection.
|
CharsetDetector |
setDetectableCharset(String encoding,
boolean enabled)
Deprecated.
This API is ICU internal only.
|
CharsetDetector |
setText(byte[] in)
Set the input text (byte) data whose charset is to be detected.
|
CharsetDetector |
setText(InputStream in)
Set the input text (byte) data whose charset is to be detected.
|
public CharsetDetector()
public CharsetDetector(int markLimit)
public static String[] getAllDetectableCharsets()
CharsetDetector
class.
Note: Multiple different charset encodings in a same family may use a single shared name in this implementation. For example, this method returns an array including "ISO-8859-1" (ISO Latin 1), but not including "windows-1252" (Windows Latin 1). However, actual detection result could be "windows-1252" when the input data matches Latin 1 code points with any points only available in "windows-1252".
CharsetDetector
class.public CharsetDetector setDeclaredEncoding(String encoding)
A declared encoding that is incompatible with the input data being analyzed will not be added to the list of possible encodings.
encoding
- The declared encodingpublic CharsetDetector setText(byte[] in)
in
- the input text of unknown encodingpublic CharsetDetector setText(InputStream in) throws IOException
The input stream that supplies the character data must have markSupported() == true; the charset detection process will read a small amount of data, then return the stream to its original position via the InputStream.reset() operation. The exact amount that will be read depends on the characteristics of the data itself.
in
- the input text of unknown encodingIOException
public CharsetMatch detect()
Note though, that because the detection only looks at the start of the input data, there is a possibility that the returned charset will fail to handle the full set of input data.
Raise an exception if
null
if there are no matches.public CharsetMatch[] detectAll()
Raise an exception if
public Reader getReader(InputStream in, String declaredEncoding)
This is a convenience method that is equivalent to
this.setDeclaredEncoding(declaredEncoding).setText(in).detect().getReader();
For the input stream that supplies the character data, markSupported() must be true; the charset detection will read a small amount of data, then return the stream to its original position via the InputStream.reset() operation. The exact amount that will be read depends on the characteristics of the data itself.
Raise an exception if no charsets appear to match the input data.
in
- The source of the byte data in the unknown charset.declaredEncoding
- A declared encoding for the data, if available,
or null or an empty string if none is available.public String getString(byte[] in, String declaredEncoding)
This is a convenience method that is equivalent to
this.setDeclaredEncoding(declaredEncoding).setText(in).detect().getString();
Raise an exception if no charsets appear to match the input data.
in
- The source of the byte data in the unknown charset.declaredEncoding
- A declared encoding for the data, if available,
or null or an empty string if none is available.public boolean inputFilterEnabled()
true
if input text will be filtered.enableInputFilter(boolean)
public boolean enableInputFilter(boolean filter)
filter
- true
to enable input text filtering.@Deprecated public String[] getDetectableCharsets()
@Deprecated public CharsetDetector setDetectableCharset(String encoding, boolean enabled)
getAllDetectableCharsets()
.encoding
- the name of charset encoding.enabled
- true
to enable, or false
to disable the
charset encoding.CharsetDetector
.IllegalArgumentException
- when the name of charset encoding is
not supported.Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.