Search in sources :

Example 1 with ByteBufferOutputStream

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

Example 2 with ByteBufferOutputStream

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

Example 3 with ByteBufferOutputStream

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

Example 4 with ByteBufferOutputStream

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

Example 5 with ByteBufferOutputStream

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

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