Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public Class CharsetDetector

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .Reader, java.util.ArrayList, .Arrays, .Collections, .List

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.

ICU Status
Stable since ICU 3.4.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final List<CharsetDetector.CSRecognizerInfo>
pack-priv short[]
pack-priv boolean
pack-priv String
private boolean[]
pack-priv byte[]
pack-priv int
pack-priv InputStream
pack-priv byte[]
pack-priv int
private boolean
private static final int

Constructor Summary

AccessConstructor and Description
public
CharsetDetector()

Constructor

Method Summary

Modifier and TypeMethod and Description
public CharsetMatch

Returns:

a CharsetMatch object representing the best matching charset, or null if there are no matches.
detect
()

Return the charset that best matches the supplied input data.

public CharsetMatch[]

Returns:

An array of CharsetMatch objects representing possibly matching charsets.
detectAll
()

Return an array of all charsets that appear to be plausible matches with the input data.

public boolean

Returns:

The previous setting.
enableInputFilter
(boolean
true to enable input text filtering.
filter
)

Enable filtering of input text.

public static String[]

Returns:

an array of the names of all charsets supported by CharsetDetector class.
getAllDetectableCharsets
()

Get the names of all charsets supported by CharsetDetector class.

public String[]

Returns:

an array of the names of charsets that can be recognized by this CharsetDetector instance.
getDetectableCharsets
()

Deprecated This API is ICU internal only.
Get the names of charsets that can be recognized by this CharsetDetector instance.
public Reader
getReader(InputStream
The source of the byte data in the unknown charset.
in
,
String
A declared encoding for the data, if available, or null or an empty string if none is available.
declaredEncoding
)

Autodetect the charset of an inputStream, and return a Java Reader to access the converted input data.

public String
getString(byte[]
The source of the byte data in the unknown charset.
in
,
String
A declared encoding for the data, if available, or null or an empty string if none is available.
declaredEncoding
)

Autodetect the charset of an inputStream, and return a String containing the converted input data.

public boolean

Returns:

true if input text will be filtered.
inputFilterEnabled
()

Test whether or not input filtering is enabled.

private void
public CharsetDetector
setDeclaredEncoding(String
The declared encoding
encoding
)

Set the declared encoding for charset detection.

public CharsetDetector

Returns:

A reference to this CharsetDetector.
setDetectableCharset
(String
the name of charset encoding.
encoding
,
boolean
true to enable, or false to disable the charset encoding.
enabled
)

Deprecated This API is ICU internal only.
Enable or disable individual charset encoding.
public CharsetDetector

Returns:

This CharsetDetector
setText
(byte[]
the input text of unknown encoding
in
)

Set the input text (byte) data whose charset is to be detected.

public CharsetDetector

Returns:

This CharsetDetector
setText
(InputStream
the input text of unknown encoding
in
)

Set the input text (byte) data whose charset is to be detected.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALL_CS_RECOGNIZERSback to summary
private static final List<CharsetDetector.CSRecognizerInfo> ALL_CS_RECOGNIZERS
fByteStatsback to summary
pack-priv short[] fByteStats
fC1Bytesback to summary
pack-priv boolean fC1Bytes
fDeclaredEncodingback to summary
pack-priv String fDeclaredEncoding
fEnabledRecognizersback to summary
private boolean[] fEnabledRecognizers
fInputBytesback to summary
pack-priv byte[] fInputBytes
fInputLenback to summary
pack-priv int fInputLen
fInputStreamback to summary
pack-priv InputStream fInputStream
fRawInputback to summary
pack-priv byte[] fRawInput
fRawLengthback to summary
pack-priv int fRawLength
fStripTagsback to summary
private boolean fStripTags
kBufSizeback to summary
private static final int kBufSize

Constructor Detail

CharsetDetectorback to summary
public CharsetDetector()

Constructor

ICU Status
Stable since ICU 3.4.

Method Detail

detectback to summary
public CharsetMatch detect()

Return the charset that best matches the supplied input data. 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

  • no charset appears to match the data.
  • no input text has been provided
Returns:CharsetMatch

a CharsetMatch object representing the best matching charset, or null if there are no matches.

ICU Status
Stable since ICU 3.4.
detectAllback to summary
public CharsetMatch[] detectAll()

Return an array of all charsets that appear to be plausible matches with the input data. The array is ordered with the best quality match first.

Raise an exception if

  • no charsets appear to match the input data.
  • no input text has been provided
