Skip to content

Commit 9f9df25

Browse files
yvrngtatu-at-datastax
authored andcommitted
Don't close IonParser on EOF to be compatible with MappingIterator when source is an empty InputStream (#487)
1 parent d90dd25 commit 9f9df25

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,6 @@ public JsonToken nextToken() throws IOException
676676
}
677677
if (type == null) {
678678
if (_parsingContext.inRoot()) { // EOF?
679-
close();
680679
_currToken = null;
681680
} else {
682681
_parsingContext = _parsingContext.getParent();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fasterxml.jackson.dataformat.ion.sequence;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import com.fasterxml.jackson.databind.MappingIterator;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.SequenceWriter;
10+
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
11+
import java.io.ByteArrayInputStream;
12+
import java.io.ByteArrayOutputStream;
13+
import org.junit.Test;
14+
15+
public class MappingIteratorTest {
16+
17+
private static final ObjectMapper MAPPER = new IonObjectMapper();
18+
19+
@Test
20+
public void testReadFromWrite() throws Exception {
21+
final Object[] values = new String[]{"1", "2", "3", "4"};
22+
23+
// write
24+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
25+
try (SequenceWriter seq = MAPPER.writer().writeValues(out)) {
26+
for (Object value : values) {
27+
seq.write(value);
28+
}
29+
}
30+
31+
// read
32+
final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
33+
try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) {
34+
for (Object value : values) {
35+
assertTrue(it.hasNext());
36+
assertTrue(it.hasNext()); // should not alter the iterator state
37+
assertEquals(value, it.next());
38+
}
39+
assertFalse(it.hasNext());
40+
}
41+
}
42+
43+
@Test
44+
public void testReadFromEmpty() throws Exception {
45+
final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
46+
try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) {
47+
assertFalse(it.hasNext());
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)