RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Implement Java Object Management Using a Protocol Buffer : Page 2

Find out how to bring a protocol buffer -- the simplest and most efficient way to manage structured data -- into your Java object management.


The Google Protocol Buffer Compiler for Java

Developers can download Google's complier from the protobuf download page. As previously stated, this compiler converts a .proto message to a Java-based source file of the message. Here is the command for converting a message to a Java source file.

Protoc --proto_path=<proto compiler path>  --java_out=<java output file path>  <full path of proto file>

Using this Java output, a developer can build a message (which is used for storing and retrieving a message in the file). Messages are stored as objects in the file, which helps speed up storage and retrieval of data. While storing the object in the output file, the stored object also gets serialized. If any change occurs in the message, the developer must rebuild the proto message.

The proto compiler reads the message, creates the package folder and sets the outer class name for the output Java file of the message in the package folder. Developers should use the JAR files below for object management, which is included with the proto compiler download.

  1. protobuf-java-<version of compiler>.jar
  2. protobuf-format-java-1.1.jar

Java Object Management Using Proto Messages

Developers should import the proto compiler's Java output into their Java files. The proto compiler's Java output is called proto Java. This proto Java has an object builder class, which is used to build the message object as well as for storing and retrieving a message in the output file.

As an example, the following code snippet is an employee proto message.

option java_package = "com.emp";
option java_outer_classname = "EmpProtos";

message Employee {
  required string name = 1;
  required int32 id = 2; // Unique ID number for this employee
  optional string email = 3;

message Emprecord {
  repeated Employee emp = 1;

The following code snippet uses a proto Java file for object management, and it writes the employee object into the output file as a serialized form.

package com.emp;
import java.io.*;
import com.emp.EmpProtos.*; //importing the Java proto which is created by proto compiler.

  //writing the employee record into the output file

  public static void addEmployee(int id ,String name,String email,  FileOutputStream fout)  throws Exception {
Emprecord.Builder recBuilder = Emprecord.newBuilder();
Employee.Builder empBuilder = Employee.newBuilder();

The following code snippet is for reading the employee record in the input file.

//Reading the employee record from the file
  public static void readEmprecord(FileInputStream fin) throws Exception {
Emprecord empList =Emprecord.parseFrom(fin);
System.out.println("==Employee List ==");
if(empList != null){
for (Employee emp: empList.getEmpList()) {
   System.out.println(emp.getId()+" "+emp.getName()+" "+getEmail());

Use the above two code snippets as examples that you can apply to your own object management implementation using proto Java files.

Ramasubramanian Thiyagarajan is a senior software engineer involved in the design and development of Java EE applications.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date