Search in sources :

Example 6 with IonReaderBuilder

use of com.amazon.ion.system.IonReaderBuilder 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 7 with IonReaderBuilder

use of com.amazon.ion.system.IonReaderBuilder 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 8 with IonReaderBuilder

use of com.amazon.ion.system.IonReaderBuilder 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 9 with IonReaderBuilder

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

the class IonReaderBinaryIncrementalTest method oversizeValueDetectedDuringReadOfTypeIdOfSymbolTableAnnotatedValueIncremental.

@Test
public void oversizeValueDetectedDuringReadOfTypeIdOfSymbolTableAnnotatedValueIncremental() throws Exception {
    // This value is not a symbol table, but follows most of the code path that symbol tables follow. Ensure that
    // `onOversizedValue` (NOT `onOversizedSymbolTable`) is called, and that the stream continues to be read.
    byte[] bytes = toBinary("$ion_symbol_table::123 \"a\"");
    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(bytes.length);
    IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(7).withMaximumBufferSize(7).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
    // The maximum buffer size is 7, which will be exceeded after the IVM (4 bytes), the annotation wrapper type ID
    // (1 byte), the annotations length (1 byte), and the annotation SID 3 (1 byte). The next byte is the wrapped
    // value type ID byte.
    boolean foundValue = false;
    for (byte b : bytes) {
        pipe.receive(b);
        IonType type = reader.next();
        if (type != null) {
            assertFalse(foundValue);
            assertEquals(IonType.STRING, type);
            assertEquals("a", reader.stringValue());
            assertEquals(1, oversizedCounter.get());
            foundValue = true;
        }
    }
    assertTrue(foundValue);
    assertEquals(1, oversizedCounter.get());
    assertNull(reader.next());
    reader.close();
    assertEquals(bytes.length, byteCounter.get());
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) IonType(com.amazon.ion.IonType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 10 with IonReaderBuilder

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

the class IonReaderBinaryIncrementalTest method newBoundedIncrementalReader.

IonReader newBoundedIncrementalReader(byte[] bytes, int maximumBufferSize) {
    UnifiedTestHandler handler = new UnifiedTestHandler() {

        @Override
        public void onOversizedSymbolTable() {
            Assert.fail("Oversized symbol table not expected.");
        }

        @Override
        public void onOversizedValue() {
            Assert.fail("Oversized value not expected.");
        }

        @Override
        public void onData(int numberOfBytes) {
        // Do nothing.
        }
    };
    IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(maximumBufferSize).withMaximumBufferSize(maximumBufferSize).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    return new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(bytes));
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

IonReaderBuilder (com.amazon.ion.system.IonReaderBuilder)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Test (org.junit.Test)7 IonType (com.amazon.ion.IonType)4 IonWriter (com.amazon.ion.IonWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4