ProtoBuf, the full name of which is Protocol Buffers, is an efficient and scalable format specification used internally by Google to encode structured data. Google uses ProtoBuf for communication protocols between many internal programs.
The following describes the basic steps for using Java ProtoBuf:
1. http://code.google.com/p/protobuf/downloads/list, select win Version Download
2.download A protobuf-java-2.4.1.jarfile, which must be the same as the proto.exe version you just created. Otherwise, compilation and translation may fail)
Http://grepcode.com/snapshot/repo1.maven.org/maven2/com.google.protobuf/protobuf-java/2.4.1
3.compile a msg. proto file in the directory at the same level as proto.exe:
- package tutorial;
- option java_package = "com.protobuftest.protobuf";
- option java_outer_classname = "PersonProbuf";
- message Person {
- required string name = 1;
- required int32 id = 2;
- optional string email = 3;
- enum PhoneType {
- MOBILE = 0;
- HOME = 1;
- WORK = 2;
- }
- message PhoneNumber {
- required string number = 1;
- optional PhoneType type = 2 [default = HOME];
- }
- repeated PhoneNumber phone = 4;
- message CountryInfo {
- required string name = 1;
- required string code = 2;
- optional int32 number = 3;
- }
- }
- message AddressBook {
- repeated Person person = 1;
- }
4. Use the following command to compile the file:
5. Introduce the generated ProtoBufferPractice. java file to eclipse
6. Introduce the downloaded protobuf-java-2.4.1.jar into the project
7. Usage:
- package com.protobuftest;
-
- import java.util.List;
- import com.google.protobuf.InvalidProtocolBufferException;
- import com.protobuftest.protobuf.PersonProbuf;
- import com.protobuftest.protobuf.PersonProbuf.Person;
- import com.protobuftest.protobuf.PersonProbuf.Person.PhoneNumber;
- import com.protobuftest.protobuf.PersonProbuf.Person.PhoneNumberOrBuilder;
- import com.protobuftest.protobuf.PersonProbuf.Person.PhoneType;
-
- public class ProtoBufTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- PersonProbuf.Person.Builder builder = PersonProbuf.Person.newBuilder();
- builder.setEmail("kkk@email.com");
- builder.setId(1);
- builder.setName("TestName");
- builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("131111111").setType(PersonProbuf.Person.PhoneType.MOBILE));
- builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("011111").setType(PersonProbuf.Person.PhoneType.HOME));
- Person person = builder.build();
- byte[] buf = person.toByteArray();
- try {
- Person person2 = PersonProbuf.Person.parseFrom(buf);
- System.out.println(person2.getName() + ", " + person2.getEmail());
- List<PhoneNumber> lstPhones = person2.getPhoneList();
- for (PhoneNumber phoneNumber : lstPhones) {
- System.out.println(phoneNumber.getNumber());
- }
- } catch (InvalidProtocolBufferException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(buf);
- }
- }
Source Document