Search in sources :

Example 6 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class DataFrame method read.

// endregion
// region Reading
/**
 * Interprets the given InputStream as a DataFrame and returns a DataFrameEntryIterator for the entries serialized
 * in it.
 *
 * @param source  The InputStream to read from.
 * @param length  The size of the inputStream.
 * @param address The DataFrame's address.
 * @return A new DataFrameEntryIterator.
 * @throws IOException If unable to parse the DataFrame's header from the InputStream.
 */
public static DataFrameEntryIterator read(InputStream source, int length, LogAddress address) throws IOException {
    // Check to see that we have enough bytes in the InputStream.
    ReadFrameHeader header = new ReadFrameHeader(source);
    if (length < ReadFrameHeader.SERIALIZATION_LENGTH + header.getContentLength()) {
        throw new SerializationException(String.format("Given buffer has insufficient number of bytes for this DataFrame. Expected %d, actual %d.", ReadFrameHeader.SERIALIZATION_LENGTH + header.getContentLength(), length));
    }
    BoundedInputStream contents = new BoundedInputStream(source, header.getContentLength());
    return new DataFrameEntryIterator(contents, address, ReadFrameHeader.SERIALIZATION_LENGTH);
}
Also used : SerializationException(io.pravega.common.io.SerializationException) BoundedInputStream(io.pravega.common.io.BoundedInputStream)

Example 7 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class RevisionDataOutputStreamTests method testNonSeekableOutputShorterLength.

/**
 * Tests the NonSeekableRevisionDataOutput class when we provide a shorter length than expected.
 */
@Test
public void testNonSeekableOutputShorterLength() throws Exception {
    @Cleanup val s = new ByteArrayOutputStream();
    // Wrap the stream, but do not auto-close it since we expect close() to fail, which is verified below.
    val impl = RevisionDataOutputStream.wrap(s);
    int correctLength = Byte.BYTES + Short.BYTES + Integer.BYTES;
    // Shorter length.
    impl.length(correctLength - 1);
    impl.writeByte(1);
    impl.writeShort(2);
    impl.writeInt(3);
    // Verify close() fails.
    AssertExtensions.assertThrows("RevisionDataOutputStream.close() did not throw for byte mismatch.", impl::close, ex -> ex instanceof SerializationException);
    // Verify the written data cannot be read back (we'll get an EOF at this time).
    @Cleanup val inputStream = RevisionDataInputStream.wrap(new ByteArrayInputStream(s.toByteArray()));
    inputStream.readByte();
    inputStream.readShort();
    AssertExtensions.assertThrows("Expecting EOF.", inputStream::readInt, ex -> ex instanceof EOFException);
}
Also used : lombok.val(lombok.val) SerializationException(io.pravega.common.io.SerializationException) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) EnhancedByteArrayOutputStream(io.pravega.common.io.EnhancedByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FixedByteArrayOutputStream(io.pravega.common.io.FixedByteArrayOutputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

SerializationException (io.pravega.common.io.SerializationException)7 lombok.val (lombok.val)3 Test (org.junit.Test)3 EnhancedByteArrayOutputStream (io.pravega.common.io.EnhancedByteArrayOutputStream)2 FixedByteArrayOutputStream (io.pravega.common.io.FixedByteArrayOutputStream)2 DurableDataLog (io.pravega.segmentstore.storage.DurableDataLog)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 Cleanup (lombok.Cleanup)2 Exceptions (io.pravega.common.Exceptions)1 ObjectClosedException (io.pravega.common.ObjectClosedException)1 Callbacks (io.pravega.common.function.Callbacks)1 BoundedInputStream (io.pravega.common.io.BoundedInputStream)1 DataCorruptionException (io.pravega.segmentstore.server.DataCorruptionException)1 TestDurableDataLog (io.pravega.segmentstore.server.TestDurableDataLog)1 DataLogNotAvailableException (io.pravega.segmentstore.storage.DataLogNotAvailableException)1 DurableDataLogException (io.pravega.segmentstore.storage.DurableDataLogException)1 LogAddress (io.pravega.segmentstore.storage.LogAddress)1