Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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);
        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";
            else if (b0 == 0xFF && b1 == 0xFE)
                return "UTF-16LE";
            else if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF)
                return "UTF-8";
            else if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F)
                return "UTF-16BE";
            else if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00)
                return "UTF-16LE";
            else if (b0 == 0x3C && b1 == 0x3F && b2 == 0x78 && b3 == 0x6D) {
                //            UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding 
                //            which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding 
                //            declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns 
                //            for the relevant ASCII characters, the encoding declaration itself may be read reliably
                InputStreamReader rdr = new InputStreamReader(stream, "US-ASCII");
                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=");
        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);
    }
}