-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathUBJsonWriter.java
More file actions
582 lines (521 loc) · 18.9 KB
/
UBJsonWriter.java
File metadata and controls
582 lines (521 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.utils;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/** Builder style API for emitting UBJSON.
* @author Justin Shapcott */
public class UBJsonWriter implements Closeable {
final DataOutputStream out;
private JsonObject current;
private boolean named;
private final Array<JsonObject> stack = new Array();
public UBJsonWriter (OutputStream out) {
if (!(out instanceof DataOutputStream)) out = new DataOutputStream(out);
this.out = (DataOutputStream)out;
}
/** Begins a new object container. To finish the object call {@link #pop()}.
* @return This writer, for chaining */
public UBJsonWriter object () throws IOException {
if (current != null) {
if (!current.array) {
if (!named) throw new IllegalStateException("Name must be set.");
named = false;
}
}
stack.add(current = new JsonObject(false));
return this;
}
/** Begins a new named object container, having the given name. To finish the object call {@link #pop()}.
* @return This writer, for chaining */
public UBJsonWriter object (String name) throws IOException {
name(name).object();
return this;
}
/** Begins a new array container. To finish the array call {@link #pop()}.
* @return this writer, for chaining. */
public UBJsonWriter array () throws IOException {
if (current != null) {
if (!current.array) {
if (!named) throw new IllegalStateException("Name must be set.");
named = false;
}
}
stack.add(current = new JsonObject(true));
return this;
}
/** Begins a new named array container, having the given name. To finish the array call {@link #pop()}.
* @return this writer, for chaining. */
public UBJsonWriter array (String name) throws IOException {
name(name).array();
return this;
}
/** Appends a name for the next object, array, or value.
* @return this writer, for chaining */
public UBJsonWriter name (String name) throws IOException {
if (current == null || current.array) throw new IllegalStateException("Current item must be an object.");
byte[] bytes = name.getBytes("UTF-8");
if (bytes.length <= Byte.MAX_VALUE) {
out.writeByte('i');
out.writeByte(bytes.length);
} else if (bytes.length <= Short.MAX_VALUE) {
out.writeByte('I');
out.writeShort(bytes.length);
} else {
out.writeByte('l');
out.writeInt(bytes.length);
}
out.write(bytes);
named = true;
return this;
}
/** Appends a {@code byte} value to the stream. This corresponds to the {@code int8} value type in the UBJSON specification.
* @return this writer, for chaining */
public UBJsonWriter value (byte value) throws IOException {
checkName();
out.writeByte('i');
out.writeByte(value);
return this;
}
/** Appends a {@code short} value to the stream. This corresponds to the {@code int16} value type in the UBJSON specification.
* @return this writer, for chaining */
public UBJsonWriter value (short value) throws IOException {
checkName();
out.writeByte('I');
out.writeShort(value);
return this;
}
/** Appends an {@code int} value to the stream. This corresponds to the {@code int32} value type in the UBJSON specification.
* @return this writer, for chaining */
public UBJsonWriter value (int value) throws IOException {
checkName();
out.writeByte('l');
out.writeInt(value);
return this;
}
/** Appends a {@code long} value to the stream. This corresponds to the {@code int64} value type in the UBJSON specification.
* @return this writer, for chaining */
public UBJsonWriter value (long value) throws IOException {
checkName();
out.writeByte('L');
out.writeLong(value);
return this;
}
/** Appends a {@code float} value to the stream. This corresponds to the {@code float32} value type in the UBJSON
* specification.
* @return this writer, for chaining */
public UBJsonWriter value (float value) throws IOException {
checkName();
out.writeByte('d');
out.writeFloat(value);
return this;
}
/** Appends a {@code double} value to the stream. This corresponds to the {@code float64} value type in the UBJSON
* specification.
* @return this writer, for chaining */
public UBJsonWriter value (double value) throws IOException {
checkName();
out.writeByte('D');
out.writeDouble(value);
return this;
}
/** Appends a {@code boolean} value to the stream. This corresponds to the {@code boolean} value type in the UBJSON
* specification.
* @return this writer, for chaining */
public UBJsonWriter value (boolean value) throws IOException {
checkName();
out.writeByte(value ? 'T' : 'F');
return this;
}
/** Appends a {@code char} value to the stream. Because, in Java, a {@code char} is 16 bytes, this corresponds to the
* {@code int16} value type in the UBJSON specification.
* @return this writer, for chaining */
public UBJsonWriter value (char value) throws IOException {
checkName();
out.writeByte('I');
out.writeChar(value);
return this;
}
/** Appends a {@code String} value to the stream. This corresponds to the {@code string} value type in the UBJSON
* specification.
* @return this writer, for chaining */
public UBJsonWriter value (String value) throws IOException {
checkName();
byte[] bytes = value.getBytes("UTF-8");
out.writeByte('S');
if (bytes.length <= Byte.MAX_VALUE) {
out.writeByte('i');
out.writeByte(bytes.length);
} else if (bytes.length <= Short.MAX_VALUE) {
out.writeByte('I');
out.writeShort(bytes.length);
} else {
out.writeByte('l');
out.writeInt(bytes.length);
}
out.write(bytes);
return this;
}
/** Appends an optimized {@code byte array} value to the stream. As an optimized array, the {@code int8} value type marker and
* element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (byte[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('i');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeByte(values[i]);
}
pop(true);
return this;
}
/** Appends an optimized {@code short array} value to the stream. As an optimized array, the {@code int16} value type marker
* and element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (short[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('I');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeShort(values[i]);
}
pop(true);
return this;
}
/** Appends an optimized {@code int array} value to the stream. As an optimized array, the {@code int32} value type marker and
* element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (int[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('l');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeInt(values[i]);
}
pop(true);
return this;
}
/** Appends an optimized {@code long array} value to the stream. As an optimized array, the {@code int64} value type marker and
* element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (long[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('L');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeLong(values[i]);
}
pop(true);
return this;
}
/** Appends an optimized {@code float array} value to the stream. As an optimized array, the {@code float32} value type marker
* and element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (float[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('d');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeFloat(values[i]);
}
pop(true);
return this;
}
/** Appends an optimized {@code double array} value to the stream. As an optimized array, the {@code float64} value type marker
* and element count are encoded once at the array marker instead of repeating the type marker for each element. element count
* are encoded once at the array marker instead of for each element.
* @return this writer, for chaining */
public UBJsonWriter value (double[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('D');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeDouble(values[i]);
}
pop(true);
return this;
}
/** Appends a {@code boolean array} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter value (boolean[] values) throws IOException {
array();
for (int i = 0, n = values.length; i < n; i++) {
out.writeByte(values[i] ? 'T' : 'F');
}
pop();
return this;
}
/** Appends an optimized {@code char array} value to the stream. As an optimized array, the {@code int16} value type marker and
* element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (char[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('C');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
out.writeChar(values[i]);
}
pop(true);
return this;
}
/** Appends an optimized {@code String array} value to the stream. As an optimized array, the {@code String} value type marker
* and element count are encoded once at the array marker instead of repeating the type marker for each element.
* @return this writer, for chaining */
public UBJsonWriter value (String[] values) throws IOException {
array();
out.writeByte('$');
out.writeByte('S');
out.writeByte('#');
value(values.length);
for (int i = 0, n = values.length; i < n; i++) {
byte[] bytes = values[i].getBytes("UTF-8");
if (bytes.length <= Byte.MAX_VALUE) {
out.writeByte('i');
out.writeByte(bytes.length);
} else if (bytes.length <= Short.MAX_VALUE) {
out.writeByte('I');
out.writeShort(bytes.length);
} else {
out.writeByte('l');
out.writeInt(bytes.length);
}
out.write(bytes);
}
pop(true);
return this;
}
/** Appends the given JsonValue, including all its fields recursively, to the stream.
* @return this writer, for chaining */
public UBJsonWriter value (JsonValue value) throws IOException {
if (value.isObject()) {
if (value.name != null)
object(value.name);
else
object();
for (JsonValue child = value.child; child != null; child = child.next)
value(child);
pop();
} else if (value.isArray()) {
if (value.name != null)
array(value.name);
else
array();
for (JsonValue child = value.child; child != null; child = child.next)
value(child);
pop();
} else if (value.isBoolean()) {
if (value.name != null) name(value.name);
value(value.asBoolean());
} else if (value.isDouble()) {
if (value.name != null) name(value.name);
value(value.asDouble());
} else if (value.isLong()) {
if (value.name != null) name(value.name);
value(value.asLong());
} else if (value.isString()) {
if (value.name != null) name(value.name);
value(value.asString());
} else if (value.isNull()) {
if (value.name != null) name(value.name);
value();
} else {
throw new IOException("Unhandled JsonValue type");
}
return this;
}
/** Appends the object to the stream, if it is a known value type. This is a convenience method that calls through to the
* appropriate value method.
* @return this writer, for chaining */
public UBJsonWriter value (Object object) throws IOException {
if (object == null) {
return value();
} else if (object instanceof Number) {
Number number = (Number)object;
if (object instanceof Byte) return value(number.byteValue());
if (object instanceof Short) return value(number.shortValue());
if (object instanceof Integer) return value(number.intValue());
if (object instanceof Long) return value(number.longValue());
if (object instanceof Float) return value(number.floatValue());
if (object instanceof Double) return value(number.doubleValue());
} else if (object instanceof Character) {
return value(((Character)object).charValue());
} else if (object instanceof CharSequence) {
return value(object.toString());
} else
throw new IOException("Unknown object type.");
return this;
}
/** Appends a {@code null} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter value () throws IOException {
checkName();
out.writeByte('Z');
return this;
}
/** Appends a named {@code byte} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, byte value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code short} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, short value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code int} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, int value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code long} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, long value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code float} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, float value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code double} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, double value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code boolean} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, boolean value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code char} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, char value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code String} value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, String value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code byte} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, byte[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code short} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, short[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code int} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, int[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code long} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, long[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code float} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, float[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code double} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, double[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code boolean} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, boolean[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code char} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, char[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code String} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name, String[] value) throws IOException {
return name(name).value(value);
}
/** Appends a named {@code null} array value to the stream.
* @return this writer, for chaining */
public UBJsonWriter set (String name) throws IOException {
return name(name).value();
}
private void checkName () {
if (current != null) {
if (!current.array) {
if (!named) throw new IllegalStateException("Name must be set.");
named = false;
}
}
}
/** Ends the current object or array and pops it off of the element stack.
* @return This writer, for chaining */
public UBJsonWriter pop () throws IOException {
return pop(false);
}
protected UBJsonWriter pop (boolean silent) throws IOException {
if (named) throw new IllegalStateException("Expected an object, array, or value since a name was set.");
if (silent)
stack.pop();
else
stack.pop().close();
current = stack.size == 0 ? null : stack.peek();
return this;
}
/** Flushes the underlying stream. This forces any buffered output bytes to be written out to the stream. */
public void flush () throws IOException {
out.flush();
}
/** Closes the underlying output stream and releases any system resources associated with the stream. */
public void close () throws IOException {
while (stack.size > 0)
pop();
out.close();
}
private class JsonObject {
final boolean array;
JsonObject (boolean array) throws IOException {
this.array = array;
out.writeByte(array ? '[' : '{');
}
void close () throws IOException {
out.writeByte(array ? ']' : '}');
}
}
}