Search in sources :

Example 26 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class ObjectStoreDataset method decode.

private T decode(byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    // decode T using schema
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    BinaryDecoder decoder = new BinaryDecoder(bis);
    try {
        return getReflectionDatumReader().read(decoder, this.schema);
    } catch (IOException e) {
        // SHOULD NEVER happen
        throw new DataSetException("Failed to decode read object: " + e.getMessage(), e);
    }
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Example 27 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class ASMDatumCodecTest method testList.

@Test
public void testList() throws IOException, UnsupportedTypeException {
    TypeToken<List<Long>> type = new TypeToken<List<Long>>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    List<Long> writeValue = ImmutableList.of(1L, 10L, 100L, 1000L);
    DatumWriter<List<Long>> writer = getWriter(type);
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<List<Long>> reader = new ReflectionDatumReader<>(getSchema(type), type);
    List<Long> value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(writeValue, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 28 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class ASMDatumCodecTest method testStreamEvent.

@Test
public void testStreamEvent() throws IOException, UnsupportedTypeException {
    TypeToken<StreamEvent> type = new TypeToken<StreamEvent>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<StreamEvent> writer = getWriter(type);
    StreamEvent event = new StreamEvent(ImmutableMap.of("key", "value"), ByteBuffer.wrap("Testing message".getBytes(Charsets.UTF_8)));
    writer.encode(event, new BinaryEncoder(os));
    ReflectionDatumReader<StreamEvent> reader = new ReflectionDatumReader<>(getSchema(type), type);
    StreamEvent value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(event.getHeaders(), value.getHeaders());
    Assert.assertEquals(event.getBody(), value.getBody());
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 29 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class ASMDatumCodecTest method testInt.

@Test
public void testInt() throws UnsupportedTypeException, IOException {
    TypeToken<Integer> type = new TypeToken<Integer>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Integer> writer = getWriter(type);
    writer.encode(12234234, new BinaryEncoder(os));
    ReflectionDatumReader<Integer> reader = new ReflectionDatumReader<>(getSchema(type), type);
    int value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(12234234, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 30 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class ASMDatumCodecTest method testShort.

@Test
public void testShort() throws UnsupportedTypeException, IOException {
    TypeToken<Short> type = new TypeToken<Short>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Short> writer = getWriter(type);
    writer.encode((short) 3000, new BinaryEncoder(os));
    ReflectionDatumReader<Short> reader = new ReflectionDatumReader<>(getSchema(type), type);
    short value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals((short) 3000, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Aggregations

BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)36 Test (org.junit.Test)23 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)22 PipedInputStream (java.io.PipedInputStream)21 PipedOutputStream (java.io.PipedOutputStream)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)19 TypeToken (com.google.common.reflect.TypeToken)17 Schema (co.cask.cdap.api.data.schema.Schema)10 IOException (java.io.IOException)10 Decoder (co.cask.cdap.common.io.Decoder)6 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)6 ByteBufferInputStream (co.cask.common.io.ByteBufferInputStream)6 ImmutableList (com.google.common.collect.ImmutableList)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)5 ByteBuffer (java.nio.ByteBuffer)5 List (java.util.List)5 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3