Search in sources :

Example 1 with CountingInputStream

use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.CountingInputStream 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 (Objects.equals(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(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.CountingInputStream) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Aggregations

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