Skip to content

Library for serializing protobuf objects - Java version

License

Notifications You must be signed in to change notification settings

fabgeyer/pbzlib-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Library for serializing protobuf objects - Java version

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.

Installation and compilation

$ git clone https://github.com/fabgeyer/pbzlib-java.git
$ cd pbzlib-java
$ gradle build

Example

Reading

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

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

Versions in other languages

About

Library for serializing protobuf objects - Java version

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages