use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class TextWriterTest method testNewLineTypesWithStandardPrinting.
@Test
public void testNewLineTypesWithStandardPrinting() {
for (IonTextWriterBuilder.NewLineType nlt : IonTextWriterBuilder.NewLineType.values()) {
String expected = String.format("\"Foo\"%sBar%<s(1 2 3)%<s[4,5,6]", nlt.getCharSequence());
IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(true).withNewLineType(nlt);
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(expected, sb.toString());
}
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonManagedBinaryWriterTest method testSymbolTableExport.
@Test
public void testSymbolTableExport() throws Exception {
writer.stepIn(IonType.STRUCT);
{
writer.setFieldName("a");
writer.writeInt(1);
writer.setFieldName("d");
writer.writeInt(4);
}
writer.stepOut();
assertValue("{a:1, d:4}");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IonWriter symbolTableWriter = system().newBinaryWriter(bos);
try {
writer.getSymbolTable().writeTo(symbolTableWriter);
} finally {
symbolTableWriter.close();
}
// SymbolTable expected = system().newSharedSymbolTable("version", )
bos.toByteArray();
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method flushBetweenStructs.
@Test
public void flushBetweenStructs() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = _Private_IonManagedBinaryWriterBuilder.create(_Private_IonManagedBinaryWriterBuilder.AllocatorMode.BASIC).withLocalSymbolTableAppendEnabled().newWriter(out);
writeFirstStruct(writer);
writer.flush();
writeSecondStruct(writer);
writer.close();
IonReader reader = newBoundedIncrementalReader(out.toByteArray(), 64);
assertFirstStruct(reader);
assertSecondStruct(reader);
reader.close();
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeValueWithSymbolTableIncremental.
@Test
public void oversizeValueWithSymbolTableIncremental() throws Exception {
// The first value is oversized because it cannot be buffered at the same time as the preceding symbol table
// without exceeding the maximum buffer size. The next value fits.
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
writer.writeString("12345678");
// Requires 32 bytes (4 IVM, 1 TID, 1 length, 26 chars)
writer.writeSymbol("abcdefghijklmnopqrstuvwxyz");
writer.close();
// The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
// for symbol 10.
// The string "12345678" requires 9 bytes, bringing the total to ~49, above the max of 48.
final AtomicInteger oversizedCounter = new AtomicInteger();
final AtomicInteger byteCounter = new AtomicInteger();
UnifiedTestHandler handler = new UnifiedTestHandler() {
@Override
public void onOversizedSymbolTable() {
throw new IllegalStateException("not expected");
}
@Override
public void onOversizedValue() {
oversizedCounter.incrementAndGet();
}
@Override
public void onData(int numberOfBytes) {
byteCounter.addAndGet(numberOfBytes);
}
};
ResizingPipedInputStream pipe = new ResizingPipedInputStream(out.size());
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(48).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
byte[] bytes = out.toByteArray();
boolean foundValue = false;
for (byte b : bytes) {
pipe.receive(b);
IonType type = reader.next();
if (type != null) {
assertFalse(foundValue);
assertEquals(IonType.SYMBOL, type);
assertEquals("abcdefghijklmnopqrstuvwxyz", reader.stringValue());
assertEquals(1, oversizedCounter.get());
foundValue = true;
}
}
assertTrue(foundValue);
assertNull(reader.next());
reader.close();
assertEquals(1, oversizedCounter.get());
assertEquals(out.size(), byteCounter.get());
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method decimals.
@Test
public void decimals() throws Exception {
final List<BigDecimal> decimals = Arrays.asList(Decimal.ZERO, Decimal.NEGATIVE_ZERO, Decimal.valueOf("1.23e4"), Decimal.valueOf("1.23e-4"), Decimal.valueOf("-1.23e4"), Decimal.valueOf("-1.23e-4"), Decimal.valueOf(Long.MAX_VALUE).add(BigDecimal.ONE), Decimal.valueOf(Long.MIN_VALUE).subtract(BigDecimal.ONE));
IonReaderBinaryIncremental reader = readerFor(new WriterFunction() {
@Override
public void write(IonWriter writer) throws IOException {
for (BigDecimal decimal : decimals) {
writer.writeDecimal(decimal);
}
writer.stepIn(IonType.STRUCT);
writer.setFieldName("foo");
writer.setTypeAnnotations("bar");
writer.writeDecimal(Decimal.valueOf(Long.MAX_VALUE).add(BigDecimal.ONE).scaleByPowerOfTen(-7));
writer.stepOut();
}
});
for (BigDecimal decimal : decimals) {
assertEquals(IonType.DECIMAL, reader.next());
assertEquals(decimal, reader.decimalValue());
}
assertEquals(IonType.STRUCT, reader.next());
reader.stepIn();
assertEquals(IonType.DECIMAL, reader.next());
assertEquals(BigDecimal.valueOf(Long.MAX_VALUE).add(BigDecimal.ONE).scaleByPowerOfTen(-7), reader.bigDecimalValue());
assertNull(reader.next());
reader.stepOut();
assertNull(reader.next());
reader.close();
}
Aggregations