determine String Encoding - Java XML

Java examples for XML:XML Encoding

Description

determine String Encoding

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String determineEncoding(InputStream stream)
            throws IOException {
        stream.mark(20000);//from   ww w  .  j  a  v  a2  s  .  c o m
        try {
            int b0 = stream.read();
            int b1 = stream.read();
            int b2 = stream.read();
            int b3 = stream.read();

            if (b0 == 0xFE && b1 == 0xFF)
                return "UTF-16BE"; //$NON-NLS-1$
            else if (b0 == 0xFF && b1 == 0xFE)
                return "UTF-16LE"; //$NON-NLS-1$
            else if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF)
                return "UTF-8"; //$NON-NLS-1$
            else if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F)
                return "UTF-16BE"; //$NON-NLS-1$
            else if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00)
                return "UTF-16LE"; //$NON-NLS-1$
            else if (b0 == 0x3C && b1 == 0x3F && b2 == 0x78 && b3 == 0x6D) {
                InputStreamReader rdr = new InputStreamReader(stream,
                        "US-ASCII"); //$NON-NLS-1$
                String hdr = readFirstLine(rdr);
                return extractEncoding(hdr);
            } else
                return null;
        } finally {
            stream.reset();
        }
    }

    private static String readFirstLine(InputStreamReader rdr)
            throws IOException {
        char[] buf = new char[1];
        StringBuffer bldr = new StringBuffer();
        rdr.read(buf);
        while (buf[0] != '>') {
            bldr.append(buf[0]);
            rdr.read(buf);
        }
        return bldr.toString();
    }

    private static String extractEncoding(String hdr) {
        int i = hdr.indexOf("encoding="); //$NON-NLS-1$
        if (i == -1)
            return null;
        hdr = hdr.substring(i + 9);
        char sep = hdr.charAt(0);
        hdr = hdr.substring(1);
        i = hdr.indexOf(sep);
        if (i == -1)
            return null;
        return hdr.substring(0, i);
    }
}

Related Tutorials