Debian Codegraph @

src/main/org/h2/util/json/JSONByteArrayTarget.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 static org.h2.util.json.JSONStringTarget.ARRAY;
9 import static org.h2.util.json.JSONStringTarget.HEX;
10 import static org.h2.util.json.JSONStringTarget.OBJECT;
11
12 import java.io.ByteArrayOutputStream;
13 import java.math.BigDecimal;
14 import java.nio.charset.StandardCharsets;
15
16 import org.h2.util.ByteStack;
17
18 /**
19 * JSON byte array target.
20 */
21 public final class JSONByteArrayTarget extends JSONTarget<byte[]> {
22
23 private static final byte[] NULL_BYTES = "null".getBytes(StandardCharsets.ISO_8859_1);
24
25 private static final byte[] FALSE_BYTES = "false".getBytes(StandardCharsets.ISO_8859_1);
26
27 private static final byte[] TRUE_BYTES = "true".getBytes(StandardCharsets.ISO_8859_1);
28
29 private static final byte[] U00_BYTES = "\\u00".getBytes(StandardCharsets.ISO_8859_1);
30
31 /**
32 * Encodes a JSON string and appends it to the specified output stream.
33 *
34 * @param baos
35 * the output stream to append to
36 * @param s
37 * the string to encode
38 * @return the specified output stream
39 */
40 public static ByteArrayOutputStream encodeString(ByteArrayOutputStream baos, String s) {
41 baos.write('"');
42 for (int i = 0, length = s.length(); i < length; i++) {
43 char c = s.charAt(i);
44 switch (c) {
45 case '\b':
46 baos.write('\\');
47 baos.write('b');
48 break;
49 case '\t':
50 baos.write('\\');
51 baos.write('t');
52 break;
53 case '\f':
54 baos.write('\\');
55 baos.write('f');
56 break;
57 case '\n':
58 baos.write('\\');
59 baos.write('n');
60 break;
61 case '\r':
62 baos.write('\\');
63 baos.write('r');
64 break;
65 case '"':
66 baos.write('\\');
67 baos.write('"');
68 break;
69 case '\\':
70 baos.write('\\');
71 baos.write('\\');
72 break;
73 default:
74 if (c >= ' ') {
75 if (c < 0x80) {
76 baos.write(c);
77 } else if (c < 0x800) {
78 baos.write(0xc0 | c >> 6);
79 baos.write(0x80 | c & 0x3f);
80 } else if (!Character.isSurrogate(c)) {
81 baos.write(0xe0 | c >> 12);
82 baos.write(0x80 | c >> 6 & 0x3f);
83 baos.write(0x80 | c & 0x3f);
84 } else {
85 char c2;
86 if (!Character.isHighSurrogate(c) || ++i >= length
87 || !Character.isLowSurrogate(c2 = s.charAt(i))) {
88 throw new IllegalArgumentException();
89 }
90 int uc = Character.toCodePoint(c, c2);
91 baos.write(0xf0 | uc >> 18);
92 baos.write(0x80 | uc >> 12 & 0x3f);
93 baos.write(0x80 | uc >> 6 & 0x3f);
94 baos.write(0x80 | uc & 0x3f);
95 }
96 } else {
97 baos.write(U00_BYTES, 0, 4);
98 baos.write(HEX[c >>> 4 & 0xf]);
99 baos.write(HEX[c & 0xf]);
100 }
101 }
102 }
103 baos.write('"');
104 return baos;
105 }
106
107 private final ByteArrayOutputStream baos;
108
109 private final ByteStack stack;
110
111 private boolean needSeparator;
112
113 private boolean afterName;
114
115 /**
116 * Creates new instance of JSON byte array target.
117 */
118 public JSONByteArrayTarget() {
119 baos = new ByteArrayOutputStream();
120 stack = new ByteStack();
121 }
122
123 @Override
124 public void startObject() {
125 beforeValue();
126 afterName = false;
127 stack.push(OBJECT);
128 baos.write('{');
129 }
130
131 @Override
132 public void endObject() {
133 if (afterName || stack.poll(-1) != OBJECT) {
134 throw new IllegalStateException();
135 }
136 baos.write('}');
137 afterValue();
138 }
139
140 @Override
141 public void startArray() {
142 beforeValue();
143 afterName = false;
144 stack.push(ARRAY);
145 baos.write('[');
146 }
147
148 @Override
149 public void endArray() {
150 if (stack.poll(-1) != ARRAY) {
151 throw new IllegalStateException();
152 }
153 baos.write(']');
154 afterValue();
155 }
156
157 @Override
158 public void member(String name) {
159 if (afterName || stack.peek(-1) != OBJECT) {
160 throw new IllegalStateException();
161 }
162 afterName = true;
163 beforeValue();
164 encodeString(baos, name).write(':');
165 }
166
167 @Override
168 public void valueNull() {
169 beforeValue();
170 baos.write(NULL_BYTES, 0, 4);
171 afterValue();
172 }
173
174 @Override
175 public void valueFalse() {
176 beforeValue();
177 baos.write(FALSE_BYTES, 0, 5);
178 afterValue();
179 }
180
181 @Override
182 public void valueTrue() {
183 beforeValue();
184 baos.write(TRUE_BYTES, 0, 4);
185 afterValue();
186 }
187
188 @Override
189 public void valueNumber(BigDecimal number) {
190 beforeValue();
191 String s = number.toString();
192 int index = s.indexOf('E');
193 byte[] b = s.getBytes(StandardCharsets.ISO_8859_1);
194 if (index >= 0 && s.charAt(++index) == '+') {
195 baos.write(b, 0, index);
196 baos.write(b, index + 1, b.length - index - 1);
197 } else {
198 baos.write(b, 0, b.length);
199 }
200 afterValue();
201 }
202
203 @Override
204 public void valueString(String string) {
205 beforeValue();
206 encodeString(baos, string);
207 afterValue();
208 }
209
210 private void beforeValue() {
211 if (!afterName && stack.peek(-1) == OBJECT) {
212 throw new IllegalStateException();
213 }
214 if (needSeparator) {
215 if (stack.isEmpty()) {
216 throw new IllegalStateException();
217 }
218 needSeparator = false;
219 baos.write(',');
220 }
221 }
222
223 private void afterValue() {
224 needSeparator = true;
225 afterName = false;
226 }
227
228 @Override
229 public boolean isPropertyExpected() {
230 return !afterName && stack.peek(-1) == OBJECT;
231 }
232
233 @Override
234 public boolean isValueSeparatorExpected() {
235 return needSeparator;
236 }
237
238 @Override
239 public byte[] getResult() {
240 if (!stack.isEmpty() || baos.size() == 0) {
241 throw new IllegalStateException();
242 }
243 return baos.toByteArray();
244 }
245
246 }