I have a ByteArrayInputStream
that was serialized with a List<TestAvroModel>
which is an implementation of SpecificRecord. I could not find a way for Avro to know about the list that was serialized so I did a hackish way to loop through the ByteArrayInputStream
.
//TestAvroModel is an implementation of SpecificRecord
List<TestAvroModel> models;
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
for(TestAvroModel model: models) {
DatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(model.getSchema());
Encoder encoder = new EncoderFactory().binaryEncoder(byteArrayStream, null);
writer.write(model, encoder);
encoder.flush();
}
//This was pre-serialized with a List of TestAvroModel
ByteArrayInputStream inputStream;
DatumReader<TestAvroModel> reader = new SpecificDatumReader<>(TestAvroModel.getClassSchema());
Decoder decoder = DecoderFactory().get().binaryDecoder(inputStream, null);
List<TestAvroModel> records = new ArrayList<>();
boolean eof = false;
while(!eof) {
try {
records.add(reader.read(null, decoder));
catch(EOFException ex) {
eof = true;
}
}
This way worked and read the serialized List<TestAvroModel>
one at a time and added it to my records list. Though looping through a DatumReader
until an EOFException
does not seem like the best way, but I haven't found a better way.
I couldn't find anything in the Avro libraries that dealt with an InputStream
with multiple Avro records in it. Though it has to have breaking points in the stream for Avro to be able to read individual records the way I did above. To reiterate, does anyone know a better way to loop through the DatumReader
then the way shown above?