use of co.cask.common.io.ByteBufferInputStream in project cdap by caskdata.
the class FlowletProgramRunner method createInputDatumDecoder.
private <T> Function<ByteBuffer, T> createInputDatumDecoder(final TypeToken<T> dataType, final Schema schema, final SchemaCache schemaCache) {
final ReflectionDatumReader<T> datumReader = new ReflectionDatumReader<>(schema, dataType);
final ByteBufferInputStream byteBufferInput = new ByteBufferInputStream(null);
final BinaryDecoder decoder = new BinaryDecoder(byteBufferInput);
return new Function<ByteBuffer, T>() {
@Nullable
@Override
public T apply(ByteBuffer input) {
byteBufferInput.reset(input);
try {
final Schema sourceSchema = schemaCache.get(input);
Preconditions.checkNotNull(sourceSchema, "Fail to find source schema.");
return datumReader.read(decoder, sourceSchema);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("dataType", dataType).add("schema", schema).toString();
}
};
}
use of co.cask.common.io.ByteBufferInputStream in project cdap by caskdata.
the class StreamEventCodec method decodePayload.
/**
* Reverse of {@link #encodePayload(StreamEvent)}.
*
* @param payload The byte array containing the queue payload.
* @return A {@link StreamEvent} reconstructed from payload.
*/
public StreamEvent decodePayload(byte[] payload) {
ByteBuffer buffer = ByteBuffer.wrap(payload);
SchemaHash schemaHash = new SchemaHash(buffer);
Preconditions.checkArgument(schemaHash.equals(STREAM_EVENT_SCHEMA.getSchemaHash()), "Schema from payload not matching StreamEvent schema.");
Decoder decoder = new BinaryDecoder(new ByteBufferInputStream(buffer));
try {
StreamEventData data = StreamEventDataCodec.decode(decoder);
// Read the timestamp
long timestamp = decoder.readLong();
return new StreamEvent(data, timestamp);
} catch (IOException e) {
// It should never happens, otherwise something very wrong.
throw Throwables.propagate(e);
}
}
Aggregations