Skip to content

Commit 56c2ad0

Browse files
committed
Fix #325
1 parent b6846ee commit 56c2ad0

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

release-notes/CREDITS

+2
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,5 @@ Alex Yursha (AlexYursha@github)
116116
Brad Hess (bdhess@github)
117117
* Contributed #323: Add `JsonParser.ALLOW_TRAILING_COMMA` to work for Arrays and Objects
118118
(2.9.0)
119+
* Reported #325: `DataInput` backed parser should handle `EOFException` at end of doc
120+
(2.9.0)

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ JSON library.
2222
(contributed by Alex Y)
2323
#323: Add `JsonParser.ALLOW_TRAILING_COMMA` to work for Arrays and Objects
2424
(contributed by Brad H)
25+
#325: `DataInput` backed parser should handle `EOFException` at end of doc
26+
(reported by Brad H)
2527

2628
2.8.4 (14-Oct-2016)
2729

src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,12 @@ public JsonToken nextToken() throws IOException
569569
if (_tokenIncomplete) {
570570
_skipString(); // only strings can be partial
571571
}
572-
int i = _skipWS();
572+
int i = _skipWSOrEnd();
573+
if (i < 0) { // end-of-input
574+
// Close/release things like input source, symbol table and recyclable buffers
575+
close();
576+
return (_currToken = null);
577+
}
573578
// clear any data retained so far
574579
_binaryValue = null;
575580
_tokenInputRow = _currInputRow;
@@ -2197,6 +2202,45 @@ private final int _skipWS() throws IOException
21972202
}
21982203
}
21992204

2205+
/**
2206+
* Alternative to {@link #_skipWS} that handles possible {@link EOFException}
2207+
* caused by trying to read past the end of {@link InputData}.
2208+
*
2209+
* @since 2.9
2210+
*/
2211+
private final int _skipWSOrEnd() throws IOException
2212+
{
2213+
int i = _nextByte;
2214+
if (i < 0) {
2215+
try {
2216+
i = _inputData.readUnsignedByte();
2217+
} catch (EOFException e) {
2218+
return -1;
2219+
}
2220+
} else {
2221+
_nextByte = -1;
2222+
}
2223+
while (true) {
2224+
if (i > INT_SPACE) {
2225+
if (i == INT_SLASH || i == INT_HASH) {
2226+
return _skipWSComment(i);
2227+
}
2228+
return i;
2229+
} else {
2230+
// 06-May-2016, tatu: Could verify validity of WS, but for now why bother.
2231+
// ... but line number is useful thingy
2232+
if (i == INT_CR || i == INT_LF) {
2233+
++_currInputRow;
2234+
}
2235+
}
2236+
try {
2237+
i = _inputData.readUnsignedByte();
2238+
} catch (EOFException e) {
2239+
return -1;
2240+
}
2241+
}
2242+
}
2243+
22002244
private final int _skipWSComment(int i) throws IOException
22012245
{
22022246
while (true) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.core.read;
2+
3+
import com.fasterxml.jackson.core.*;
4+
5+
/* Additional testing for {@link java.io.DataInput} specific
6+
* challenges for parsing.
7+
*/
8+
public class DataInputTest
9+
extends com.fasterxml.jackson.core.BaseTest
10+
{
11+
private final JsonFactory JSON_F = new JsonFactory();
12+
13+
public void testEOFAfterArray() throws Exception
14+
{
15+
JsonParser p = createParser(JSON_F, MODE_DATA_INPUT, "[ 1 ] ");
16+
assertToken(JsonToken.START_ARRAY, p.nextToken());
17+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
18+
assertToken(JsonToken.END_ARRAY, p.nextToken());
19+
assertNull(p.nextToken());
20+
p.close();
21+
}
22+
23+
public void testEOFAfterObject() throws Exception
24+
{
25+
JsonParser p = createParser(JSON_F, MODE_DATA_INPUT, "{ \"value\" : true }");
26+
assertToken(JsonToken.START_OBJECT, p.nextToken());
27+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
28+
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
29+
assertToken(JsonToken.END_OBJECT, p.nextToken());
30+
assertNull(p.nextToken());
31+
p.close();
32+
}
33+
34+
public void testEOFAfterScalar() throws Exception
35+
{
36+
JsonParser p = createParser(JSON_F, MODE_DATA_INPUT, "\"foobar\" ");
37+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
38+
assertEquals("foobar", p.getText());
39+
assertNull(p.nextToken());
40+
p.close();
41+
}
42+
}

src/test/java/com/fasterxml/jackson/core/testsupport/MockDataInput.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public boolean readBoolean() throws IOException {
4242
public byte readByte() throws IOException {
4343
int ch = _input.read();
4444
if (ch < 0) {
45-
throw new IOException("End-of-input for readByte()");
45+
throw new EOFException("End-of-input for readByte()");
4646
}
4747
return (byte) ch;
4848
}

0 commit comments

Comments
 (0)