Search in sources :

Example 11 with IonWriter

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());
    }
}
Also used : IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonDatagram(com.amazon.ion.IonDatagram) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 12 with IonWriter

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();
}
Also used : IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 13 with IonWriter

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();
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 14 with IonWriter

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());
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) IonType(com.amazon.ion.IonType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 15 with IonWriter

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();
}
Also used : IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

IonWriter (com.amazon.ion.IonWriter)71 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)35 IonReader (com.amazon.ion.IonReader)19 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)16 SymbolTable (com.amazon.ion.SymbolTable)12 IonDatagram (com.amazon.ion.IonDatagram)11 IOException (java.io.IOException)11 IonTextWriterBuilder (com.amazon.ion.system.IonTextWriterBuilder)10 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)9 IonSystem (com.amazon.ion.IonSystem)8 OutputStream (java.io.OutputStream)8 IonCatalog (com.amazon.ion.IonCatalog)4 IonReaderBuilder (com.amazon.ion.system.IonReaderBuilder)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IonException (com.amazon.ion.IonException)3 IonStruct (com.amazon.ion.IonStruct)3 StringWriter (java.io.StringWriter)3