Java Charset Create getEncoding(byte[] htmlData)

Here you can find the source of getEncoding(byte[] htmlData)

Description

get Encoding

License

Open Source License

Declaration

public static Charset getEncoding(byte[] htmlData) 

Method Source Code

//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;
    }
}

Related

  1. getEncodedUrl(String unEncodedUrl)
  2. getEncoder()
  3. getEncoder()
  4. getEncoder()
  5. getEncoder(String encoding)
  6. getEncoding(OutputStreamWriter inWriter)
  7. getEncoding(String text)
  8. getEncodingOfXml(File file)
  9. getEncodingOption(List options)