use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class VersionedSerializer method serialize.
/**
* Serializes the given object to an in-memory buffer (RandomAccessOutputStream) and returns a view of it.
*
* @param object The object to serialize.
* @return An ArrayView which represents the serialized data. This provides a view (offset+length) into a Java byte
* array and has APIs to extract or copy the data out of there.
* @throws IOException If an IO Exception occurred.
*/
public ByteArraySegment serialize(T object) throws IOException {
val result = new ByteBufferOutputStream();
serialize(result, object);
return result.getData();
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class RevisionDataStreamCommonTests method testLength.
private <T> void testLength(BiConsumerWithException<RevisionDataOutputStream, T> write, BiFunction<RevisionDataOutputStream, T, Integer> getLength, T value) throws Exception {
@Cleanup val os = new ByteBufferOutputStream();
@Cleanup val rdos = RevisionDataOutputStream.wrap(os);
val initialLength = os.getData().getLength();
write.accept(rdos, value);
rdos.flush();
val expectedValue = os.getData().getLength() - initialLength;
val actualValue = getLength.apply(rdos, value);
Assert.assertEquals(String.format("Unexpected length for '%s'.", value), expectedValue, (int) actualValue);
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class CompositeByteArraySegmentTests method testSliceRead.
/**
* Tests the {@link CompositeByteArraySegment#slice} method while reading indirectly by invoking
* {@link CompositeByteArraySegment#getReader(int, int)}.
*/
@Test
public void testSliceRead() {
testProgressiveCopies((expectedData, s, offset, length) -> {
@Cleanup val targetStream = new ByteBufferOutputStream(s.getLength());
s.copyTo(targetStream);
val targetData = targetStream.getData().getCopy();
for (int sliceOffset = 0; sliceOffset <= s.getLength() / 2; sliceOffset++) {
val sliceLength = s.getLength() - 2 * sliceOffset;
InputStream reader = s.getReader(sliceOffset, sliceLength);
if (sliceLength == 0) {
Assert.assertEquals("Unexpected data read for empty slice.", -1, reader.read());
} else {
val actualData = StreamHelpers.readAll(reader, sliceLength);
AssertExtensions.assertArrayEquals("Unexpected data sliced for step " + offset, targetData, sliceOffset, actualData, 0, actualData.length);
}
}
});
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class DataFrameOutputStreamTests method testWritePrimitiveTypes.
private <T> void testWritePrimitiveTypes(Function<Integer, T> toPrimitiveType, OutputStreamWriter<T> writer, OutputStreamExpectedWriter<T> expectedWriter) throws Exception {
// Very small frame, so we can test switching over to new frames.
int maxFrameSize = 511;
val values = IntStream.range(0, Short.MAX_VALUE).boxed().map(toPrimitiveType).collect(Collectors.toList());
@Cleanup val expectedRecord = new ByteBufferOutputStream();
@Cleanup val expectedRecordWriter = new DataOutputStream(expectedRecord);
// Callback for when a frame is written.
ArrayList<DataFrame> writtenFrames = new ArrayList<>();
try (DataFrameOutputStream s = new DataFrameOutputStream(maxFrameSize, writtenFrames::add)) {
// Write a single record, and dump all values in it.
s.startNewRecord();
for (val v : values) {
writer.accept(s, v);
expectedWriter.accept(expectedRecordWriter, v);
}
s.endRecord();
// Seal whatever is left at the end.
s.flush();
expectedRecordWriter.flush();
}
AssertExtensions.assertGreaterThan("No frame has been created during the test.", 0, writtenFrames.size());
val readFrames = writtenFrames.stream().map(this::readFrame).collect(Collectors.toList());
DataFrameTestHelpers.checkReadRecords(readFrames, Collections.singletonList(expectedRecord.getData()), b -> b);
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class TestLogItem method getFullSerialization.
@SneakyThrows(IOException.class)
byte[] getFullSerialization() {
int expectedSize = Long.BYTES + Integer.BYTES + this.data.length;
@Cleanup val resultStream = new ByteBufferOutputStream(expectedSize);
serialize(resultStream);
val result = resultStream.getData().getCopy();
Assert.assertEquals(expectedSize, result.length);
return result;
}
Aggregations