11package com .jsoniter ;
22
3+ import com .jsoniter .any .Any ;
4+
35import java .io .IOException ;
46
57class IterImplForStreaming {
@@ -227,6 +229,9 @@ final static boolean loadMore(JsonIterator iter) throws IOException {
227229 if (iter .in == null ) {
228230 return false ;
229231 }
232+ if (iter .skipStartedAt != -1 ) {
233+ return keepSkippedBytesThenRead (iter );
234+ }
230235 int n = iter .in .read (iter .buf );
231236 if (n < 1 ) {
232237 if (n == -1 ) {
@@ -241,6 +246,34 @@ final static boolean loadMore(JsonIterator iter) throws IOException {
241246 return true ;
242247 }
243248
249+ private static boolean keepSkippedBytesThenRead (JsonIterator iter ) throws IOException {
250+ int n ;
251+ int offset ;
252+ if (iter .skipStartedAt == 0 || iter .skipStartedAt < iter .tail / 2 ) {
253+ byte [] newBuf = new byte [iter .buf .length * 2 ];
254+ offset = iter .tail - iter .skipStartedAt ;
255+ System .arraycopy (iter .buf , iter .skipStartedAt , newBuf , 0 , offset );
256+ iter .buf = newBuf ;
257+ n = iter .in .read (iter .buf , offset , iter .buf .length - offset );
258+ } else {
259+ offset = iter .tail - iter .skipStartedAt ;
260+ System .arraycopy (iter .buf , iter .skipStartedAt , iter .buf , 0 , offset );
261+ n = iter .in .read (iter .buf , offset , iter .buf .length - offset );
262+ }
263+ iter .skipStartedAt = 0 ;
264+ if (n < 1 ) {
265+ if (n == -1 ) {
266+ return false ;
267+ } else {
268+ throw iter .reportError ("loadMore" , "read from input stream returned " + n );
269+ }
270+ } else {
271+ iter .head = offset ;
272+ iter .tail = offset + n ;
273+ }
274+ return true ;
275+ }
276+
244277 final static byte readByte (JsonIterator iter ) throws IOException {
245278 if (iter .head == iter .tail ) {
246279 if (!loadMore (iter )) {
@@ -249,4 +282,60 @@ final static byte readByte(JsonIterator iter) throws IOException {
249282 }
250283 return iter .buf [iter .head ++];
251284 }
285+
286+ public static Any readAny (JsonIterator iter ) throws IOException {
287+ iter .skipStartedAt = iter .head ;
288+ byte c = IterImpl .nextToken (iter );
289+ switch (c ) {
290+ case '"' :
291+ IterImpl .skipString (iter );
292+ byte [] copied = copySkippedBytes (iter );
293+ return Any .lazyString (copied , 0 , copied .length );
294+ case '-' :
295+ case '0' :
296+ case '1' :
297+ case '2' :
298+ case '3' :
299+ case '4' :
300+ case '5' :
301+ case '6' :
302+ case '7' :
303+ case '8' :
304+ case '9' :
305+ IterImpl .skipUntilBreak (iter );
306+ copied = copySkippedBytes (iter );
307+ return Any .lazyNumber (copied , 0 , copied .length );
308+ case 't' :
309+ IterImpl .skipUntilBreak (iter );
310+ iter .skipStartedAt = -1 ;
311+ return Any .wrap (true );
312+ case 'f' :
313+ IterImpl .skipUntilBreak (iter );
314+ iter .skipStartedAt = -1 ;
315+ return Any .wrap (false );
316+ case 'n' :
317+ IterImpl .skipUntilBreak (iter );
318+ iter .skipStartedAt = -1 ;
319+ return Any .wrap ((Object )null );
320+ case '[' :
321+ IterImpl .skipArray (iter );
322+ copied = copySkippedBytes (iter );
323+ return Any .lazyArray (copied , 0 , copied .length );
324+ case '{' :
325+ IterImpl .skipObject (iter );
326+ copied = copySkippedBytes (iter );
327+ return Any .lazyObject (copied , 0 , copied .length );
328+ default :
329+ throw iter .reportError ("IterImplSkip" , "do not know how to skip: " + c );
330+ }
331+ }
332+
333+ private static byte [] copySkippedBytes (JsonIterator iter ) {
334+ int start = iter .skipStartedAt ;
335+ iter .skipStartedAt = -1 ;
336+ int end = iter .head ;
337+ byte [] bytes = new byte [end - start ];
338+ System .arraycopy (iter .buf , start , bytes , 0 , bytes .length );
339+ return bytes ;
340+ }
252341}
0 commit comments