use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class PrintApp method process.
protected void process(IonReader reader, OutputStream out) throws IOException, IonException {
IonSystem system = this.mySystem;
IonWriter writer = system.newTextWriter(out);
writer.writeValues(reader);
// Ensure there's a newline at the end and flush the buffer.
out.write('\n');
out.flush();
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class Event method writeOutput.
/**
* write Event structure to Event Stream and return the updated eventIndex.
*/
public int writeOutput(IonWriter ionWriterForOutput, int eventIndex) throws IOException {
ionWriterForOutput.stepIn(IonType.STRUCT);
if (this.eventType != null) {
ionWriterForOutput.setFieldName("event_type");
ionWriterForOutput.writeSymbol(this.eventType.toString());
}
if (this.ionType != null) {
ionWriterForOutput.setFieldName("ion_type");
ionWriterForOutput.writeSymbol(this.ionType.toString());
}
if (this.fieldName != null) {
ionWriterForOutput.setFieldName("field_name");
ionWriterForOutput.stepIn(IonType.STRUCT);
ionWriterForOutput.setFieldName("text");
if (this.fieldName.getText() == null) {
ionWriterForOutput.writeNull();
} else {
ionWriterForOutput.writeString(this.fieldName.getText());
}
ionWriterForOutput.setFieldName("import_location");
ionWriterForOutput.writeNull();
ionWriterForOutput.stepOut();
}
if (this.annotations != null && this.annotations.length > 0) {
ionWriterForOutput.setFieldName("annotations");
ionWriterForOutput.stepIn(IonType.LIST);
for (SymbolToken annotation : this.annotations) {
ionWriterForOutput.stepIn(IonType.STRUCT);
ionWriterForOutput.setFieldName("text");
String text = annotation.getText();
if (text == null) {
ionWriterForOutput.writeNull();
ionWriterForOutput.setFieldName("import_location");
ionWriterForOutput.writeNull();
} else {
ionWriterForOutput.writeString(text);
}
ionWriterForOutput.stepOut();
}
ionWriterForOutput.stepOut();
}
if (this.value != null) {
String valueText;
byte[] valueBinary;
try (ByteArrayOutputStream textOut = new ByteArrayOutputStream();
IonWriter textWriter = IonTextWriterBuilder.standard().build(textOut);
ByteArrayOutputStream binaryOut = new ByteArrayOutputStream();
IonWriter binaryWriter = IonBinaryWriterBuilder.standard().build(binaryOut)) {
// write Text
this.value.writeTo(textWriter);
textWriter.finish();
valueText = textOut.toString("utf-8");
ionWriterForOutput.setFieldName("value_text");
ionWriterForOutput.writeString(valueText);
// write binary
this.value.writeTo(binaryWriter);
binaryWriter.finish();
valueBinary = binaryOut.toByteArray();
ionWriterForOutput.setFieldName("value_binary");
ionWriterForOutput.stepIn(IonType.LIST);
for (byte b : valueBinary) {
ionWriterForOutput.writeInt(b & 0xff);
}
ionWriterForOutput.stepOut();
}
}
if (this.imports != null && this.imports.length > 0) {
ionWriterForOutput.setFieldName("imports");
ionWriterForOutput.stepIn(IonType.LIST);
for (ImportDescriptor anImport : this.imports) {
ionWriterForOutput.stepIn(IonType.STRUCT);
ionWriterForOutput.setFieldName("name");
ionWriterForOutput.writeString(anImport.getImportName());
ionWriterForOutput.setFieldName("version");
ionWriterForOutput.writeInt(anImport.getVersion());
ionWriterForOutput.setFieldName("max_id");
ionWriterForOutput.writeInt(anImport.getMaxId());
ionWriterForOutput.stepOut();
}
ionWriterForOutput.stepOut();
}
if (this.depth != -1) {
ionWriterForOutput.setFieldName("depth");
ionWriterForOutput.writeInt(this.depth);
}
ionWriterForOutput.stepOut();
// event index + 1 if we write OutputStream successfully.
return eventIndex + 1;
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class SymbolTableTest method testMutateDomAfterLocalSymbolTableAppend.
@Test
public void testMutateDomAfterLocalSymbolTableAppend() throws IOException {
String text = LocalSymbolTablePrefix + "{" + " imports:[{name:\"foo\", version:1, max_id:1}], " + " symbols:[\"s1\", \"s2\"]" + "}\n" + // Symbol with unknown text from "foo"
"$10\n" + // s1
"$11\n" + LocalSymbolTablePrefix + "{" + " imports:" + ION_SYMBOL_TABLE + "," + " symbols:[\"s3\"]" + "}\n" + // s2
"$12";
IonDatagram datagram = loader().load(text);
datagram.add(1, system().newSymbol("abc"));
// s3
datagram.add(system().newSymbol(new SymbolTokenImpl(null, 13)));
// Symbol with unknown text from "foo"
datagram.add(system().newSymbol(new SymbolTokenImpl(null, 10)));
ByteArrayOutputStream textOutput = new ByteArrayOutputStream();
ByteArrayOutputStream binaryOutput = new ByteArrayOutputStream();
List<String> fooSymbols = Collections.singletonList("bar");
SymbolTable fooTable = system().newSharedSymbolTable("foo", 1, fooSymbols.iterator());
IonWriter textWriter = IonTextWriterBuilder.standard().withImports(fooTable).build(textOutput);
datagram.writeTo(textWriter);
textWriter.close();
IonWriter binaryWriter = IonBinaryWriterBuilder.standard().withImports(fooTable).build(binaryOutput);
datagram.writeTo(binaryWriter);
binaryWriter.close();
SimpleCatalog catalog = new SimpleCatalog();
catalog.putTable(fooTable);
IonSystem system = IonSystemBuilder.standard().withCatalog(catalog).build();
IonDatagram textRoundtrip = system.getLoader().load(textOutput.toByteArray());
IonDatagram binaryRoundtrip = system.getLoader().load(binaryOutput.toByteArray());
assertEquals(textRoundtrip, binaryRoundtrip);
assertEquals("bar", ((IonSymbol) textRoundtrip.get(0)).stringValue());
assertEquals("abc", ((IonSymbol) textRoundtrip.get(1)).stringValue());
assertEquals("s1", ((IonSymbol) textRoundtrip.get(2)).stringValue());
assertEquals("s2", ((IonSymbol) textRoundtrip.get(3)).stringValue());
assertEquals("s3", ((IonSymbol) textRoundtrip.get(4)).stringValue());
assertEquals("bar", ((IonSymbol) textRoundtrip.get(0)).stringValue());
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class TextWriterTest method testWritingTopLevelValuesOnNewLinesShouldHaveNoEffectWithPrettyPrint.
@Test
public void testWritingTopLevelValuesOnNewLinesShouldHaveNoEffectWithPrettyPrint() {
// Setting top-level newlines to false should have no effect when pretty printing.
IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(false).withPrettyPrinting().withNewLineType(IonTextWriterBuilder.NewLineType.LF);
IonDatagram dg = system().newDatagram();
dg.add().newString("Foo");
dg.add().newSymbol("Bar");
dg.add().newSexp(new int[] { 1, 2, 3 });
dg.add().newList(new int[] { 4, 5, 6 });
StringBuilder sb = new StringBuilder();
IonWriter writer = writerBuilder.build(sb);
dg.writeTo(writer);
assertEquals("\n\"Foo\"\nBar\n(\n 1\n 2\n 3\n)\n[\n 4,\n 5,\n 6\n]", sb.toString());
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class TextWriterTest method testWritingTopLevelValuesOnNewLinesWithoutPrettyPrint.
@Test
public void testWritingTopLevelValuesOnNewLinesWithoutPrettyPrint() {
IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(true).withNewLineType(IonTextWriterBuilder.NewLineType.LF);
IonDatagram dg = system().newDatagram();
dg.add().newString("Foo");
dg.add().newSymbol("Bar");
dg.add().newSexp(new int[] { 1, 2, 3 });
dg.add().newList(new int[] { 4, 5, 6 });
IonStruct struct = dg.add().newEmptyStruct();
struct.add("def").newInt(42);
struct.addTypeAnnotation("abc");
StringBuilder sb = new StringBuilder();
IonWriter writer = writerBuilder.build(sb);
dg.writeTo(writer);
assertEquals("\"Foo\"\nBar\n(1 2 3)\n[4,5,6]\nabc::{def:42}", sb.toString());
}
Aggregations