read and write multiple objects from protobuff

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);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s