Search in sources :

Example 1 with IonBufferConfiguration

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

the class IonReaderLookaheadBufferTest method oversizedIncompleteGzipValueDoesNotThrow.

@Test
public void oversizedIncompleteGzipValueDoesNotThrow() throws Exception {
    // Tests that InputStream implementations that throw EOFException when too many bytes are requested to be
    // skipped do not cause the lookahead wrapper to throw.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputStream gzip = new GZIPOutputStream(out);
    gzip.write(bytes(0xE0, 0x01, 0x00, 0xEA));
    // timestamp 0001-01-01T00:00Z
    gzip.write(bytes(0x66, 0x80, 0x81, 0x81, 0x81, 0x80, 0x80));
    gzip.close();
    byte[] bytes = out.toByteArray();
    // Less than the length of the timestamp value.
    final int maximumSize = 5;
    // Cutting off 15 bytes removes the entire GZIP trailer and part of the value.
    byte[] truncatedBytes = new byte[bytes.length - 15];
    System.arraycopy(bytes, 0, truncatedBytes, 0, truncatedBytes.length);
    final RecordingEventHandler eventHandler = new RecordingEventHandler();
    builderSupplier = new BuilderSupplier<IonBufferConfiguration, IonBufferConfiguration.Builder>() {

        @Override
        public IonBufferConfiguration.Builder get() {
            return IonBufferConfiguration.Builder.standard().withMaximumBufferSize(maximumSize).withInitialBufferSize(maximumSize).onOversizedValue(eventHandler).onOversizedSymbolTable(eventHandler).onData(eventHandler);
        }
    };
    InputStream input = new GZIPInputStream(new ByteArrayInputStream(truncatedBytes));
    IonReaderLookaheadBuffer lookahead = new IonReaderLookaheadBuffer(builder.build(), input);
    lookahead.fillInput();
    assertTrue(lookahead.moreDataRequired());
    lookahead.fillInput();
    assertTrue(lookahead.moreDataRequired());
    input.close();
// Note: even though the value would be considered oversize, the event handler may not have been notified
// before the input is closed, so nothing is asserted here. The purpose of this test is to verify that
// an EOFException is not thrown.
}
Also used : IonBufferConfiguration(com.amazon.ion.IonBufferConfiguration) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) IonBinaryWriterBuilder(com.amazon.ion.system.IonBinaryWriterBuilder) IonReaderBuilder(com.amazon.ion.system.IonReaderBuilder) IonSystemBuilder(com.amazon.ion.system.IonSystemBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 2 with IonBufferConfiguration

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

the class IonReaderBuilderTest method testBufferConfiguration.

@Test
public void testBufferConfiguration() {
    IonBufferConfiguration configuration1 = IonBufferConfiguration.Builder.standard().build();
    IonBufferConfiguration configuration2 = IonBufferConfiguration.Builder.standard().build();
    IonReaderBuilder builder = IonReaderBuilder.standard();
    assertNull(builder.getBufferConfiguration());
    builder.withBufferConfiguration(configuration1);
    assertSame(configuration1, builder.getBufferConfiguration());
    builder.setBufferConfiguration(configuration2);
    assertSame(configuration2, builder.getBufferConfiguration());
    builder.withBufferConfiguration(null);
    assertNull(builder.getBufferConfiguration());
}
Also used : IonBufferConfiguration(com.amazon.ion.IonBufferConfiguration) Test(org.junit.Test)

Example 3 with IonBufferConfiguration

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

the class IonReaderLookaheadBufferTest method skipOversizedValues.

@Test
public void skipOversizedValues() throws Exception {
    if (initialBufferSize == null) {
        // of the buffer is unlimited. Skip.
        return;
    }
    // 14 values ranging in size from 1-14 bytes. There are 14 - x values larger than x bytes.
    TestUtils.BinaryIonAppender appender = new TestUtils.BinaryIonAppender().append(// boolean true
    0x11, // int 0 (overpadded)
    0x21, // int 0 (overpadded)
    0x00, // int 1 (overpadded)
    0x22, // int 1 (overpadded)
    0x00, // int 1 (overpadded)
    0x01, // int -2 (overpadded)
    0x33, // int -2 (overpadded)
    0x00, // int -2 (overpadded)
    0x00, // int -2 (overpadded)
    0x02, // float 0e0
    0x44, // float 0e0
    0x00, // float 0e0
    0x00, // float 0e0
    0x00, // float 0e0
    0x00, // decimal 0d0 (overpadded)
    0x55, // decimal 0d0 (overpadded)
    0x00, // decimal 0d0 (overpadded)
    0x00, // decimal 0d0 (overpadded)
    0x80, // decimal 0d0 (overpadded)
    0x00, // decimal 0d0 (overpadded)
    0x00, // timestamp 0001-01-01T00:00Z
    0x66, // timestamp 0001-01-01T00:00Z
    0x80, // timestamp 0001-01-01T00:00Z
    0x81, // timestamp 0001-01-01T00:00Z
    0x81, // timestamp 0001-01-01T00:00Z
    0x81, // timestamp 0001-01-01T00:00Z
    0x80, // timestamp 0001-01-01T00:00Z
    0x80, // symbol 0 (overpadded length and value)
    0x7E, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x82, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // string abcdefgh
    0x88, // string abcdefgh
    'a', // string abcdefgh
    'b', // string abcdefgh
    'c', // string abcdefgh
    'd', // string abcdefgh
    'e', // string abcdefgh
    'f', // string abcdefgh
    'g', // string abcdefgh
    'h', // clob abcdefghi
    0x99, // clob abcdefghi
    'a', // clob abcdefghi
    'b', // clob abcdefghi
    'c', // clob abcdefghi
    'd', // clob abcdefghi
    'e', // clob abcdefghi
    'f', // clob abcdefghi
    'g', // clob abcdefghi
    'h', // clob abcdefghi
    'i', // blob
    0xAA, // blob
    0x01, // blob
    0x02, // blob
    0x03, // blob
    0x04, // blob
    0x05, // blob
    0x06, // blob
    0x07, // blob
    0x08, // blob
    0x09, // blob
    0x0A, // list with ten int 0
    0xBB, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, // sexp with ten decimal 0d0
    0xCC, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, // struct with system symbol field names and int 0 values (some overpadded)
    0xDD, 0x84, 0x20, 0x85, 0x20, 0x86, 0x20, 0x87, 0x21, 0x00, 0x88, 0x22, 0x00, 0x00);
    byte[] data = appender.toByteArray();
    IonDatagram expectedValues = SYSTEM.getLoader().load(data);
    ;
    for (int maxValueSize : Arrays.asList(5, 7, 8, 10, 13)) {
        if (maxValueSize < initialBufferSize) {
            // This would violate the IonBufferConfiguration contract; no need to test here.
            continue;
        }
        ByteArrayInputStream input = new ByteArrayInputStream(data);
        RecordingEventHandler eventHandler = new RecordingEventHandler();
        IonBufferConfiguration.Builder builder = IonBufferConfiguration.Builder.standard().withMaximumBufferSize(maxValueSize).onOversizedValue(eventHandler).onOversizedSymbolTable(eventHandler).onData(eventHandler).withInitialBufferSize(initialBufferSize);
        IonReaderLookaheadBuffer lookahead = new IonReaderLookaheadBuffer(builder.build(), input);
        IonReader reader = null;
        try {
            int valueIndex = 0;
            while (true) {
                lookahead.fillInput();
                if (lookahead.moreDataRequired()) {
                    break;
                }
                if (reader == null) {
                    reader = lookahead.newIonReader(IonReaderBuilder.standard());
                }
                assertNotNull(reader.next());
                assertTrue(Equivalence.ionEquals(expectedValues.get(valueIndex), SYSTEM.newValue(reader)));
                valueIndex++;
            }
            // There is one value per size. For a max size of x, `moreDataRequired` should be false x times.
            assertEquals(valueIndex, maxValueSize);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        int expectedOversizedValues = expectedValues.size() - maxValueSize;
        assertEquals(String.format("Expect %d oversized values for max size %d", expectedOversizedValues, maxValueSize), expectedOversizedValues, eventHandler.oversizedValueCount);
        assertEquals(0, eventHandler.oversizedSymbolTableCount);
        ResizingPipedInputStream buffer = (ResizingPipedInputStream) lookahead.getPipe();
        // The buffer grows in increments of its initial buffer size. Therefore, the final capacity will be at
        // most `initialBufferSize` larger than the lookahead wrapper's configured max value size.
        assertTrue(buffer.capacity() <= maxValueSize + buffer.getInitialBufferSize());
    }
}
Also used : TestUtils(com.amazon.ion.TestUtils) IonBufferConfiguration(com.amazon.ion.IonBufferConfiguration) IonDatagram(com.amazon.ion.IonDatagram) ByteArrayInputStream(java.io.ByteArrayInputStream) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Aggregations

IonBufferConfiguration (com.amazon.ion.IonBufferConfiguration)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IonDatagram (com.amazon.ion.IonDatagram)1 IonReader (com.amazon.ion.IonReader)1 TestUtils (com.amazon.ion.TestUtils)1 IonBinaryWriterBuilder (com.amazon.ion.system.IonBinaryWriterBuilder)1 IonReaderBuilder (com.amazon.ion.system.IonReaderBuilder)1 IonSystemBuilder (com.amazon.ion.system.IonSystemBuilder)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1