use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class RevisionDataOutputStreamTests method testRandomOutputExpandable.
/**
* Tests the RandomRevisionDataOutput class with an {@link ByteBufferOutputStream}.
*/
@Test
public void testRandomOutputExpandable() throws Exception {
@Cleanup val s = new ByteBufferOutputStream();
@Cleanup val impl = RevisionDataOutputStream.wrap(s);
testImpl(impl, s::getData);
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class BitConverterTests method testLong.
/**
* Tests the {@link BitConverter#writeLong} and {@link BitConverter#readLong}.
*/
@Test
public void testLong() throws IOException {
test(BitConverter::writeLong, BitConverter::readLong, DataInputStream::readLong, Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L);
WriteArray<Long> streamWriter = (target, offset, value) -> {
@Cleanup val s = new ByteBufferOutputStream();
try {
BitConverter.writeLong(s, value);
} catch (IOException ex) {
throw new CompletionException(ex);
}
s.getData().copyTo(target, offset, s.size());
return s.size();
};
test(streamWriter, BitConverter::readLong, DataInputStream::readLong, Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L);
ReadStream<Long> streamReader = s -> {
byte[] data = new byte[Long.BYTES];
Assert.assertEquals(Long.BYTES, s.read(data));
return BitConverter.readLong(data, 0);
};
testStream(BitConverter::writeLong, streamReader, DataInputStream::readLong, Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L);
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class BitConverterTests method testStream.
@SafeVarargs
private final <T> void testStream(WriteStream<T> write, ReadStream<T> read, ReadDataStream<T> dataStreamReader, T... testValues) throws IOException {
for (T value : testValues) {
@Cleanup val s = new ByteBufferOutputStream(MAX_LENGTH);
write.apply(s, value);
T readValue = read.apply(s.getData().getReader());
Assert.assertEquals("Unexpected deserialized value.", value, readValue);
// Use a DataInputStream to verify that the value was correctly encoded.
@Cleanup val ds = new DataInputStream(s.getData().getReader());
T dataStreamReadValue = dataStreamReader.apply(ds);
Assert.assertEquals("Unexpected deserialized value (DataInputStream).", dataStreamReadValue, value);
}
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class RevisionDataStreamCommonTests method testGetRemaining.
/**
* Tests {@link RevisionDataInput#getRemaining()}.
*/
@Test
public void testGetRemaining() throws Exception {
@Cleanup val os = new ByteBufferOutputStream();
@Cleanup val rdos = RevisionDataOutputStream.wrap(os);
rdos.writeInt(1);
rdos.writeLong(2L);
rdos.writeBuffer(new ByteArraySegment(new byte[3]));
rdos.flush();
rdos.close();
// BoundedInputStream header.
int expectedRemaining = os.getData().getLength() - Integer.BYTES;
// Use a SequenceInputStream - this will always have available() set to 0.
@Cleanup val rdis = RevisionDataInputStream.wrap(os.getData().getReader());
Assert.assertEquals(expectedRemaining, rdis.getRemaining());
Assert.assertEquals(1, rdis.readInt());
expectedRemaining -= Integer.BYTES;
Assert.assertEquals(expectedRemaining, rdis.getRemaining());
Assert.assertEquals(2L, rdis.readLong());
expectedRemaining -= Long.BYTES;
Assert.assertEquals(expectedRemaining, rdis.getRemaining());
Assert.assertEquals(3, rdis.readArray().length);
expectedRemaining = 0;
Assert.assertEquals(expectedRemaining, rdis.getRemaining());
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class RevisionDataStreamCommonTests method testEncodeDecode.
private <T> void testEncodeDecode(BiConsumerWithException<RevisionDataOutputStream, T> write, FunctionWithException<RevisionDataInputStream, T> read, BiFunction<RevisionDataOutputStream, T, Integer> getLength, T value, BiPredicate<T, T> equalityTester) throws Exception {
@Cleanup val os = new ByteBufferOutputStream();
@Cleanup val rdos = RevisionDataOutputStream.wrap(os);
write.accept(rdos, value);
rdos.close();
os.close();
// Subtract 4 because this is the Length being encoded.
val actualLength = os.size() - Integer.BYTES;
Assert.assertEquals("Unexpected length for value " + value, (int) getLength.apply(rdos, value), actualLength);
@Cleanup val rdis = RevisionDataInputStream.wrap(os.getData().getReader());
val actualValue = read.apply(rdis);
Assert.assertTrue(String.format("Encoding/decoding failed for %s (decoded %s).", value, actualValue), equalityTester.test(value, actualValue));
}
Aggregations