void detectEncoding() throws java.lang.Exception
    {
        byte signature[] = new byte[4];

        // Read the first four bytes for
        // autodetection.
        is.mark(4);
        is.read(signature);
        is.reset();

        // Look for a known signature.
        if (tryEncoding(signature, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x3c)) {
            // UCS-4 must begin with "<!XML"
            // 0x00 0x00 0x00 0x3c: UCS-4, big-endian (1234)
            encoding = ENCODING_UCS_4_1234;
        } else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x00,
                (byte) 0x00, (byte) 0x00)) {
            // UCS-4 must begin with "<!XML"
            // 0x3c 0x00 0x00 0x00: UCS-4, little-endian (4321)
            encoding = ENCODING_UCS_4_4321;
        } else if (tryEncoding(signature, (byte) 0x00, (byte) 0x00,
                (byte) 0x3c, (byte) 0x00)) {
            // UCS-4 must begin with "<!XML"
            // 0x00 0x00 0x3c 0x00: UCS-4, unusual (2143)
            encoding = ENCODING_UCS_4_2143;
        } else if (tryEncoding(signature, (byte) 0x00, (byte) 0x3c,
                (byte) 0x00, (byte) 0x00)) {
            // UCS-4 must begin with "<!XML"
            // 0x00 0x3c 0x00 0x00: UCS-4, unusual (3421)
            encoding = ENCODING_UCS_4_3412;
        } else if (tryEncoding(signature, (byte) 0xfe, (byte) 0xff)) {
            // UCS-2 with a byte-order marker.
            // 0xfe 0xff: UCS-2, big-endian (12)
            encoding = ENCODING_UCS_2_12;
            is.read();
            is.read();
        } else if (tryEncoding(signature, (byte) 0xff, (byte) 0xfe)) {
            // UCS-2 with a byte-order marker.
            // 0xff 0xfe: UCS-2, little-endian (21)
            encoding = ENCODING_UCS_2_21;
            is.read();
            is.read();
        } else if (tryEncoding(signature, (byte) 0x00, (byte) 0x3c,
                (byte) 0x00, (byte) 0x3f)) {
            // UCS-2 without a BOM must begin with "<?XML"
            // 0x00 0x3c 0x00 0x3f: UCS-2, big-endian, no byte-order mark
            encoding = ENCODING_UCS_2_12;
            error("no byte-order mark for UCS-2 entity", null, null);
        } else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x00,
                (byte) 0x3f, (byte) 0x00)) {
            // UCS-2 without a BOM must begin with "<?XML"
            // 0x3c 0x00 0x3f 0x00: UCS-2, little-endian, no byte-order mark
            encoding = ENCODING_UCS_2_21;
            error("no byte-order mark for UCS-2 entity", null, null);
        } else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x3f,
                (byte) 0x78, (byte) 0x6d)) {
            // Some kind of 8-bit encoding with "<?XML"
            // 0x3c 0x3f 0x78 0x6d: UTF-8 or other 8-bit markup (read ENCODING)
            encoding = ENCODING_UTF_8;
            read8bitEncodingDeclaration();
        } else {
            // Some kind of 8-bit encoding without "<?XML"
            // (otherwise) UTF-8 without encoding/XML declaration
            encoding = ENCODING_UTF_8;
        }
    }