Introduction to ProtocolBuffer Learning

Protocol Buffer is an open source serialization and deserialization mechanism similar to xml and JSON parsing, but Protocol Buffer is more flexible, efficient and simple. Protocol buffer is a data exchange format, which is independent of language and platform. Because it is a binary format, it is much faster than using xml for data exchange. It can be used for data communication between distributed applications or data exchange in heterogeneous environments.

Protocol Buffer Download Address: https://github.com/google/protobuf/tree/master/java

This article takes protobuf-3.1.x as a column. This example is downloaded from the official website of pb: http://download.csdn.net/detail/yzr_java/9853141

ProtoBuf's official download package does not contain jar files. Users need to compile it by themselves (this article skips Maven's installation environment and configuration). To compile, they need to download protoc.exe files first, and download them on the official website according to the version of protocolbuffer.

Protoc.exe executor download address: http://central.maven.org/maven2/com/google/protobuf/protoc/

Rename the downloaded protoc files such as protoc-3.1.0-windows-x86_64.exe to protoc.exe and place them in the protobuf-3.1.x/src folder.
Use cmd to enter the directory G: Tools JAVA protocolbuffer protobuf-3.1.x src. If you do not need to validate the compilation results (do not perform unit tests), you can execute mvn install Dmaven.test.skip=true.

According to the official website, the process is as follows:


After execution, protobuf-java-3.1.0.jar is automatically generated under the directory G: Tools JAVA protocolbuffer protobuf-3.1.x java core target.


Serialization and deserialization using protocolbuffer

1. First, you need to create a file with the suffix. proto, which is listed as eboy.proto.

The contents are as follows:

syntax = "proto2";
option java_package = "com.proto";
option java_outer_classname = "UserInfoModule";

message UserInfo{
	required int64 userId = 1;
	
	required string UserName = 2;
	
	repeated Dog dogList = 3;
}

message Dog{
	required int64 dogId = 1;
	
	required string dogName = 2;
}

You can see the difference between syntax for proto2 and proto3 http://www.cppblog.com/tx7do/archive/2016/11/17/214414.html

The data type of protocolbuffer and the grammar of proto file can be referred to as follows: http://blog.csdn.net/sylar_d/article/details/51325987

In the example above, two objects are created, one is UserInfo and the other is Dog. The association between the two objects is that UserInfo has a List < Dog > attribute.

2. Generating java files with protoc.exe

For convenience, we create a proto folder in our project to store the eboy.proto file we just created. Then we create a bat batch file with the contents of

build.bat:

protoc ./proto/eboy.proto --java_out=./src

pause
Put the generated java file into the src of the project. After execution, refresh the project and you will see the automatically generated UserInfoModule.java file

3. Serialization and deserialization using protocolbuffer

package com.proto;

import java.util.Arrays;

import com.proto.UserInfoModule.Dog;
import com.proto.UserInfoModule.UserInfo;
import com.proto.UserInfoModule.UserInfo.Builder;


public class PbToBytes {

	public static void main(String[] args) throws Exception {
		byte[] bytes = toBytes();
		toUserInfo(bytes);

	}
	/**
	 * serialize
	 */
	public static byte[] toBytes(){
		com.proto.UserInfoModule.Dog.Builder dogBuilder = UserInfoModule.Dog.newBuilder();
		dogBuilder.setDogId(1).setDogName("Wangwang");
		Dog dog=dogBuilder.build();
		
		//Get a PBPlayer constructor
		Builder builder = UserInfoModule.UserInfo.newBuilder();
		//Setting up data
		builder.addDogList(dog).setUserId(2011344223).setUserName("YZR");
		//builder.addDogList(UserInfoModule.Dog.newBuilder().setDogId(1).setDogName("Wangwang")).setUserId(2011344223).setUserName("YZR");
		//Construct objects
		UserInfo user = builder.build();
		//Serialize into byte arrays
		byte[] byteArray = user.toByteArray();
		
		System.out.println(Arrays.toString(byteArray));
		
		return byteArray;
	}
	
	/**
	 * Deserialize
	 * @param bs
	 * @throws Exception 
	 */
	public static void toUserInfo(byte[] bs) throws Exception{
		
		 UserInfo user = UserInfoModule.UserInfo.parseFrom(bs);
		 
		 System.out.println("userId:" + user.getUserId());
		 System.out.println("userName:" + user.getUserName());
		 System.out.println("Dog:" + (Arrays.toString(user.getDogListList().toArray())));
	}
}

The information printed in the console is:

