11package com .jsoniter ;
22
33import com .jsoniter .any .Any ;
4+ import com .jsoniter .spi .JsonException ;
45import java .io .IOException ;
56import java .io .InputStream ;
67import junit .framework .TestCase ;
78import org .junit .experimental .categories .Category ;
9+ import sun .reflect .generics .reflectiveObjects .NotImplementedException ;
810
911public class IterImplForStreamingTest extends TestCase {
1012
@@ -18,34 +20,64 @@ public void testReadMaxDouble() throws Exception {
1820
1921 @ Category (StreamingCategory .class )
2022 public void testLoadMore () throws IOException {
21- final String originalContent = "1234 " ;
23+ final String originalContent = "1234567890 " ;
2224 final byte [] src = ("{\" a\" :\" " + originalContent + "\" }" ).getBytes ();
23- InputStream slowStream = new InputStream () {
25+
26+ int initialBufferSize ;
27+ Any parsedString ;
28+ // Case #1: Data fits into initial buffer, autoresizing on
29+ // Input must definitely fit into such large buffer
30+ initialBufferSize = src .length * 2 ;
31+ JsonIterator jsonIterator = JsonIterator .parse (getSluggishInputStream (src ), initialBufferSize , 512 );
32+ jsonIterator .readObject ();
33+ parsedString = jsonIterator .readAny ();
34+ assertEquals (originalContent , parsedString .toString ());
35+ // Check buffer was not expanded
36+ assertEquals (initialBufferSize , jsonIterator .buf .length );
37+
38+ // Case #2: Data does fit into initial buffer, autoresizing off
39+ initialBufferSize = originalContent .length () / 2 ;
40+ jsonIterator = JsonIterator .parse (getSluggishInputStream (src ), initialBufferSize , 0 );
41+ jsonIterator .readObject ();
42+ try {
43+ jsonIterator .readAny ();
44+ fail ("Expect to fail because buffer is too small." );
45+ } catch (JsonException e ) {
46+ if (!e .getMessage ().startsWith ("loadMore" )) {
47+ throw e ;
48+ }
49+ }
50+ // Check buffer was not expanded
51+ assertEquals (initialBufferSize , jsonIterator .buf .length );
52+
53+ // Case #3: Data does fit into initial buffer, autoresizing on
54+ initialBufferSize = originalContent .length () / 2 ;
55+ int autoExpandBufferStep = initialBufferSize * 3 ;
56+ jsonIterator = JsonIterator .parse (getSluggishInputStream (src ), initialBufferSize , autoExpandBufferStep );
57+ jsonIterator .readObject ();
58+ parsedString = jsonIterator .readAny ();
59+ assertEquals (originalContent , parsedString .toString ());
60+ // Check buffer was expanded exactly once
61+ assertEquals (initialBufferSize + autoExpandBufferStep , jsonIterator .buf .length );
62+ }
63+
64+ private static InputStream getSluggishInputStream (final byte [] src ) {
65+ return new InputStream () {
2466 int position = 0 ;
25- boolean pretendEmptyNextRead = false ;
2667
2768 @ Override
2869 public int read () throws IOException {
70+ throw new NotImplementedException ();
71+ }
72+
73+ @ Override
74+ public int read (byte [] b , int off , int len ) throws IOException {
2975 if (position < src .length ) {
30- if (pretendEmptyNextRead ) {
31- pretendEmptyNextRead = false ;
32- return -1 ;
33- } else {
34- pretendEmptyNextRead = true ;
35- return src [position ++];
36- }
76+ b [off ] = src [position ++];
77+ return 1 ;
3778 }
3879 return -1 ;
3980 }
4081 };
41-
42- // Input must definitely fit into such large buffer
43- final int initialBufferSize = src .length * 2 ;
44- JsonIterator jsonIterator = JsonIterator .parse (slowStream , initialBufferSize );
45- jsonIterator .readObject ();
46- Any parsedString = jsonIterator .readAny ();
47- assertEquals (originalContent , parsedString .toString ());
48- // Check buffer was not expanded prematurely
49- assertEquals (initialBufferSize , jsonIterator .buf .length );
5082 }
5183}
0 commit comments