a simple trick to read and write the object through protobuff, is to create an additional wrapper .proto

syntax = "proto3";option java_package = "models";message Model{
int64 id =1;
string name =2;
}

then wrap it

message Models {
repeated Model models = 1;
}

then instead of read and write the objects one by one through java stream. it could be read and write in one go through Models.

ModelsClass.Models models = ModelsClass.Models.newBuilder().mergeFrom(fileInputStream).build();models.writeTo(fileOutputStream);