@@ -79,7 +79,7 @@ public void testSimpleWrites() throws Exception
79
79
_testSimpleWrites (true );
80
80
}
81
81
82
- public void _testSimpleWrites (boolean useStream ) throws Exception
82
+ private void _testSimpleWrites (boolean useStream ) throws Exception
83
83
{
84
84
ByteArrayOutputStream out = new ByteArrayOutputStream ();
85
85
StringWriter w = new StringWriter ();
@@ -102,4 +102,102 @@ public void _testSimpleWrites(boolean useStream) throws Exception
102
102
String json = useStream ? out .toString ("UTF-8" ) : w .toString ();
103
103
assertEquals ("123 \" abc\" true" , json );
104
104
}
105
+
106
+ // [core#516]: Off-by-one read problem
107
+ public void testRootOffsetIssue516Bytes () throws Exception
108
+ {
109
+ // InputStream that forces _parseNumber2 to be invoked.
110
+ final InputStream in = new Issue516InputStream (new byte [][] {
111
+ "1234" .getBytes ("UTF-8" ),
112
+ "5 true" .getBytes ("UTF-8" )
113
+ });
114
+
115
+ JsonParser parser = JSON_F .createParser (in );
116
+ assertEquals (12345 , parser .nextIntValue (0 ));
117
+
118
+ // Fails with com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'rue': was expecting ('true', 'false' or 'null')
119
+ assertTrue (parser .nextBooleanValue ());
120
+
121
+ parser .close ();
122
+ in .close ();
123
+ }
124
+
125
+ // [core#516]: Off-by-one read problem
126
+ public void testRootOffsetIssue516Chars () throws Exception
127
+ {
128
+ // InputStream that forces _parseNumber2 to be invoked.
129
+ final Reader in = new Issue516Reader (new char [][] {
130
+ "1234" .toCharArray (), "5 true" .toCharArray ()
131
+ });
132
+
133
+ JsonParser parser = JSON_F .createParser (in );
134
+ assertEquals (12345 , parser .nextIntValue (0 ));
135
+
136
+ // Fails with com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'rue': was expecting ('true', 'false' or 'null')
137
+ assertTrue (parser .nextBooleanValue ());
138
+
139
+ parser .close ();
140
+ in .close ();
141
+ }
142
+
143
+ static class Issue516InputStream extends InputStream
144
+ {
145
+ private final byte [][] reads ;
146
+ private int currentRead ;
147
+
148
+ public Issue516InputStream (byte [][] reads ) {
149
+ this .reads = reads ;
150
+ this .currentRead = 0 ;
151
+ }
152
+
153
+ @ Override
154
+ public int read () throws IOException {
155
+ throw new UnsupportedOperationException ();
156
+ }
157
+
158
+ @ Override
159
+ public int read (byte [] b , int off , int len ) throws IOException {
160
+ if (currentRead >= reads .length ) {
161
+ return -1 ;
162
+ }
163
+ byte [] bytes = reads [currentRead ++];
164
+ if (len < bytes .length ) {
165
+ throw new IllegalArgumentException ();
166
+ }
167
+ System .arraycopy (bytes , 0 , b , off , bytes .length );
168
+ return bytes .length ;
169
+ }
170
+ }
171
+
172
+ static class Issue516Reader extends Reader
173
+ {
174
+ private final char [][] reads ;
175
+ private int currentRead ;
176
+
177
+ public Issue516Reader (char [][] reads ) {
178
+ this .reads = reads ;
179
+ this .currentRead = 0 ;
180
+ }
181
+
182
+ @ Override
183
+ public void close () { }
184
+
185
+ @ Override
186
+ public int read () throws IOException {
187
+ throw new UnsupportedOperationException ();
188
+ }
189
+
190
+ @ Override
191
+ public int read (char [] b , int off , int len ) throws IOException {
192
+ if (currentRead >= reads .length ) {
193
+ return -1 ;
194
+ }
195
+ char [] bytes = reads [currentRead ++];
196
+ if (len < bytes .length ) {
197
+ throw new IllegalArgumentException ();
198
+ }
199
+ System .arraycopy (bytes , 0 , b , off , bytes .length );
200
+ return bytes .length ;
201
+ }
202
+ }
105
203
}
0 commit comments