Search in sources :

Example 1 with RepeatInputStream

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

the class IonReaderBinaryRawLargeStreamTest method readLargeContainer.

@Test
public void readLargeContainer() throws Exception {
    final Timestamp timestamp = Timestamp.forDay(2000, 1, 1);
    byte[] data = testData(timestamp);
    // 32768 makes the value exceed Integer.MAX_VALUE by an arbitrary amount.
    final int totalNumberOfBatches = (Integer.MAX_VALUE / data.length) + 32768;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    header.write(BINARY_VERSION_MARKER_1_0);
    // List with length subfield.
    header.write(0xBE);
    // Length
    IonBinary.writeVarUInt(header, (long) data.length * totalNumberOfBatches);
    InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), // This will provide the data 'totalNumberOfBatches' times
    new RepeatInputStream(data, totalNumberOfBatches - 1));
    IonReader reader = IonReaderBuilder.standard().build(inputStream);
    assertEquals(IonType.LIST, reader.next());
    reader.stepIn();
    int batchesRead = 0;
    while (reader.next() != null) {
        // Materializing on every iteration makes the tests take too long. Do it on every 100.
        boolean materializeValues = (batchesRead % 100) == 0;
        assertEquals(IonType.STRING, reader.getType());
        if (materializeValues) {
            assertEquals("foo", reader.stringValue());
        }
        assertEquals(IonType.DECIMAL, reader.next());
        if (materializeValues) {
            assertEquals(BigDecimal.TEN, reader.decimalValue());
        }
        assertEquals(IonType.TIMESTAMP, reader.next());
        if (materializeValues) {
            assertEquals(timestamp, reader.timestampValue());
        }
        batchesRead++;
    }
    assertNull(reader.next());
    reader.stepOut();
    assertNull(reader.next());
    assertEquals(totalNumberOfBatches, batchesRead);
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Example 2 with RepeatInputStream

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

the class IonReaderBinaryRawLargeStreamTest method skipLargeContainer.

@Test
public void skipLargeContainer() throws Exception {
    final Timestamp timestamp = Timestamp.forDay(2000, 1, 1);
    byte[] data = testData(timestamp);
    // 17 makes the value exceed Integer.MAX_VALUE by an arbitrary amount.
    final int totalNumberOfBatches = (Integer.MAX_VALUE / data.length) + 17;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    header.write(BINARY_VERSION_MARKER_1_0);
    // S-exp with length subfield.
    header.write(0xCE);
    // Length
    IonBinary.writeVarUInt(header, (long) data.length * totalNumberOfBatches);
    InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), new SequenceInputStream(// This will provide the data 'totalNumberOfBatches' times
    new RepeatInputStream(data, totalNumberOfBatches - 1), // The string "bar"
    new ByteArrayInputStream(new byte[] { (byte) 0x83, 'b', 'a', 'r' })));
    IonReader reader = IonReaderBuilder.standard().build(inputStream);
    assertEquals(IonType.SEXP, reader.next());
    assertEquals(IonType.STRING, reader.next());
    assertEquals("bar", reader.stringValue());
    assertNull(reader.next());
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Example 3 with RepeatInputStream

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

the class IonReaderBinaryRawLargeStreamTest method cleanlyFailsOnLargeAnnotatedScalar.

private void cleanlyFailsOnLargeAnnotatedScalar(IonReaderBuilder readerBuilder) throws Exception {
    byte[] data = "foobarbaz".getBytes("UTF-8");
    // 9999 makes the value exceed Integer.MAX_VALUE by an arbitrary amount.
    final int totalNumberOfBatches = (Integer.MAX_VALUE / data.length) + 9999;
    final long stringLength = (long) totalNumberOfBatches * data.length;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    header.write(BINARY_VERSION_MARKER_1_0);
    // Annotation wrapper with length subfield.
    header.write(0xEE);
    IonBinary.writeVarUInt(header, 3 + IonBinary.lenVarUInt(stringLength) + stringLength);
    // One byte of annotations.
    header.write(0x81);
    // Conveniently use SID 4 ("name"), which is in the system symbol table.
    header.write(0x84);
    // String with length subfield.
    header.write(0x8E);
    IonBinary.writeVarUInt(header, stringLength);
    InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), // This will provide the data 'totalNumberOfBatches' times
    new RepeatInputStream(data, totalNumberOfBatches - 1));
    IonReader reader = readerBuilder.build(inputStream);
    // If support for large scalars is added, the following line will be deleted and the rest of the test
    // completed to assert the correctness of the value.
    thrown.expect(IonException.class);
    reader.next();
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with RepeatInputStream

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

the class IonReaderBinaryRawLargeStreamTest method cleanlyFailsOnLargeScalar.

private void cleanlyFailsOnLargeScalar(IonReaderBuilder readerBuilder) throws Exception {
    byte[] data = "foobarbaz".getBytes("UTF-8");
    // 123 makes the value exceed Integer.MAX_VALUE by an arbitrary amount.
    final int totalNumberOfBatches = (Integer.MAX_VALUE / data.length) + 123;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    header.write(BINARY_VERSION_MARKER_1_0);
    // String with length subfield.
    header.write(0x8E);
    IonBinary.writeVarUInt(header, (long) totalNumberOfBatches * data.length);
    InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), // This will provide the data 'totalNumberOfBatches' times
    new RepeatInputStream(data, totalNumberOfBatches - 1));
    IonReader reader = readerBuilder.build(inputStream);
    // If support for large scalars is added, the following line will be deleted and the rest of the test
    // completed to assert the correctness of the value.
    thrown.expect(IonException.class);
    reader.next();
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with RepeatInputStream

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

the class IonReaderBinaryRawLargeStreamTest method cleanlyFailsOnLargeContainerIncremental.

@Test
public void cleanlyFailsOnLargeContainerIncremental() throws Exception {
    final Timestamp timestamp = Timestamp.forDay(2000, 1, 1);
    byte[] data = testData(timestamp);
    // 42 makes the value exceed Integer.MAX_VALUE by an arbitrary amount.
    final int totalNumberOfBatches = (Integer.MAX_VALUE / data.length) + 42;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    header.write(BINARY_VERSION_MARKER_1_0);
    // S-exp with length subfield.
    header.write(0xCE);
    // Length
    IonBinary.writeVarUInt(header, (long) data.length * totalNumberOfBatches);
    InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), // This will provide the data 'totalNumberOfBatches' times
    new RepeatInputStream(data, totalNumberOfBatches - 1));
    IonReader reader = IonReaderBuilder.standard().withIncrementalReadingEnabled(true).build(inputStream);
    thrown.expect(IonException.class);
    reader.next();
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Aggregations

IonReader (com.amazon.ion.IonReader)9 RepeatInputStream (com.amazon.ion.util.RepeatInputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)9 SequenceInputStream (java.io.SequenceInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Timestamp (com.amazon.ion.Timestamp)7 Test (org.junit.Test)6