Step 4: getting started with Protocol Buffers in Java
Protocol Buffers (protobuf) is a Google technology used to serialize and deserialize structured data, which is often used for network transmission.
This product is actually similar to XML generation and parsing, but protobuf is more efficient than XML, but protobuf generates bytecode, which is less readable than XML. Similar to json and Java Serializable.
Protobuf supports various languages. This article takes Java as an example to briefly introduce how to use protobuf. The usage of other languages is similar.
First download:
Http://download.csdn.net/download/xiao__gui/7586617
There are two files after decompression: protobuf-java-2.5.0.jarand protoc.exe.
The protobuf-java-2.5.0.jar is the jar package required by protobuf. If maven is used, you can ignore this file;
Protoc.exe is a protobuf code generation tool.
Step 1: define the data structure
First, define the data structure of protobuf. here we need to write a. proto file. This file is similar to defining a class. For example, define a Person and save the file PersonMsg. proto (note that the file name should not be the same as the message name in it ).
Message Person {// ID (required) required int32 id = 1; // name (required) required string name = 2; // email (optional) optional string email = 3; // friend (SET) repeated string friends = 4 ;}
The above 1, 2, 3, and 4 are unique numbered tags and are unique identifiers.
The above example defines a very simple data structure. Of course, you can also define a more complex structure. We will not discuss it here. For details, refer to the official documentation.
Step 2: protoc.exe generate Java code
Run the following command using protoc.exe and cmd:
Protoc.exe -- java_out = E: \ java PersonMsg. proto
The input file is PersonMsg. proto, that is, the file that defines the data structure. The output folder is E: \ java, which generates the java file in E: \ java. After the command is run successfully, the PersonMsg. java is generated:
Create a project in Eclipse and copy the java file to the project. The protobuf-java-2.5.0.jar package needs to be introduced in the project. If it is a maven project, add:
<dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>2.5.0</version></dependency>
Step 3: serialization
Step 4: deserialization
In general, serialization and deserialization are separated. For example, during network transmission, one Party sends the data serialized to the other party for receiving and parsing, And the serialization sending and receiving deserialization are not the same. However, the following is a simple example of writing the two in the same program.
Import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. util. list; public class Main {public static void main (String [] args) throws IOException {// create a PersonPersonMsg according to the defined data structure. person. builder personBuilder = PersonMsg. person. newBuilder (); personBuilder. setId (1); personBuilder. setName (""); personBuilder. setEmail ("xxg@163.com"); personBuilder. addFriends ("Friend A"); personBuilder. addFriends ("Friend B"); PersonMsg. person xxg = personBuilder. build (); // write data to the output stream, such as the network output stream. Here ByteArrayOutputStream is used to replace ByteArrayOutputStream output = new ByteArrayOutputStream (); xxg. writeTo (output); // -------------- split line: the above is the sender, which serializes the data and sends --------------- byte [] byteArray = output. toByteArray (); // -------------- split line: Below is the receiver, deserializing the received data --------------- // receives and reads the stream, such as the network input stream, byteArrayInputStream is used to replace ByteArrayInputStream input = new ByteArrayInputStream (byteArray); // deserialize PersonMsg. person xxg2 = PersonMsg. person. parseFrom (input); System. out. println ("ID:" + xxg2.getId (); System. out. println ("name:" + xxg2.getName (); System. out. println ("email:" + xxg2.getEmail (); System. out. println ("friend:"); List <String> friends = xxg2.getFriendsList (); for (String friend: friends) {System. out. println (friend );}}}
Author: Cross brother reprint please indicate the source: http://blog.csdn.net/xiao__gui/article/details/36643949