Java File Read by Charset detectCharset(File f, String[] charsets)

Here you can find the source of detectCharset(File f, String[] charsets)

Description

detect Charset

License

Open Source License

Declaration

public static Charset detectCharset(File f, String[] charsets) throws Exception 

Method Source Code

//package com.java2s;
/*//  ww  w. j a  v a 2  s  .c om
 * License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt
 */

import java.io.*;

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {
    public static Charset detectCharset(File f, String[] charsets) throws Exception {
        Charset charset = null;
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));
        byte[] buffer = new byte[5120];
        input.read(buffer);
        for (String charsetName : charsets) {
            charset = detectCharset(buffer, Charset.forName(charsetName));
            if (charset != null) {
                break;
            }
        }
        input.close();
        return charset;
    }

    private static Charset detectCharset(byte[] buffer, Charset charset) {
        try {
            CharsetDecoder decoder = charset.newDecoder();
            decoder.reset();

            boolean identified = identify(buffer, decoder);

            if (identified) {
                return charset;
            } else {
                return null;
            }

        } catch (Exception e) {
            return null;
        }
    }

    private static boolean identify(byte[] bytes, CharsetDecoder decoder) {
        try {
            decoder.decode(ByteBuffer.wrap(bytes));
        } catch (CharacterCodingException e) {
            return false;
        }
        return true;
    }
}

Related

  1. bomStream(String charset, String file)
  2. copyFileWithEolConversion(File inFile, File outFile, Charset charset)
  3. doTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)
  4. fileContent(File file, Charset charset)
  5. fileToString(final File f, final Charset c)
  6. fileToString(final Path path, final Charset charset)