Here you can find the source of getEncoding(byte[] htmlData)
public static Charset getEncoding(byte[] htmlData)
//package com.java2s; //License from project: Open Source License import java.nio.charset.Charset; public class Main { public static Charset getEncoding(byte[] htmlData) { Charset charset = null;/*from ww w .j ava 2s . c o m*/ String charsetName = getEncodingName(htmlData); if (charsetName != null) { try { charset = Charset.forName(charsetName); } catch (Exception e) { System.err.println("Charset encoding " + charsetName + " not found"); charset = null; } } return charset; } public static String getEncodingName(byte[] htmlData) { String charsetName = null; String htmlStr = new String(htmlData); int startIdx = htmlStr.indexOf("charset="); if (startIdx >= 0) { startIdx += "charset=".length(); int endIdx = startIdx; while (!Character.isWhitespace(htmlStr.charAt(endIdx)) && htmlStr.charAt(endIdx) != '\"' && htmlStr.charAt(endIdx) != ';' && htmlStr.charAt(endIdx) != '\'' && endIdx < htmlStr.length()) { endIdx++; } charsetName = htmlStr.substring(startIdx, endIdx); } return charsetName; } }