1
1
package com .jsoniter ;
2
2
3
+ import com .jsoniter .any .Any ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStream ;
3
6
import junit .framework .TestCase ;
7
+ import org .junit .experimental .categories .Category ;
4
8
5
9
public class IterImplForStreamingTest extends TestCase {
6
10
@@ -11,4 +15,37 @@ public void testReadMaxDouble() throws Exception {
11
15
String number = new String (numberChars .chars , 0 , numberChars .charsLength );
12
16
assertEquals (maxDouble , number );
13
17
}
14
- }
18
+
19
+ @ Category (StreamingCategory .class )
20
+ public void testLoadMore () throws IOException {
21
+ final String originalContent = "1234" ;
22
+ final byte [] src = ("{\" a\" :\" " + originalContent + "\" }" ).getBytes ();
23
+ InputStream slowStream = new InputStream () {
24
+ int position = 0 ;
25
+ boolean pretendEmptyNextRead = false ;
26
+
27
+ @ Override
28
+ public int read () throws IOException {
29
+ if (position < src .length ) {
30
+ if (pretendEmptyNextRead ) {
31
+ pretendEmptyNextRead = false ;
32
+ return -1 ;
33
+ } else {
34
+ pretendEmptyNextRead = true ;
35
+ return src [position ++];
36
+ }
37
+ }
38
+ return -1 ;
39
+ }
40
+ };
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 );
50
+ }
51
+ }
0 commit comments