Search in sources :

Example 56 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInTheMiddle.

@Test
public void oversizeSymbolTableDetectedInTheMiddle() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
    writer.writeString("12345678");
    writer.finish();
    // 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.
    final AtomicInteger oversizedCounter = new AtomicInteger();
    UnifiedTestHandler handler = new UnifiedTestHandler() {

        @Override
        public void onOversizedSymbolTable() {
            oversizedCounter.incrementAndGet();
        }

        @Override
        public void onOversizedValue() {
            throw new IllegalStateException("not expected");
        }

        @Override
        public void onData(int numberOfBytes) {
        }
    };
    IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(32).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(out.toByteArray()));
    assertEquals(IonType.STRING, reader.next());
    assertEquals("12345678", reader.stringValue());
    assertNull(reader.next());
    assertEquals(1, oversizedCounter.get());
    reader.close();
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 57 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method oversizeValueWithSymbolTable.

@Test
public void oversizeValueWithSymbolTable() 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);
        }
    };
    IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(48).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(out.toByteArray()));
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals(1, oversizedCounter.get());
    assertEquals("abcdefghijklmnopqrstuvwxyz", reader.stringValue());
    assertNull(reader.next());
    reader.close();
    assertEquals(1, oversizedCounter.get());
    assertEquals(out.size(), byteCounter.get());
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 58 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method structsWithFloat32AndPreallocatedLength.

@Test
public void structsWithFloat32AndPreallocatedLength() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = _Private_IonManagedBinaryWriterBuilder.create(_Private_IonManagedBinaryWriterBuilder.AllocatorMode.BASIC).withPaddedLengthPreallocation(2).withFloatBinary32Enabled().newWriter(out);
    writeFirstStruct(writer);
    writeSecondStruct(writer);
    writer.close();
    IonReader reader = newBoundedIncrementalReader(out.toByteArray(), 64);
    assertFirstStruct(reader);
    assertSecondStruct(reader);
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 59 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInTheMiddleIncremental.

@Test
public void oversizeSymbolTableDetectedInTheMiddleIncremental() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
    writer.writeString("12345678");
    writer.finish();
    // 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.
    final AtomicInteger oversizedCounter = new AtomicInteger();
    UnifiedTestHandler handler = new UnifiedTestHandler() {

        @Override
        public void onOversizedSymbolTable() {
            oversizedCounter.incrementAndGet();
        }

        @Override
        public void onOversizedValue() {
            throw new IllegalStateException("not expected");
        }

        @Override
        public void onData(int numberOfBytes) {
        }
    };
    ResizingPipedInputStream pipe = new ResizingPipedInputStream(out.size());
    byte[] bytes = out.toByteArray();
    IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(32).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
    boolean foundValue = false;
    for (byte b : bytes) {
        IonType type = reader.next();
        if (type != null) {
            assertFalse(foundValue);
            assertEquals(IonType.STRING, type);
            assertEquals("12345678", reader.stringValue());
            foundValue = true;
        }
        pipe.receive(b);
    }
    assertTrue(foundValue);
    assertNull(reader.next());
    assertEquals(1, oversizedCounter.get());
    reader.close();
}
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 60 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class BinaryWriterTest method testManyAnnotations.

@Test
public void testManyAnnotations() throws Exception {
    // TT72090065 - >= 14 bytes of annotation causes a length miscalculation
    final IonWriter out = makeWriter();
    out.stepIn(IonType.SEXP);
    {
        for (int i = 0; i < MANY_ANNOTATION_LENGTH; i++) {
            out.addTypeAnnotation(MANY_ANNOTATION_SYMBOL_TEXT);
        }
        out.writeSymbol(MANY_ANNOTATION_SYMBOL_TEXT);
    }
    out.stepOut();
    out.close();
    final IonReader in = reread();
    assertEquals(IonType.SEXP, in.next());
    in.stepIn();
    {
        assertEquals(IonType.SYMBOL, in.next());
        final String[] ann = in.getTypeAnnotations();
        assertEquals(MANY_ANNOTATION_LENGTH, ann.length);
        for (int i = 0; i < MANY_ANNOTATION_LENGTH; i++) {
            assertEquals(MANY_ANNOTATION_SYMBOL_TEXT, ann[i]);
        }
        assertEquals(null, in.next());
    }
    in.stepOut();
    assertEquals(null, in.next());
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) 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