| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.eclipse.parsson; |
| 18 |
|
| 19 |
import jakarta.json.JsonException; |
| 20 |
import java.io.FilterInputStream; |
| 21 |
import java.io.IOException; |
| 22 |
import java.io.InputStream; |
| 23 |
import java.nio.charset.Charset; |
| 24 |
import java.nio.charset.StandardCharsets; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class UnicodeDetectingInputStream extends FilterInputStream { |
| 33 |
private static final byte FF = (byte)0xFF; |
| 34 |
private static final byte FE = (byte)0xFE; |
| 35 |
private static final byte EF = (byte)0xEF; |
| 36 |
private static final byte BB = (byte)0xBB; |
| 37 |
private static final byte BF = (byte)0xBF; |
| 38 |
private static final byte NUL = (byte)0x00; |
| 39 |
|
| 40 |
private static Charset utf32Le; |
| 41 |
private static Charset utf32Be; |
| 42 |
|
| 43 |
private final byte[] buf = new byte[4]; |
| 44 |
private int bufLen; |
| 45 |
private int curIndex; |
| 46 |
private final Charset charset; |
| 47 |
|
| 48 |
UnicodeDetectingInputStream(InputStream is) { |
| 49 |
super(is); |
| 50 |
charset = detectEncoding(); |
| 51 |
} |
| 52 |
|
| 53 |
Charset getCharset() { |
| 54 |
return charset; |
| 55 |
} |
| 56 |
|
| 57 |
private void fillBuf() { |
| 58 |
int b1; |
| 59 |
int b2; |
| 60 |
int b3; |
| 61 |
int b4; |
| 62 |
|
| 63 |
try { |
| 64 |
b1 = in.read(); |
| 65 |
if (b1 == -1) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
b2 = in.read(); |
| 70 |
if (b2 == -1) { |
| 71 |
bufLen = 1; |
| 72 |
buf[0] = (byte)b1; |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
b3 = in.read(); |
| 77 |
if (b3 == -1) { |
| 78 |
bufLen = 2; |
| 79 |
buf[0] = (byte)b1; |
| 80 |
buf[1] = (byte)b2; |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
b4 = in.read(); |
| 85 |
if (b4 == -1) { |
| 86 |
bufLen = 3; |
| 87 |
buf[0] = (byte)b1; |
| 88 |
buf[1] = (byte)b2; |
| 89 |
buf[2] = (byte)b3; |
| 90 |
return; |
| 91 |
} |
| 92 |
bufLen = 4; |
| 93 |
buf[0] = (byte)b1; |
| 94 |
buf[1] = (byte)b2; |
| 95 |
buf[2] = (byte)b3; |
| 96 |
buf[3] = (byte)b4; |
| 97 |
} catch (IOException ioe) { |
| 98 |
throw new JsonException(JsonMessages.PARSER_INPUT_ENC_DETECT_IOERR(), ioe); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private Charset detectEncoding() { |
| 103 |
fillBuf(); |
| 104 |
if (bufLen < 2) { |
| 105 |
throw new JsonException(JsonMessages.PARSER_INPUT_ENC_DETECT_FAILED()); |
| 106 |
} else if (bufLen == 4) { |
| 107 |
|
| 108 |
if (buf[0] == NUL && buf[1] == NUL && buf[2] == FE && buf[3] == FF) { |
| 109 |
curIndex = 4; |
| 110 |
return getUtf32be(); |
| 111 |
} else if (buf[0] == FF && buf[1] == FE && buf[2] == NUL && buf[3] == NUL) { |
| 112 |
curIndex = 4; |
| 113 |
return getUtf32le(); |
| 114 |
} else if (buf[0] == FE && buf[1] == FF) { |
| 115 |
curIndex = 2; |
| 116 |
return StandardCharsets.UTF_16BE; |
| 117 |
} else if (buf[0] == FF && buf[1] == FE) { |
| 118 |
curIndex = 2; |
| 119 |
return StandardCharsets.UTF_16LE; |
| 120 |
} else if (buf[0] == EF && buf[1] == BB && buf[2] == BF) { |
| 121 |
curIndex = 3; |
| 122 |
return StandardCharsets.UTF_8; |
| 123 |
} |
| 124 |
|
| 125 |
if (buf[0] == NUL && buf[1] == NUL && buf[2] == NUL) { |
| 126 |
return getUtf32be(); |
| 127 |
} else if (buf[0] == NUL && buf[2] == NUL) { |
| 128 |
return StandardCharsets.UTF_16BE; |
| 129 |
} else if (buf[1] == NUL && buf[2] == NUL && buf[3] == NUL) { |
| 130 |
return getUtf32le(); |
| 131 |
} else if (buf[1] == NUL && buf[3] == NUL) { |
| 132 |
return StandardCharsets.UTF_16LE; |
| 133 |
} |
| 134 |
} |
| 135 |
return StandardCharsets.UTF_8; |
| 136 |
} |
| 137 |
|
| 138 |
private static Charset getUtf32be() { |
| 139 |
if (utf32Be == null) { |
| 140 |
utf32Be = Charset.forName("UTF-32BE"); |
| 141 |
} |
| 142 |
return utf32Be; |
| 143 |
} |
| 144 |
|
| 145 |
private static Charset getUtf32le() { |
| 146 |
if (utf32Le == null) { |
| 147 |
utf32Le = Charset.forName("UTF-32LE"); |
| 148 |
} |
| 149 |
return utf32Le; |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public int read() throws IOException { |
| 154 |
if (curIndex < bufLen) { |
| 155 |
return buf[curIndex++]; |
| 156 |
} |
| 157 |
return in.read(); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public int read(byte b[], int off, int len) throws IOException { |
| 162 |
if (curIndex < bufLen) { |
| 163 |
if (len == 0) { |
| 164 |
return 0; |
| 165 |
} |
| 166 |
if (off < 0 || len < 0 || len > b.length -off) { |
| 167 |
throw new IndexOutOfBoundsException(); |
| 168 |
} |
| 169 |
int min = Math.min(bufLen-curIndex, len); |
| 170 |
System.arraycopy(buf, curIndex, b, off, min); |
| 171 |
curIndex += min; |
| 172 |
return min; |
| 173 |
} |
| 174 |
return in.read(b, off, len); |
| 175 |
} |
| 176 |
|
| 177 |
} |