public static final ConcurrentHashMap<String, SpecificDatumReader<?>> readerMap =
new ConcurrentHashMap<String, SpecificDatumReader<?>>();
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> byte[] serializer(T value, Schema schema) throws IOException{
SpecificDatumWriter writer = (SpecificDatumWriter<?>) writerMap.get(schema.getFullName());
if (writer == null) {
writer = new SpecificDatumWriter(schema);// ignore dirty bits
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;
}