Java protobuf framework usage wizard

Source: Internet
Author: User

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:

 
  1. package tutorial;   
  2. option java_package = "com.protobuftest.protobuf";   
  3. option java_outer_classname = "PersonProbuf";   
  4. message Person {   
  5.   required string name = 1;   
  6.   required int32 id = 2;   
  7.   optional string email = 3;   
  8.   enum PhoneType {   
  9.     MOBILE = 0;   
  10.     HOME = 1;   
  11.     WORK = 2;   
  12.   }   
  13.   message PhoneNumber {   
  14.     required string number = 1;   
  15.     optional PhoneType type = 2 [default = HOME];   
  16.   }   
  17.   repeated PhoneNumber phone = 4;   
  18.   message CountryInfo {  
  19.           required string name = 1;  
  20.           required string code = 2;  
  21.           optional int32 number = 3;  
  22.   }  
  23. }   
  24. message AddressBook {   
  25.   repeated Person person = 1;   
  26. }  

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:

 
  1. package com.protobuftest;  
  2.  
  3. import java.util.List;  
  4. import com.google.protobuf.InvalidProtocolBufferException;  
  5. import com.protobuftest.protobuf.PersonProbuf;  
  6. import com.protobuftest.protobuf.PersonProbuf.Person;  
  7. import com.protobuftest.protobuf.PersonProbuf.Person.PhoneNumber;  
  8. import com.protobuftest.protobuf.PersonProbuf.Person.PhoneNumberOrBuilder;  
  9. import com.protobuftest.protobuf.PersonProbuf.Person.PhoneType;  
  10.  
  11. public class ProtoBufTest {  
  12. /**  
  13.  * @param args  
  14.  */ 
  15. public static void main(String[] args) {  
  16. // TODO Auto-generated method stub  
  17. PersonProbuf.Person.Builder builder = PersonProbuf.Person.newBuilder();  
  18. builder.setEmail("kkk@email.com");  
  19. builder.setId(1);  
  20. builder.setName("TestName");  
  21. builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("131111111").setType(PersonProbuf.Person.PhoneType.MOBILE));  
  22. builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("011111").setType(PersonProbuf.Person.PhoneType.HOME));  
  23. Person person = builder.build();  
  24. byte[] buf = person.toByteArray();  
  25. try {  
  26. Person person2 = PersonProbuf.Person.parseFrom(buf);  
  27. System.out.println(person2.getName() + ", " + person2.getEmail());  
  28. List<PhoneNumber> lstPhones = person2.getPhoneList();  
  29. for (PhoneNumber phoneNumber : lstPhones) {  
  30. System.out.println(phoneNumber.getNumber());  
  31. }  
  32. } catch (InvalidProtocolBufferException e) {  
  33. // TODO Auto-generated catch block  
  34. e.printStackTrace();  
  35. }  
  36. System.out.println(buf);  
  37. }  
  38. } 

Source Document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.