Skip to content

Commit

Permalink
Don't close IonParser on EOF to be compatible with MappingIterator wh…
Browse files Browse the repository at this point in the history
…en source is an empty InputStream (#487)
  • Loading branch information
yvrng committed Apr 26, 2024
1 parent 1d187df commit 2f6f1fd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Expand Up @@ -676,7 +676,6 @@ public JsonToken nextToken() throws IOException
}
if (type == null) {
if (_parsingContext.inRoot()) { // EOF?
close();
_currToken = null;
} else {
_parsingContext = _parsingContext.getParent();
Expand Down
@@ -0,0 +1,50 @@
package com.fasterxml.jackson.dataformat.ion.sequence;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Test;

public class MappingIteratorTest {

private static final ObjectMapper MAPPER = new IonObjectMapper();

@Test
public void testReadFromWrite() throws Exception {
final Object[] values = new String[]{"1", "2", "3", "4"};

// write
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (SequenceWriter seq = MAPPER.writer().writeValues(out)) {
for (Object value : values) {
seq.write(value);
}
}

// read
final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) {
for (Object value : values) {
assertTrue(it.hasNext());
assertTrue(it.hasNext()); // should not alter the iterator state
assertEquals(value, it.next());
}
assertFalse(it.hasNext());
}
}

@Test
public void testReadFromEmpty() throws Exception {
final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) {
assertFalse(it.hasNext());
}
}
}

0 comments on commit 2f6f1fd

Please sign in to comment.