Search in sources :

Example 1 with EnhancedByteArrayOutputStream

use of io.pravega.common.io.EnhancedByteArrayOutputStream 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 EnhancedByteArrayOutputStream();
    @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) Cleanup(lombok.Cleanup) EnhancedByteArrayOutputStream(io.pravega.common.io.EnhancedByteArrayOutputStream)

Example 2 with EnhancedByteArrayOutputStream

use of io.pravega.common.io.EnhancedByteArrayOutputStream 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 EnhancedByteArrayOutputStream();
    serialize(result, object);
    return result.getData();
}
Also used : lombok.val(lombok.val) EnhancedByteArrayOutputStream(io.pravega.common.io.EnhancedByteArrayOutputStream)

Example 3 with EnhancedByteArrayOutputStream

use of io.pravega.common.io.EnhancedByteArrayOutputStream 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.
 */
@SneakyThrows(IOException.class)
static ByteArraySegment serialize(RollingSegmentHandle handle) {
    try (EnhancedByteArrayOutputStream os = new EnhancedByteArrayOutputStream()) {
        // 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 : EnhancedByteArrayOutputStream(io.pravega.common.io.EnhancedByteArrayOutputStream) SneakyThrows(lombok.SneakyThrows)

Example 4 with EnhancedByteArrayOutputStream

use of io.pravega.common.io.EnhancedByteArrayOutputStream in project pravega by pravega.

the class RevisionDataOutputStreamTests method testRandomOutputExpandable.

/**
 * Tests the RandomRevisionDataOutput class with an expandable RandomAccessOutputStream.
 */
@Test
public void testRandomOutputExpandable() throws Exception {
    @Cleanup val s = new EnhancedByteArrayOutputStream();
    @Cleanup val impl = RevisionDataOutputStream.wrap(s);
    testImpl(impl, s::getData);
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) EnhancedByteArrayOutputStream(io.pravega.common.io.EnhancedByteArrayOutputStream) Test(org.junit.Test)

Example 5 with EnhancedByteArrayOutputStream

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

Aggregations

EnhancedByteArrayOutputStream (io.pravega.common.io.EnhancedByteArrayOutputStream)5 lombok.val (lombok.val)4 Cleanup (lombok.Cleanup)3 AssertExtensions (io.pravega.test.common.AssertExtensions)1 Arrays (java.util.Arrays)1 SneakyThrows (lombok.SneakyThrows)1 Test (org.junit.Test)1