Search in sources :

Example 1 with TFRecordCodec

use of org.apache.beam.sdk.io.TFRecordIO.TFRecordCodec in project beam by apache.

the class TFRecordIOTest method testTFRecordCodec.

@Test
public void testTFRecordCodec() throws IOException {
    Decoder b64 = Base64.getDecoder();
    TFRecordCodec codec = new TFRecordCodec();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PickyWriteChannel outChan = new PickyWriteChannel(baos);
    codec.write(outChan, "foo".getBytes(StandardCharsets.UTF_8));
    assertArrayEquals(b64.decode(FOO_RECORD_BASE64), baos.toByteArray());
    codec.write(outChan, "bar".getBytes(StandardCharsets.UTF_8));
    assertArrayEquals(b64.decode(FOO_BAR_RECORD_BASE64), baos.toByteArray());
    PickyReadChannel inChan = new PickyReadChannel(new ByteArrayInputStream(baos.toByteArray()));
    byte[] foo = codec.read(inChan);
    byte[] bar = codec.read(inChan);
    assertNull(codec.read(inChan));
    assertEquals("foo", new String(foo, StandardCharsets.UTF_8));
    assertEquals("bar", new String(bar, StandardCharsets.UTF_8));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TFRecordCodec(org.apache.beam.sdk.io.TFRecordIO.TFRecordCodec) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Decoder(java.util.Base64.Decoder) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Decoder (java.util.Base64.Decoder)1 TFRecordCodec (org.apache.beam.sdk.io.TFRecordIO.TFRecordCodec)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1