Search in sources :

Example 1 with IonReaderBuilder

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

the class IonReaderBinaryIncrementalTest method oversizeValueDetectedDuringSingleByteReadIncremental.

@Test
public void oversizeValueDetectedDuringSingleByteReadIncremental() throws Exception {
    // Requires a 2-byte header.
    byte[] bytes = toBinary("\"abcdefghijklmnopqrstuvwxyz\"");
    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(5).withMaximumBufferSize(5).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
    for (byte b : bytes) {
        assertNull(reader.next());
        pipe.receive(b);
    }
    // The maximum buffer size is 5, which will be exceeded after the IVM (4 bytes), the type ID (1 byte), and
    // the length byte (1 byte).
    assertNull(reader.next());
    reader.close();
    assertEquals(1, oversizedCounter.get());
    assertEquals(bytes.length, byteCounter.get());
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with IonReaderBuilder

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

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

the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInHeaderIncremental.

private void oversizeSymbolTableDetectedInHeaderIncremental(int maximumBufferSize) throws Exception {
    // The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
    // for symbol 10.
    byte[] bytes = toBinary("abcdefghijklmnopqrstuvwxyz");
    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(bytes.length);
    IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(maximumBufferSize).withMaximumBufferSize(maximumBufferSize).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
    for (byte b : bytes) {
        assertNull(reader.next());
        pipe.receive(b);
    }
    assertNull(reader.next());
    assertEquals(1, oversizedCounter.get());
    reader.close();
}
Also used : IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 4 with IonReaderBuilder

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

the class IonReaderBinaryIncrementalTest method oversizeValueDetectedDuringMultiByteReadIncremental.

@Test
public void oversizeValueDetectedDuringMultiByteReadIncremental() throws Exception {
    byte[] bytes = toBinary(// Requires 32 bytes (4 IVM, 1 TID, 1 length, 26 chars)
    "\"abcdefghijklmnopqrstuvwxyz\" " + "\"abc\" " + "\"abcdefghijklmnopqrstuvwxyz\" " + "\"def\"");
    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(8).withMaximumBufferSize(16).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
    int valueCounter = 0;
    for (byte b : bytes) {
        pipe.receive(b);
        IonType type = reader.next();
        if (type != null) {
            valueCounter++;
            assertTrue(valueCounter < 3);
            if (valueCounter == 1) {
                assertEquals(IonType.STRING, type);
                assertEquals("abc", reader.stringValue());
                assertEquals(1, oversizedCounter.get());
            } else {
                assertEquals(2, valueCounter);
                assertEquals(IonType.STRING, type);
                assertEquals("def", reader.stringValue());
                assertEquals(2, oversizedCounter.get());
            }
        }
    }
    assertEquals(2, valueCounter);
    assertNull(reader.next());
    reader.close();
    assertEquals(2, oversizedCounter.get());
    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 5 with IonReaderBuilder

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

the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInHeader.

private void oversizeSymbolTableDetectedInHeader(int maximumBufferSize) throws Exception {
    // The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
    // for symbol 10.
    byte[] bytes = toBinary("abcdefghijklmnopqrstuvwxyz");
    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(maximumBufferSize).withMaximumBufferSize(maximumBufferSize).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(bytes));
    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)

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