-
Notifications
You must be signed in to change notification settings - Fork 529
Expand file tree
/
Copy pathIterImplForStreaming.java
More file actions
659 lines (623 loc) · 24.6 KB
/
IterImplForStreaming.java
File metadata and controls
659 lines (623 loc) · 24.6 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package com.jsoniter;
import com.jsoniter.any.Any;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.Slice;
import java.io.IOException;
class IterImplForStreaming {
public static final int readObjectFieldAsHash(JsonIterator iter) throws IOException {
if (nextToken(iter) != '"') {
throw iter.reportError("readObjectFieldAsHash", "expect \"");
}
long hash = 0x811c9dc5;
for (; ; ) {
byte c = 0;
int i = iter.head;
for (; i < iter.tail; i++) {
c = iter.buf[i];
if (c == '"') {
break;
}
hash ^= c;
hash *= 0x1000193;
}
if (c == '"') {
iter.head = i + 1;
if (nextToken(iter) != ':') {
throw iter.reportError("readObjectFieldAsHash", "expect :");
}
return (int) hash;
}
if (!loadMore(iter)) {
throw iter.reportError("readObjectFieldAsHash", "unmatched quote");
}
}
}
public static final Slice readObjectFieldAsSlice(JsonIterator iter) throws IOException {
Slice field = readSlice(iter);
boolean notCopied = field != null;
if (CodegenAccess.skipWhitespacesWithoutLoadMore(iter)) {
if (notCopied) {
int len = field.tail() - field.head();
byte[] newBuf = new byte[len];
System.arraycopy(field.data(), field.head(), newBuf, 0, len);
field.reset(newBuf, 0, newBuf.length);
}
if (!loadMore(iter)) {
throw iter.reportError("readObjectFieldAsSlice", "expect : after object field");
}
}
if (iter.buf[iter.head] != ':') {
throw iter.reportError("readObjectFieldAsSlice", "expect : after object field");
}
iter.head++;
return field;
}
final static void skipArray(JsonIterator iter) throws IOException {
int level = 1;
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
switch (iter.buf[i]) {
case '"': // If inside string, skip it
iter.head = i + 1;
skipString(iter);
i = iter.head - 1; // it will be i++ soon
break;
case '[': // If open symbol, increase level
level++;
break;
case ']': // If close symbol, decrease level
level--;
// If we have returned to the original level, we're done
if (level == 0) {
iter.head = i + 1;
return;
}
break;
}
}
if (!loadMore(iter)) {
return;
}
}
}
final static void skipObject(JsonIterator iter) throws IOException {
int level = 1;
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
switch (iter.buf[i]) {
case '"': // If inside string, skip it
iter.head = i + 1;
skipString(iter);
i = iter.head - 1; // it will be i++ soon
break;
case '{': // If open symbol, increase level
level++;
break;
case '}': // If close symbol, decrease level
level--;
// If we have returned to the original level, we're done
if (level == 0) {
iter.head = i + 1;
return;
}
break;
}
}
if (!loadMore(iter)) {
return;
}
}
}
final static void skipString(JsonIterator iter) throws IOException {
for (; ; ) {
int end = IterImplSkip.findStringEnd(iter);
if (end == -1) {
int j = iter.tail - 1;
boolean escaped = true;
// can not just look the last byte is \
// because it could be \\ or \\\
for (; ; ) {
// walk backward until head
if (j < iter.head || iter.buf[j] != '\\') {
// even number of backslashes
// either end of buffer, or " found
escaped = false;
break;
}
j--;
if (j < iter.head || iter.buf[j] != '\\') {
// odd number of backslashes
// it is \" or \\\"
break;
}
j--;
}
if (!loadMore(iter)) {
throw iter.reportError("skipString", "incomplete string");
}
if (escaped) {
// TODO add unit test to prove/verify bug
iter.head += 1; // skip the first char as last char is \
}
} else {
iter.head = end;
return;
}
}
}
final static void skipUntilBreak(JsonIterator iter) throws IOException {
// true, false, null, number
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
if (IterImplSkip.breaks[c]) {
iter.head = i;
return;
}
}
if (!loadMore(iter)) {
iter.head = iter.tail;
return;
}
}
}
final static boolean skipNumber(JsonIterator iter) throws IOException {
// true, false, null, number
boolean dotFound = false;
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
if (c == '.' || c == 'e' || c == 'E') {
dotFound = true;
continue;
}
if (IterImplSkip.breaks[c]) {
iter.head = i;
return dotFound;
}
}
if (!loadMore(iter)) {
iter.head = iter.tail;
return dotFound;
}
}
}
// read the bytes between " "
final static Slice readSlice(JsonIterator iter) throws IOException {
if (IterImpl.nextToken(iter) != '"') {
throw iter.reportError("readSlice", "expect \" for string");
}
int end = IterImplString.findSliceEnd(iter);
if (end != -1) {
// reuse current buffer
iter.reusableSlice.reset(iter.buf, iter.head, end - 1);
iter.head = end;
return iter.reusableSlice;
}
// TODO: avoid small memory allocation
byte[] part1 = new byte[iter.tail - iter.head];
System.arraycopy(iter.buf, iter.head, part1, 0, part1.length);
for (; ; ) {
if (!loadMore(iter)) {
throw iter.reportError("readSlice", "unmatched quote");
}
end = IterImplString.findSliceEnd(iter);
if (end == -1) {
byte[] part2 = new byte[part1.length + iter.buf.length];
System.arraycopy(part1, 0, part2, 0, part1.length);
System.arraycopy(iter.buf, 0, part2, part1.length, iter.buf.length);
part1 = part2;
} else {
byte[] part2 = new byte[part1.length + end - 1];
System.arraycopy(part1, 0, part2, 0, part1.length);
System.arraycopy(iter.buf, 0, part2, part1.length, end - 1);
iter.head = end;
iter.reusableSlice.reset(part2, 0, part2.length);
return iter.reusableSlice;
}
}
}
final static byte nextToken(JsonIterator iter) throws IOException {
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
switch (c) {
case ' ':
case '\n':
case '\t':
case '\r':
continue;
default:
iter.head = i + 1;
return c;
}
}
if (!loadMore(iter)) {
return 0;
}
}
}
public final static boolean loadMore(JsonIterator iter) throws IOException {
if (iter.in == null) {
return false;
}
if (iter.skipStartedAt != -1) {
return keepSkippedBytesThenRead(iter);
}
int n = iter.in.read(iter.buf);
if (n < 1) {
if (n == -1) {
return false;
} else {
throw iter.reportError("loadMore", "read from input stream returned " + n);
}
} else {
iter.head = 0;
iter.tail = n;
}
return true;
}
private static boolean keepSkippedBytesThenRead(JsonIterator iter) throws IOException {
int offset = iter.tail - iter.skipStartedAt;
byte[] srcBuffer = iter.buf;
// Check there is no unused buffer capacity
if ((getUnusedBufferByteCount(iter)) == 0) {
// If auto expand buffer enabled, then create larger buffer
if (iter.autoExpandBufferStep > 0) {
iter.buf = new byte[iter.buf.length + iter.autoExpandBufferStep];
} else {
throw iter.reportError("loadMore", String.format("buffer is full and autoexpansion is disabled. tail: [%s] skipStartedAt: [%s]", iter.tail, iter.skipStartedAt));
}
}
System.arraycopy(srcBuffer, iter.skipStartedAt, iter.buf, 0, offset);
int n = iter.in.read(iter.buf, offset, iter.buf.length - offset);
iter.skipStartedAt = 0;
if (n < 1) {
if (n == -1) {
return false;
} else {
throw iter.reportError("loadMore", "read from input stream returned " + n);
}
} else {
iter.head = offset;
iter.tail = offset + n;
}
return true;
}
private static int getUnusedBufferByteCount(JsonIterator iter) {
// Get bytes from 0 to skipStart + from tail till end
return iter.buf.length - iter.tail + iter.skipStartedAt;
}
final static byte readByte(JsonIterator iter) throws IOException {
if (iter.head == iter.tail) {
if (!loadMore(iter)) {
throw iter.reportError("readByte", "no more to read");
}
}
return iter.buf[iter.head++];
}
public static Any readAny(JsonIterator iter) throws IOException {
// TODO: avoid small memory allocation
iter.skipStartedAt = iter.head;
byte c = nextToken(iter);
switch (c) {
case '"':
skipString(iter);
byte[] copied = copySkippedBytes(iter);
return Any.lazyString(copied, 0, copied.length);
case 't':
case 'f':
iter.unreadByte();
return Any.wrap(iter.readBoolean());
case 'n':
iter.unreadByte();
iter.readNull();
return Any.wrap((Object) null);
case '[':
skipArray(iter);
copied = copySkippedBytes(iter);
return Any.lazyArray(copied, 0, copied.length);
case '{':
skipObject(iter);
copied = copySkippedBytes(iter);
return Any.lazyObject(copied, 0, copied.length);
default:
if (skipNumber(iter)) {
copied = copySkippedBytes(iter);
return Any.lazyDouble(copied, 0, copied.length);
} else {
copied = copySkippedBytes(iter);
return Any.lazyLong(copied, 0, copied.length);
}
}
}
private static byte[] copySkippedBytes(JsonIterator iter) {
int start = iter.skipStartedAt;
iter.skipStartedAt = -1;
int end = iter.head;
byte[] bytes = new byte[end - start];
System.arraycopy(iter.buf, start, bytes, 0, bytes.length);
return bytes;
}
public static void skipFixedBytes(JsonIterator iter, int n) throws IOException {
iter.head += n;
if (iter.head >= iter.tail) {
int more = iter.head - iter.tail;
if (!loadMore(iter)) {
if (more == 0) {
iter.head = iter.tail;
return;
}
throw iter.reportError("skipFixedBytes", "unexpected end");
}
iter.head += more;
}
}
public static int updateStringCopyBound(final JsonIterator iter, final int bound) {
if (bound > iter.tail - iter.head) {
return iter.tail - iter.head;
} else {
return bound;
}
}
public final static int readStringSlowPath(JsonIterator iter, int j) throws IOException {
boolean isExpectingLowSurrogate = false;
for (;;) {
int bc = readByte(iter);
if (bc == '"') {
return j;
}
if (bc == '\\') {
bc = readByte(iter);
switch (bc) {
case 'b':
bc = '\b';
break;
case 't':
bc = '\t';
break;
case 'n':
bc = '\n';
break;
case 'f':
bc = '\f';
break;
case 'r':
bc = '\r';
break;
case '"':
case '/':
case '\\':
break;
case 'u':
bc = (IterImplString.translateHex(readByte(iter)) << 12) +
(IterImplString.translateHex(readByte(iter)) << 8) +
(IterImplString.translateHex(readByte(iter)) << 4) +
IterImplString.translateHex(readByte(iter));
if (Character.isHighSurrogate((char) bc)) {
if (isExpectingLowSurrogate) {
throw new JsonException("invalid surrogate");
} else {
isExpectingLowSurrogate = true;
}
} else if (Character.isLowSurrogate((char) bc)) {
if (isExpectingLowSurrogate) {
isExpectingLowSurrogate = false;
} else {
throw new JsonException("invalid surrogate");
}
} else {
if (isExpectingLowSurrogate) {
throw new JsonException("invalid surrogate");
}
}
break;
default:
throw iter.reportError("readStringSlowPath", "invalid escape character: " + bc);
}
} else if ((bc & 0x80) != 0) {
final int u2 = readByte(iter);
if ((bc & 0xE0) == 0xC0) {
bc = ((bc & 0x1F) << 6) + (u2 & 0x3F);
} else {
final int u3 = readByte(iter);
if ((bc & 0xF0) == 0xE0) {
bc = ((bc & 0x0F) << 12) + ((u2 & 0x3F) << 6) + (u3 & 0x3F);
} else {
final int u4 = readByte(iter);
if ((bc & 0xF8) == 0xF0) {
bc = ((bc & 0x07) << 18) + ((u2 & 0x3F) << 12) + ((u3 & 0x3F) << 6) + (u4 & 0x3F);
} else {
throw iter.reportError("readStringSlowPath", "invalid unicode character");
}
if (bc >= 0x10000) {
// check if valid unicode
if (bc >= 0x110000)
throw iter.reportError("readStringSlowPath", "invalid unicode character");
// split surrogates
final int sup = bc - 0x10000;
if (iter.reusableChars.length == j) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
iter.reusableChars[j++] = (char) ((sup >>> 10) + 0xd800);
if (iter.reusableChars.length == j) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
iter.reusableChars[j++] = (char) ((sup & 0x3ff) + 0xdc00);
continue;
}
}
}
}
if (iter.reusableChars.length == j) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
iter.reusableChars[j++] = (char) bc;
}
}
static long readLongSlowPath(final JsonIterator iter, long value) throws IOException {
value = -value; // add negatives to avoid redundant checks for Long.MIN_VALUE on each iteration
long multmin = -922337203685477580L; // limit / 10
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
int ind = IterImplNumber.intDigits[iter.buf[i]];
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
return value;
}
if (value < multmin) {
throw iter.reportError("readLongSlowPath", "value is too large for long");
}
value = (value << 3) + (value << 1) - ind;
if (value >= 0) {
throw iter.reportError("readLongSlowPath", "value is too large for long");
}
}
if (!IterImpl.loadMore(iter)) {
iter.head = iter.tail;
return value;
}
}
}
static int readIntSlowPath(final JsonIterator iter, int value) throws IOException {
value = -value; // add negatives to avoid redundant checks for Integer.MIN_VALUE on each iteration
int multmin = -214748364; // limit / 10
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
int ind = IterImplNumber.intDigits[iter.buf[i]];
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
iter.head = i;
return value;
}
if (value < multmin) {
throw iter.reportError("readIntSlowPath", "value is too large for int");
}
value = (value << 3) + (value << 1) - ind;
if (value >= 0) {
throw iter.reportError("readIntSlowPath", "value is too large for int");
}
}
if (!IterImpl.loadMore(iter)) {
iter.head = iter.tail;
return value;
}
}
}
public static final double readDoubleSlowPath(final JsonIterator iter) throws IOException {
try {
numberChars numberChars = readNumber(iter);
if (numberChars.charsLength == 0 && iter.whatIsNext() == ValueType.STRING) {
String possibleInf = iter.readString();
if ("infinity".equals(possibleInf)) {
return Double.POSITIVE_INFINITY;
}
if ("-infinity".equals(possibleInf)) {
return Double.NEGATIVE_INFINITY;
}
throw iter.reportError("readDoubleSlowPath", "expect number but found string: " + possibleInf);
}
return Double.valueOf(new String(numberChars.chars, 0, numberChars.charsLength));
} catch (NumberFormatException e) {
throw iter.reportError("readDoubleSlowPath", e.toString());
}
}
static class numberChars {
char[] chars;
int charsLength;
boolean dotFound;
}
public static final numberChars readNumber(final JsonIterator iter) throws IOException {
int j = 0;
boolean dotFound = false;
for (; ; ) {
for (int i = iter.head; i < iter.tail; i++) {
if (j == iter.reusableChars.length) {
char[] newBuf = new char[iter.reusableChars.length * 2];
System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
iter.reusableChars = newBuf;
}
byte c = iter.buf[i];
switch (c) {
case '.':
case 'e':
case 'E':
dotFound = true;
// fallthrough
case '-':
case '+':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
iter.reusableChars[j++] = (char) c;
break;
default:
iter.head = i;
numberChars numberChars = new numberChars();
numberChars.chars = iter.reusableChars;
numberChars.charsLength = j;
numberChars.dotFound = dotFound;
return numberChars;
}
}
if (!IterImpl.loadMore(iter)) {
iter.head = iter.tail;
numberChars numberChars = new numberChars();
numberChars.chars = iter.reusableChars;
numberChars.charsLength = j;
numberChars.dotFound = dotFound;
return numberChars;
}
}
}
static final double readDouble(final JsonIterator iter) throws IOException {
return readDoubleSlowPath(iter);
}
static final long readLong(final JsonIterator iter, final byte c) throws IOException {
long ind = IterImplNumber.intDigits[c];
if (ind == 0) {
assertNotLeadingZero(iter);
return 0;
}
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
throw iter.reportError("readLong", "expect 0~9");
}
return IterImplForStreaming.readLongSlowPath(iter, ind);
}
static final int readInt(final JsonIterator iter, final byte c) throws IOException {
int ind = IterImplNumber.intDigits[c];
if (ind == 0) {
assertNotLeadingZero(iter);
return 0;
}
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
throw iter.reportError("readInt", "expect 0~9");
}
return IterImplForStreaming.readIntSlowPath(iter, ind);
}
static void assertNotLeadingZero(JsonIterator iter) throws IOException {
try {
byte nextByte = iter.buf[iter.head];
int ind2 = IterImplNumber.intDigits[nextByte];
if (ind2 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
return;
}
throw iter.reportError("assertNotLeadingZero", "leading zero is invalid");
} catch (ArrayIndexOutOfBoundsException e) {
iter.head = iter.tail;
return;
}
}
}