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);
}
}
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);
}
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());
}
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);
}
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);
}
Aggregations