use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class AbstractBufferViewTests method testBuilder.
/**
* Tests {@link BufferView#builder()}.
*/
@Test
public void testBuilder() throws IOException {
val components = new ArrayList<BufferView>();
val builder = BufferView.builder();
// Empty buffer.
builder.add(BufferView.empty());
Assert.assertEquals(0, builder.getLength());
Assert.assertSame("Expected empty buffer view if no components added.", BufferView.empty(), builder.build());
// One-component buffer.
val c1 = new ByteArraySegment("component1".getBytes());
components.add(c1);
builder.add(c1);
Assert.assertEquals("Unexpected length with one component.", c1.getLength(), builder.getLength());
Assert.assertSame("Unexpected result with one component.", c1, builder.build());
// Multi-component buffer.
val c2 = new ByteArraySegment("component2".getBytes());
val c3 = new ByteArraySegment("component3".getBytes());
val c4 = new ByteArraySegment("component4".getBytes());
// Adding same buffer multiple times is OK.
val compositeComponents = Arrays.asList(new BufferView[] { c1, c2, c3, c4 });
builder.add(BufferView.wrap(compositeComponents));
components.addAll(compositeComponents);
val expectedLength = c1.getLength() * 2 + c2.getLength() + c3.getLength() + c4.getLength();
Assert.assertEquals(expectedLength, builder.getLength());
val finalBuffer = builder.build();
val expectedDataWriter = new ByteBufferOutputStream(expectedLength);
for (val c : components) {
c.copyTo(expectedDataWriter);
}
val expectedData = expectedDataWriter.getData();
Assert.assertEquals(expectedData, finalBuffer);
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class BufferViewTestBase method getData.
private ArrayView getData(List<ByteBuffer> buffers) {
@Cleanup val os = new ByteBufferOutputStream();
for (ByteBuffer buffer : buffers) {
byte[] contents = new byte[buffer.remaining()];
buffer.get(contents);
assert buffer.remaining() == 0;
os.write(contents);
}
return os.getData();
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class CompositeByteArraySegmentTests method testCopyToStream.
/**
* Tests the functionality of {@link CompositeByteArraySegment#copyTo(OutputStream)}.
*/
@Override
@Test
public void testCopyToStream() {
testProgressiveCopies((expectedData, s, offset, length) -> {
@Cleanup val targetStream = new ByteBufferOutputStream();
s.copyTo(targetStream);
val targetData = targetStream.getData().getCopy();
Assert.assertArrayEquals("Unexpected data copied for step " + offset, expectedData, targetData);
});
}
use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.
the class HandleSerializer method serialize.
/**
* Serializes an entire RollingSegmentHandle into a new ByteArraySegment.
*
* @param handle The RollingSegmentHandle to serialize.
* @return A ByteArraySegment with the serialization.
*/
static ByteArraySegment serialize(RollingSegmentHandle handle) {
try (ByteBufferOutputStream os = new ByteBufferOutputStream()) {
// 1. Policy Max Size.
os.write(combine(KEY_POLICY_MAX_SIZE, Long.toString(handle.getRollingPolicy().getMaxLength())));
// 2. Chunks.
handle.chunks().forEach(chunk -> os.write(serializeChunk(chunk)));
return os.getData();
}
}
Aggregations