[8, -33, -38, -118, -65, 7, 18, 3, 89, 90, 82, 26, 12, 8, 1, 18, 8, 87, 97, 110, 103, 119, 97, 110, 103]
userId:2011344223
userName:YZR
Dog:[dogId: 1
dogName: "Wangwang"
]

We can see that protcolbuffer serializes the UserInfo object into an object of byte data type, and then serializes it into an object through this byte array.


In fact, we can also use java API to serialize objects into an array of byte types. The code is as follows:

package com.java;

import java.io.Serializable;

public class Dog implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8047025244053232753L;
	private int dogId;
	private String dogName;
	public int getDogId() {
		return dogId;
	}
	public void setDogId(int dogId) {
		this.dogId = dogId;
	}
	public String getDogName() {
		return dogName;
	}
	public void setDogName(String dogName) {
		this.dogName = dogName;
	}
	public Dog(int id,String name){
		this.dogId=id;
		this.dogName=name;
	}
	@Override
	public String toString() {
		return "Dog [dogId=" + dogId + ", dogName=" + dogName + "]";
	}
	
}

package com.java;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class UserInfo implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1586269696229279818L;
	private int userId;
	private String userName;
	private List<Dog> dogList=new ArrayList<>();
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public List<Dog> getDogList() {
		return dogList;
	}
	public void setDogList(List<Dog> dogList) {
		this.dogList = dogList;
	}
	public UserInfo(int id,String name){
		this.userId=id;
		this.userName=name;
	}
	
}

package com.java;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

public class JAVA2Bytes {

	public static void main(String[] args) throws Exception {
		byte[] bytes = toBytes();
		toPlayer(bytes);
	}
	
	
	/**
	 * serialize
	 * @throws IOException 
	 */
	public static byte[] toBytes() throws IOException{
		UserInfo user=new UserInfo(2017,"YZR");
		user.getDogList().add(new Dog(1,"WangWang"));
		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		
		//Write object
		objectOutputStream.writeObject(user);
		
		//Get byte arrays
		byte[] byteArray = byteArrayOutputStream.toByteArray();
		System.out.println(Arrays.toString(byteArray));
		return byteArray;
	}
	
	
	/**
	 * Deserialize
	 * @param bs
	 * @throws Exception 
	 */
	public static void toPlayer(byte[] bs) throws Exception{
		
		ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(bs));
		UserInfo user = (UserInfo)inputStream.readObject();
		
		//Printing
		 System.out.println("userId:" + user.getUserId());
		 System.out.println("username:" + user.getUserName());
		 System.out.println("dogs:" + (Arrays.toString(user.getDogList().toArray())));
	}

}

The information printed in the console is as follows:

[-84, -19, 0, 5, 115, 114, 0, 17, 99, 111, 109, 46, 106, 97, 118, 97, 46, 85, 115, 101, 114, 73, 110, 102, 111, -23, -4, 112, 29, -98, 73, 119, -74, 2, 0, 3, 73, 0, 6, 117, 115, 101, 114, 73, 100, 76, 0, 7, 100, 111, 103, 76, 105, 115, 116, 116, 0, 16, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 76, 105, 115, 116, 59, 76, 0, 8, 117, 115, 101, 114, 78, 97, 109, 101, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 120, 112, 0, 0, 7, -31, 115, 114, 0, 19, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114, 97, 121, 76, 105, 115, 116, 120, -127, -46, 29, -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105, 122, 101, 120, 112, 0, 0, 0, 1, 119, 4, 0, 0, 0, 1, 115, 114, 0, 12, 99, 111, 109, 46, 106, 97, 118, 97, 46, 68, 111, 103, 111, -84, -58, -45, 18, 100, -36, 113, 2, 0, 2, 73, 0, 5, 100, 111, 103, 73, 100, 76, 0, 7, 100, 111, 103, 78, 97, 109, 101, 113, 0, 126, 0, 2, 120, 112, 0, 0, 0, 1, 116, 0, 8, 87, 97, 110, 103, 87, 97, 110, 103, 120, 116, 0, 3, 89, 90, 82]
userId:2017
username:YZR
dogs:[Dog [dogId=1, dogName=WangWang]]


Compared with the results of PB serialization above, the size of the byte array serialized in this series is nearly 10 times larger.

The whole project is as follows:

Code download:

http://download.csdn.net/detail/yzr_java/9853180

Keywords: Java xml Google Maven

Added by robburne on Fri, 28 Jun 2019 00:55:41 +0300