Using Java.nio.charset.CharsetDecoder to automatically identify character sets

Source: Internet
Author: User

This paper studies the method of automatically identifying character set which can be found on the Internet, and it is effective to use the third-party class library Jchardet. Also useful cpdetector, actually also use Jchardet. Accidental discovery of JDK java.nio.charset.CharsetDecoder can be used to identify character sets.

First, the principle

The InputStreamReader is generally constructed in two ways:

New InputStreamReader (in, CharsetName);

Or

New InputStreamReader (in, CharSet);

If the charset does not match, the output is garbled.

There is also a way to build, using Charsetdecoder:

Charsetdecoder cd =new InputStreamReader (in, CD);

If this does not match, an exception is thrown:

Java.nio.charset.MalformedInputException:Input length = 1 at    java.nio.charset.CoderResult.throwException ( Coderresult.java:277) at    sun.nio.cs.StreamDecoder.implRead (streamdecoder.java:338)    at Sun.nio.cs.StreamDecoder.read (Streamdecoder.java:177) ...        .

This allows you to use it as a character set probe.

Second, the use of Autocharsetreader

Autocharsetreader is based on the above principle, reference InputStreamReader and written class, inherit reader, can be regarded as CharSet adaptive inputstreamreader.

New Autocharsetreader (in); char c = ar.read (); .... Char New Char [new  bufferedreader (AR); Br.readline ();

Another example is Lucene. TextField, which creates a full-text index, requires reader parameters that can be used directly from this class:

New New Autocharsetreader (file));

After you finish reading the file, you can get the charset of the file. Note that after reading it.

Charset Charset = Ar.charset ();

Third, alternate character sets

Because the character set is ultimately determined using a multiple-attempt approach, an alternative is provided. The default alternate character set provided by the current code is as follows:

    Private Final Static string[] _defaultcharsets = {                    "Us-ascii",            "UTF-8"            , "GB2312",             "BIG5",            "GBK",            "GB18030             ",                            "Utf-16be", " Utf-16le ",             " UTF-16 ",            " UNICODE "};

A method for changing the alternate character set is also provided. Like what:

New Autocharsetreader (in). Setcharset ("ASCII", "Utf-8", "GBK");

The sequencing will affect the detection results. For example, if GBK before GB2312, then the test results can only be GBK, not GB2312, because GBK contains GB2312.

Four, only for character set detection

Can be used only for character set detection:

Charsetscharsetsstops);

Quickdetect read-only one character, applicable to single character set files. For HTML, you may need to read all to know CharSet, then use Deepdetect. Where the parameter charsets can be null.

If a set of files, known possible character sets have "ASCII", "Utf-8", "gb2312", and "GBK", when the detection of a file character set is "Utf-8" or "GBK", you can return the results immediately, do not need to continue to read the file. The stops parameter can then be assigned a value of {"Utf-8", "GBK"}. null is required to complete the reading.

V. Other

To improve efficiency, this class has buffer, the primary character set decoding failed, do not have to re-read IO. The buffer size defaults to 8192, and when the object is built, the buffer size can be customized and set to 16 if the parameter is less than 16.

Vi. Source Code

http://download.csdn.net/detail/u012994553/9777709

Using Java.nio.charset.CharsetDecoder to automatically identify character sets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.