Returns:CharsetMatch[]

An array of CharsetMatch objects representing possibly matching charsets.

ICU Status
Stable since ICU 3.4.
enableInputFilterback to summary
public boolean enableInputFilter(boolean filter)

Enable filtering of input text. If filtering is enabled, text within angle brackets ("<" and ">") will be removed before detection.

Parameters
filter:boolean

true to enable input text filtering.

Returns:boolean

The previous setting.

ICU Status
Stable since ICU 3.4.
getAllDetectableCharsetsback to summary
public static String[] getAllDetectableCharsets()

Get the names of all charsets supported by 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".

Returns:String[]

an array of the names of all charsets supported by CharsetDetector class.

ICU Status
Stable since ICU 3.4.
getDetectableCharsetsback to summary
public String[] getDetectableCharsets()

Deprecated

This API is ICU internal only.

Get the names of charsets that can be recognized by this CharsetDetector instance.

Returns:String[]

an array of the names of charsets that can be recognized by this CharsetDetector instance.

Annotations
@Deprecated
ICU Status
Internal ICU component.
getReaderback to summary
public Reader getReader(InputStream in, String declaredEncoding)

Autodetect the charset of an inputStream, and return a Java Reader to access the converted input data.

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.

Parameters
in:InputStream

The source of the byte data in the unknown charset.

declaredEncoding:String

A declared encoding for the data, if available, or null or an empty string if none is available.

ICU Status
Stable since ICU 3.4.
getStringback to summary
public String getString(byte[] in, String declaredEncoding)

Autodetect the charset of an inputStream, and return a String containing the converted input data.

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.

Parameters
in:byte[]

The source of the byte data in the unknown charset.

declaredEncoding:String

A declared encoding for the data, if available, or null or an empty string if none is available.

ICU Status
Stable since ICU 3.4.
inputFilterEnabledback to summary
public boolean inputFilterEnabled()

Test whether or not input filtering is enabled.

Returns:boolean

true if input text will be filtered.

ICU Status
Stable since ICU 3.4.
See Also
enableInputFilter
MungeInputback to summary
private void MungeInput()
setDeclaredEncodingback to summary
public CharsetDetector setDeclaredEncoding(String encoding)

Set the declared encoding for charset detection. The declared encoding of an input text is an encoding obtained from an http header or xml declaration or similar source that can be provided as additional information to the charset detector. A match between a declared encoding and a possible detected encoding will raise the quality of that detected encoding by a small delta, and will also appear as a "reason" for the match.

A declared encoding that is incompatible with the input data being analyzed will not be added to the list of possible encodings.

Parameters
encoding:String

The declared encoding

ICU Status
Stable since ICU 3.4.
setDetectableCharsetback to summary
public CharsetDetector setDetectableCharset(String encoding, boolean enabled)

Deprecated

This API is ICU internal only.

Enable or disable individual charset encoding. A name of charset encoding must be included in the names returned by getAllDetectableCharsets().

Parameters
encoding:String

the name of charset encoding.

enabled:boolean

true to enable, or false to disable the charset encoding.

Returns:CharsetDetector

A reference to this CharsetDetector.

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
when the name of charset encoding is not supported.
ICU Status
Internal ICU component.
setTextback to summary
public CharsetDetector setText(byte[] in)

Set the input text (byte) data whose charset is to be detected.

Parameters
in:byte[]

the input text of unknown encoding

Returns:CharsetDetector

This CharsetDetector

ICU Status
Stable since ICU 3.4.
setTextback to summary
public CharsetDetector setText(InputStream in) throws IOException

Set the input text (byte) data whose charset is to be detected.

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.

Parameters
in:InputStream

the input text of unknown encoding

Returns:CharsetDetector

This CharsetDetector

ICU Status
Stable since ICU 3.4.
com.ibm.icu.text back to summary

private Class CharsetDetector.CSRecognizerInfo

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv CharsetRecognizer

Constructor Summary

AccessConstructor and Description
pack-priv
CSRecognizerInfo(CharsetRecognizer recognizer, boolean isDefaultEnabled)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isDefaultEnabledback to summary
pack-priv boolean isDefaultEnabled
recognizerback to summary
pack-priv CharsetRecognizer recognizer

Constructor Detail

CSRecognizerInfoback to summary
pack-priv CSRecognizerInfo(CharsetRecognizer recognizer, boolean isDefaultEnabled)