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);
}
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);
}
Aggregations