Debian Codegraph @

src/main/org/h2/util/json/JSONBytesSource.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util.json;
7
8 import java.math.BigDecimal;
9 import java.nio.charset.Charset;
10 import java.nio.charset.StandardCharsets;
11
12 /**
13 * JSON byte array source.
14 */
15 public final class JSONBytesSource extends JSONTextSource {
16
17 /**
18 * Parses source bytes to a specified target.
19 *
20 * @param bytes
21 * source
22 * @param target
23 * target
24 * @param <R>
25 * the type of the result
26 * @return the result of the target
27 */
28 public static <R> R parse(byte[] bytes, JSONTarget<R> target) {
29 int length = bytes.length;
30 Charset charset = null;
31 if (length >= 4) {
32 byte b0 = bytes[0];
33 byte b1 = bytes[1];
34 byte b2 = bytes[2];
35 byte b3 = bytes[3];
36 switch (b0) {
37 case -2:
38 if (b1 == -1) {
39 charset = StandardCharsets.UTF_16BE;
40 }
41 break;
42 case -1:
43 if (b1 == -2) {
44 if (b2 == 0 && b3 == 0) {
45 charset = Charset.forName("UTF-32LE");
46 } else {
47 charset = StandardCharsets.UTF_16LE;
48 }
49 }
50 break;
51 case 0:
52 if (b1 != 0) {
53 charset = StandardCharsets.UTF_16BE;
54 } else if (b2 == 0 && b3 != 0 || b2 == -2 && b3 == -1) {
55 charset = Charset.forName("UTF-32BE");
56 }
57 break;
58 default:
59 if (b1 == 0) {
60 if (b2 == 0 && b3 == 0) {
61 charset = Charset.forName("UTF-32LE");
62 } else {
63 charset = StandardCharsets.UTF_16LE;
64 }
65 }
66 break;
67 }
68 } else if (length >= 2) {
69 byte b0 = bytes[0];
70 byte b1 = bytes[1];
71 if (b0 != 0) {
72 if (b1 == 0) {
73 charset = StandardCharsets.UTF_16LE;
74 }
75 } else if (b1 != 0) {
76 charset = StandardCharsets.UTF_16BE;
77 }
78 }
79 (charset == null ? new JSONBytesSource(bytes, target)
80 : new JSONStringSource(new String(bytes, charset), target)).parse();
81 return target.getResult();
82 }
83
84 /**
85 * Converts bytes into normalized JSON representation.
86 *
87 * @param bytes
88 * source representation
89 * @return normalized representation
90 */
91 public static byte[] normalize(byte[] bytes) {
92 return parse(bytes, new JSONByteArrayTarget());
93 }
94
95 private final byte[] bytes;
96
97 private final int length;
98
99 private int index;
100
101 JSONBytesSource(byte[] bytes, JSONTarget<?> target) {
102 super(target);
103 this.bytes = bytes;
104 this.length = bytes.length;
105 // Ignore BOM
106 if (nextChar() != '\uFEFF') {
107 index = 0;
108 }
109 }
110
111 @Override
112 int nextCharAfterWhitespace() {
113 int index = this.index;
114 while (index < length) {
115 byte ch = bytes[index++];
116 switch (ch) {
117 case '\t':
118 case '\n':
119 case '\r':
120 case ' ':
121 break;
122 default:
123 if (ch < 0) {
124 throw new IllegalArgumentException();
125 }
126 this.index = index;
127 return ch;
128 }
129 }
130 return -1;
131 }
132
133 @Override
134 void readKeyword1(String keyword) {
135 int l = keyword.length() - 1;
136 if (index + l > length) {
137 throw new IllegalArgumentException();
138 }
139 for (int i = index, j = 1; j <= l; i++, j++) {
140 if (bytes[i] != keyword.charAt(j)) {
141 throw new IllegalArgumentException();
142 }
143 }
144 index += l;
145 }
146
147 @Override
148 void parseNumber(boolean positive) {
149 int index = this.index;
150 int start = index - 1;
151 index = skipInt(index, positive);
152 l: if (index < length) {
153 byte ch = bytes[index];
154 if (ch == '.') {
155 index = skipInt(index + 1, false);
156 if (index >= length) {
157 break l;
158 }
159 ch = bytes[index];
160 }
161 if (ch == 'E' || ch == 'e') {
162 if (++index >= length) {
163 throw new IllegalArgumentException();
164 }
165 ch = bytes[index];
166 if (ch == '+' || ch == '-') {
167 index++;
168 }
169 index = skipInt(index, false);
170 }
171 }
172 target.valueNumber(new BigDecimal(new String(bytes, start, index - start, StandardCharsets.ISO_8859_1)));
173 this.index = index;
174 }
175
176 private int skipInt(int index, boolean hasInt) {
177 while (index < length) {
178 byte ch = bytes[index];
179 if (ch >= '0' && ch <= '9') {
180 hasInt = true;
181 index++;
182 } else {
183 break;
184 }
185 }
186 if (!hasInt) {
187 throw new IllegalArgumentException();
188 }
189 return index;
190 }
191
192 @Override
193 int nextChar() {
194 if (index >= length) {
195 throw new IllegalArgumentException();
196 }
197 int b1 = bytes[index++] & 0xff;
198 if (b1 >= 0x80) {
199 if (b1 >= 0xe0) {
200 if (b1 >= 0xf0) {
201 if (index + 2 >= length) {
202 throw new IllegalArgumentException();
203 }
204 int b2 = bytes[index++] & 0xff;
205 int b3 = bytes[index++] & 0xff;
206 int b4 = bytes[index++] & 0xff;
207 b1 = ((b1 & 0xf) << 18) + ((b2 & 0x3f) << 12) + ((b3 & 0x3f) << 6) + (b4 & 0x3f);
208 if (b1 < 0x10000 || b1 > Character.MAX_CODE_POINT || (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80
209 || (b4 & 0xc0) != 0x80) {
210 throw new IllegalArgumentException();
211 }
212 } else {
213 if (index + 1 >= length) {
214 throw new IllegalArgumentException();
215 }
216 int b2 = bytes[index++] & 0xff;
217 int b3 = bytes[index++] & 0xff;
218 b1 = ((b1 & 0xf) << 12) + ((b2 & 0x3f) << 6) + (b3 & 0x3f);
219 if (b1 < 0x800 || (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80) {
220 throw new IllegalArgumentException();
221 }
222 }
223 } else {
224 if (index >= length) {
225 throw new IllegalArgumentException();
226 }
227 int b2 = bytes[index++] & 0xff;
228 b1 = ((b1 & 0x1f) << 6) + (b2 & 0x3f);
229 if (b1 < 0x80 || (b2 & 0xc0) != 0x80) {
230 throw new IllegalArgumentException();
231 }
232 }
233 }
234 return b1;
235 }
236
237 @Override
238 char readHex() {
239 if (index + 3 >= length) {
240 throw new IllegalArgumentException();
241 }
242 int ch;
243 try {
244 ch = Integer.parseInt(new String(bytes, index, 4, StandardCharsets.ISO_8859_1), 16);
245 } catch (NumberFormatException e) {
246 throw new IllegalArgumentException();
247 }
248 index += 4;
249 return (char) ch;
250 }
251
252 @Override
253 public String toString() {
254 return new String(bytes, 0, index, StandardCharsets.UTF_8) + "[*]"
255 + new String(bytes, index, length, StandardCharsets.UTF_8);
256 }
257
258 }