I have a AvroHelper that serializes/deserializes about 99% of some very
complex classes, The one class component it cannot seem to handle is a
field that contains a simple byte array. The helper is defined

public class AvroHelper<T> {

    private Class<T> type;
    private Schema schema;
    private DatumReader<T> reader;
    private DatumWriter<T> writer;
    private ByteArrayOutputStream bos;
    private Encoder encoder;
    private DecoderFactory decoderFactory;

    public AvroHelper(Class<T> type) {

           this.type = type;
           this.schema = ReflectData.AllowNull.get().getSchema(type);
           this.bos = new ByteArrayOutputStream();
           this.decoderFactory = DencoderFactory.get();
           this.encoder = EncoderFactory.get().binaryEncoder(bos, null);
           this.reader = new ReflectDatumReader<T>(schema);
           this.writer = new ReflectDatumWriter<T>(schema);
    }

    public byte[] toAvroBytes( T o) throws IOException {
           bos.reset();
           writer.write(o, encoder);
           encoder.flush();
           return bos.toByteArray();
    }

    public T fromAvroBytes(byte [] raw) throws IOException {
              return reader.read(null, decoderFactory.binaryDecoder(raw,
null));
    }
}

The simple test POJO is

public class TestAvroData {
         private byte [] data;

         public TestAvroData() {
         }

         public void setData(byte [] data) {
               this.data = data;
         }

         public byte[] getData() {
                 return this.data;
         }
}

Unit Test

public class  TestAvroHelper {

      @Test
      public void testAvroHelper() {
          AvroHelper<TestAvroData> helper = new
AvroHelper<TestAvroHelper>(TestAvroHelper.class);


      }

Reply via email to