use of org.apache.avro.io.BinaryEncoder in project beam by apache.
the class AvroCoder method encode.
@Override
public void encode(T value, OutputStream outStream) throws IOException {
// Get a BinaryEncoder instance from the ThreadLocal cache and attempt to reuse it.
BinaryEncoder encoderInstance = ENCODER_FACTORY.directBinaryEncoder(outStream, encoder.get());
// Save the potentially-new instance for reuse later.
encoder.set(encoderInstance);
writer.get().write(value, encoderInstance);
// Direct binary encoder does not buffer any data and need not be flushed.
}
use of org.apache.avro.io.BinaryEncoder in project gora by apache.
the class AvroSerializerUtil method serializer.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> byte[] serializer(T value, Schema schema) throws IOException {
SpecificDatumWriter writer = writerMap.get(schema.getFullName());
if (writer == null) {
// ignore dirty bits
writer = new SpecificDatumWriter(schema);
writerMap.put(schema.getFullName(), writer);
}
BinaryEncoder encoderFromCache = encoders.get();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
outputStream.set(bos);
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
if (encoderFromCache == null) {
encoders.set(encoder);
}
//reset the buffers
ByteArrayOutputStream os = outputStream.get();
os.reset();
writer.write(value, encoder);
encoder.flush();
byte[] byteValue = os.toByteArray();
return byteValue;
}
use of org.apache.avro.io.BinaryEncoder in project gora by apache.
the class IOUtils method serialize.
/**
* Serializes the field object using the datumWriter.
*
* @param <T> class type of object to be serialized.
* @param datumWriter AVRO datum writer for given schema.
* @param object object to be serialized.
* @param os output stream which serialized content is written.
* @throws IOException occurred while serializing the object to bytes.
*/
public static <T extends SpecificRecord> void serialize(OutputStream os, SpecificDatumWriter<T> datumWriter, T object) throws IOException {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(os, null);
datumWriter.write(object, encoder);
encoder.flush();
}
use of org.apache.avro.io.BinaryEncoder in project cdap by caskdata.
the class LoggingEventSerializer method toBytes.
/**
* Encodes a {@link ILoggingEvent} to byte array.
*/
public byte[] toBytes(ILoggingEvent event) {
event.prepareForDeferredProcessing();
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(getAvroSchema());
try {
writer.write(toGenericRecord(event), encoder);
} catch (IOException e) {
// This shouldn't happen since we are writing to byte array output stream.
throw Throwables.propagate(e);
}
return out.toByteArray();
}
Aggregations