This library is used for simplifying the serialization and deserialization of protocol buffer objects to/from files.
The main use-case is to save and read a large collection of objects of the same type.
Each file contains a header with the description of the protocol buffer, meaning that no compilation of .proto
description file is required before reading a pbz
file.
$ git clone https://github.com/fabgeyer/pbzlib-java.git
$ cd pbzlib-java
$ gradle build
Reading a pbz
file:
import com.github.fabgeyer.pbzlib.io.PBZReader;
import com.google.protobuf.DynamicMessage;
public class Main {
public static void main(String[] args) throws Exception {
PBZReader rdr = new PBZReader("file.pbz");
while (true) {
DynamicMessage msg = rdr.nextMessage();
if (msg == null) {
break;
}
System.out.println(msg);
}
}
}
Reading a pbz
file with a given target class:
import your.protobuf.YourProtobufClass;
import com.github.fabgeyer.pbzlib.io.PBZReader;
public class Main {
public static void main(String[] args) throws Exception {
PBZReader rdr = new PBZReader("file.pbz");
while (true) {
YourProtobufClass obj = rdr.nextMessage(YourProtobufClass.parser());
if (obj == null) {
break;
}
System.out.println(obj);
}
}
}
Writing a pbz
file:
import com.github.fabgeyer.pbzlib.io.PBZWriter;
import your.protobuf.MainClass;
import your.protobuf.Message;
public class Main {
public static void main(String[] args) throws Exception {
PBZWriter wrtr = new PBZWriter("file.pbz", MainClass.getDescriptor());
Message msg = Message.newBuilder.setField(value).build();
wrtr.add(msg);
wrtr.close();
}
}