use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class ASMDatumCodecTest method testTree.
@Test
public void testTree() throws IOException, UnsupportedTypeException {
TypeToken<Node> type = new TypeToken<Node>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
DatumWriter<Node> writer = getWriter(type);
Node root = new Node((short) 1, new Node((short) 2, null, new Node((short) 3, null, null)), new Node((short) 4, new Node((short) 5, null, null), null));
writer.encode(root, new BinaryEncoder(os));
ReflectionDatumReader<Node> reader = new ReflectionDatumReader<>(getSchema(type), type);
Node value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertEquals(root, value);
}
use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class QueueEntry method serializeEmptyHashKeys.
// many entries will have no hash keys. Serialize that once and for good
private static byte[] serializeEmptyHashKeys() {
try {
// we don't synchronize here: the worst thing that go wrong here is repeated assignment to the same value
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(bos);
encoder.writeInt(0);
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("encoding empty hash keys went wrong - bailing out: " + e.getMessage(), e);
}
}
use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class StreamEventCodec method encodePayload.
/**
* Encodes the given {@link StreamEvent} into {@code byte[]} that could become
* payload of a QueueEntry.
*
* @param event The {@link StreamEvent} to encode.
* @return Encoded {@code byte[]}.
*/
public byte[] encodePayload(StreamEvent event) {
// TODO: This is a bit hacky to do it directly for now, for performance reason.
ByteBuffer body = event.getBody();
Map<String, String> headers = event.getHeaders();
long timestamp = event.getTimestamp();
// Some assumption on the header size to minimize array copying
// 16 bytes Schema hash + body size + (header size) * (50 bytes key/value pair) + 9 bytes timestamp (vlong encoding)
ByteArrayOutputStream os = new ByteArrayOutputStream(16 + body.remaining() + headers.size() * 50 + 9);
Encoder encoder = new BinaryEncoder(os);
try {
// Write the schema hash
os.write(STREAM_EVENT_SCHEMA.getSchemaHash().toByteArray());
StreamEventDataCodec.encode(event, encoder);
encoder.writeLong(timestamp);
return os.toByteArray();
} catch (IOException e) {
// It should never happens, otherwise something very wrong.
throw Throwables.propagate(e);
}
}
use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class AccessTokenIdentifierCodec method encode.
@Override
public byte[] encode(AccessTokenIdentifier identifier) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(bos);
encoder.writeInt(AccessTokenIdentifier.Schemas.getVersion());
DatumWriter<AccessTokenIdentifier> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, AccessTokenIdentifier.Schemas.getCurrentSchema());
writer.encode(identifier, encoder);
return bos.toByteArray();
}
use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class KeyIdentifierCodec method encode.
@Override
public byte[] encode(KeyIdentifier keyIdentifier) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(bos);
encoder.writeInt(KeyIdentifier.Schemas.getVersion());
DatumWriter<KeyIdentifier> writer = writerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema());
writer.encode(keyIdentifier, encoder);
return bos.toByteArray();
}
Aggregations