Search in sources :

Example 16 with ByteBufferOutputStream

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);
}
Also used : lombok.val(lombok.val) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 17 with ByteBufferOutputStream

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();
}
Also used : lombok.val(lombok.val) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer)

Example 18 with ByteBufferOutputStream

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);
    });
}
Also used : lombok.val(lombok.val) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 19 with ByteBufferOutputStream

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();
    }
}
Also used : ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream)

Aggregations

ByteBufferOutputStream (io.pravega.common.io.ByteBufferOutputStream)19 lombok.val (lombok.val)15 Cleanup (lombok.Cleanup)13 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataOutputStream (java.io.DataOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SneakyThrows (lombok.SneakyThrows)3 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)2 AssertExtensions (io.pravega.test.common.AssertExtensions)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 Random (java.util.Random)2 Assert (org.junit.Assert)2 Charsets (com.google.common.base.Charsets)1 ByteBuf (io.netty.buffer.ByteBuf)1 Exceptions (io.pravega.common.Exceptions)1 Futures (io.pravega.common.concurrent.Futures)1