public GenericRecord reencode(GenericRecord r, Schema readerSchema) throws AvroSerdeException { baos.reset(); BinaryEncoder be = EncoderFactory.get().directBinaryEncoder(baos, null); gdw.setSchema(r.getSchema()); try { gdw.write(r, be); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); binaryDecoder = DecoderFactory.defaultFactory().createBinaryDecoder(bais, binaryDecoder); ReaderWriterSchemaPair pair = new ReaderWriterSchemaPair(r.getSchema(), readerSchema); GenericDatumReader<GenericRecord> gdr = gdrCache.retrieve(pair); return gdr.read(r, binaryDecoder); } catch (IOException e) { throw new AvroSerdeException("Exception trying to re-encode record to new schema", e); } } }