Search in sources :

Example 1 with UnownedInputStream

use of org.apache.beam.sdk.util.UnownedInputStream in project beam by apache.

the class CoderProperties method decode.

@VisibleForTesting
static <T> T decode(Coder<T> coder, Coder.Context context, byte[] bytes) throws CoderException, IOException {
    @SuppressWarnings("unchecked") Coder<T> deserializedCoder = SerializableUtils.clone(coder);
    byte[] buffer;
    if (context == Coder.Context.NESTED) {
        buffer = new byte[bytes.length + 1];
        System.arraycopy(bytes, 0, buffer, 0, bytes.length);
        buffer[bytes.length] = 1;
    } else {
        buffer = bytes;
    }
    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
    T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
    assertThat("consumed bytes equal to encoded bytes", cis.getCount(), equalTo((long) bytes.length));
    return value;
}
Also used : UnownedInputStream(org.apache.beam.sdk.util.UnownedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(com.google.common.io.CountingInputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CountingInputStream (com.google.common.io.CountingInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnownedInputStream (org.apache.beam.sdk.util.UnownedInputStream)1