Skip to content

Allow IonWriters to be reused. #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ion/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tree model)
<dependency>
<groupId>com.amazon.ion</groupId>
<artifactId>ion-java</artifactId>
<version>1.4.0</version>
<version>1.5.1</version>
</dependency>

<!-- Extends Jackson core, databind -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public IonParser createParser(IonValue value) {
* @since 2.7
*/
public JsonGenerator createGenerator(IonWriter out) {
return _createGenerator(out, _createContext(out, false), out);
return _createGenerator(out, false, _createContext(out, false), out);
}

/**
Expand All @@ -214,7 +214,7 @@ public IonParser createJsonParser(IonValue value) {
*/
@Deprecated
public JsonGenerator createJsonGenerator(IonWriter out) {
return _createGenerator(out, _createContext(out, false), out);
return _createGenerator(out, false, _createContext(out, false), out);
}

/*
Expand Down Expand Up @@ -267,7 +267,7 @@ public JsonGenerator createGenerator(Writer out)
if (createBinaryWriters()) {
throw new IOException("Can only create binary Ion writers that output to OutputStream, not Writer");
}
return _createGenerator(_system.newTextWriter(out), _createContext(out, false), out);
return _createGenerator(_system.newTextWriter(out), true, _createContext(out, false), out);
}

@Override
Expand Down Expand Up @@ -340,11 +340,11 @@ protected IonGenerator _createGenerator(OutputStream out, JsonEncoding enc, bool
ion = _system.newTextWriter(w);
dst = w;
}
return _createGenerator(ion, ctxt, dst);
return _createGenerator(ion, true, ctxt, dst);
}

protected IonGenerator _createGenerator(IonWriter ion, IOContext ctxt, Closeable dst)
protected IonGenerator _createGenerator(IonWriter ion, boolean ionWriterIsManaged, IOContext ctxt, Closeable dst)
{
return new IonGenerator(_generatorFeatures, _objectCodec, ion, ctxt, dst);
return new IonGenerator(_generatorFeatures, _objectCodec, ion, ionWriterIsManaged, ctxt, dst);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class IonGenerator

/* This is the textual or binary writer */
protected final IonWriter _writer;
/* Indicates whether the IonGenerator is responsible for closing the underlying IonWriter. */
protected final boolean _ionWriterIsManaged;

protected final IOContext _ioContext;

Expand All @@ -70,10 +72,11 @@ public class IonGenerator
*/

public IonGenerator(int features, ObjectCodec codec,
IonWriter ion, IOContext ctxt, Closeable dst)
IonWriter ion, boolean ionWriterIsManaged, IOContext ctxt, Closeable dst)
{
super(features, codec);
_writer = ion;
_ionWriterIsManaged = ionWriterIsManaged;
_ioContext = ctxt;
_destination = dst;
}
Expand All @@ -94,9 +97,9 @@ public void close() throws IOException
{
if (!_closed) {
_closed = true;
// force flush the writer
_writer.close();
// either way
if (_ionWriterIsManaged) {
_writer.close();
}
if (_ioContext.isResourceManaged()) {
_destination.close();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,26 @@

package com.fasterxml.jackson.dataformat.ion;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.ion.IonFactory;
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;

import com.amazon.ion.IonDatagram;
import com.amazon.ion.IonList;
import com.amazon.ion.IonReader;
import com.amazon.ion.IonStruct;
import com.amazon.ion.IonSystem;
import com.amazon.ion.IonType;
import com.amazon.ion.IonWriter;
import com.amazon.ion.system.IonBinaryWriterBuilder;
import com.amazon.ion.system.IonReaderBuilder;
import com.amazon.ion.system.IonSystemBuilder;
import com.amazon.ion.system.IonTextWriterBuilder;
import com.amazon.ion.system.IonWriterBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class DataBindWriteTest {

Expand Down Expand Up @@ -132,7 +134,19 @@ public void testIntArrayWriteBinary() throws Exception
IonDatagram loadedDatagram = ion.newLoader().load(data);
assertEquals(expectedArray, loadedDatagram);
}


@Test
public void testReusingBinaryIonWriter() throws Exception
{
_testIonWriterReuse(IonBinaryWriterBuilder.standard());
}

@Test
public void testReusingTextIonWriter() throws Exception
{
_testIonWriterReuse(IonTextWriterBuilder.standard());
}

// // Helper methods

private byte[] _writeAsBytes(Object ob) throws IOException
Expand All @@ -143,4 +157,26 @@ private byte[] _writeAsBytes(Object ob) throws IOException
m.writeValue(out, ob);
return out.toByteArray();
}

private void _testIonWriterReuse(IonWriterBuilder ionWriterBuilder) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
IonWriter ionWriter = ionWriterBuilder.build(byteArrayOutputStream);

IonObjectMapper ionObjectMapper = new IonObjectMapper();
ionObjectMapper.writeValue(ionWriter, "Animal");
ionObjectMapper.writeValue(ionWriter, "Vegetable");
ionObjectMapper.writeValue(ionWriter, "Mineral");
ionWriter.close();

byte[] data = byteArrayOutputStream.toByteArray();
assertNotNull(data);

IonReader ionReader = IonReaderBuilder.standard().build(data);
assertEquals(IonType.STRING, ionReader.next());
assertEquals("Animal", ionReader.stringValue());
assertEquals(IonType.STRING, ionReader.next());
assertEquals("Vegetable", ionReader.stringValue());
assertEquals(IonType.STRING, ionReader.next());
assertEquals("Mineral", ionReader.stringValue());
}
}