Here you can find the source of detectCharset(File f, String[] charsets)
public static Charset detectCharset(File f, String[] charsets) throws Exception
//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; } }