Reads the first bytes of the input stream to guess the text encoding. @param encoding encoding (ignored if null) @throws IOException I/O exception
(final String encoding)
| 68 | * @throws IOException I/O exception |
| 69 | */ |
| 70 | private void guess(final String encoding) throws IOException { |
| 71 | try { |
| 72 | final int a = readByte(), b = readByte(), c = readByte(), d = readByte(); |
| 73 | String enc = normEncoding(encoding, false); |
| 74 | int skip = 0; |
| 75 | if(Strings.eq(enc, UTF8, null) && a == 0xEF && b == 0xBB && c == 0xBF) { |
| 76 | enc = UTF8; |
| 77 | skip = 3; |
| 78 | } else if(Strings.eq(enc, UTF16, UTF16LE, null) && a == 0xFF && b == 0xFE) { |
| 79 | enc = UTF16LE; |
| 80 | skip = 2; |
| 81 | } else if(Strings.eq(enc, UTF16, UTF16BE, null) && a == 0xFE && b == 0xFF) { |
| 82 | enc = UTF16BE; |
| 83 | skip = 2; |
| 84 | } else if(Strings.eq(enc, UTF16, null) && a == '<' && b == 0 && c == '?' && d == 0) { |
| 85 | enc = UTF16LE; |
| 86 | } else if(Strings.eq(enc, UTF16, null) && a == 0 && b == '<' && c == 0 && d == '?') { |
| 87 | enc = UTF16BE; |
| 88 | } else if(Strings.eq(enc, UTF16)) { |
| 89 | enc = UTF16BE; |
| 90 | } else if(enc == null) { |
| 91 | enc = UTF8; |
| 92 | } |
| 93 | reset(); |
| 94 | for(int s = 0; s < skip; s++) readByte(); |
| 95 | decoder = TextDecoder.get(enc); |
| 96 | } catch(final IOException ex) { |
| 97 | close(); |
| 98 | throw ex; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Checks the input for valid XML characters and throws an exception if invalid |