Preliminary use of protobuf

Source: Internet
Author: User

 

Java Tutorial: http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/javatutorial.html
Environment preparation:
(1) download protoc code generator and source package: http://code.google.com/p/protobuf/downloads/list
Protobuf-2.4.1.tar.bz2
Protoc-2.4.1-win32.zip
(2zip decompress protoc-2.4.1-win32.zip to D: \ doc \ protobuf \ protoc-2.4.1-win32and add protoc.exe to Path Environment Variable
(3)decompress protobuf-2.4.1.tar.bz2 to D: \ doc \ protobuf \ protobuf-2.4.1,copy protoc.exe to D: \ doc \ protobuf \ protobuf-2.4.1 \ SRC,
Enter the D: \ doc \ protobuf \ protobuf-2.4.1 \ Java directory, and then execute: MVN-install, will be in D: the protobuf-2.4.1 package is generated under the target directory under the \ doc \ protobuf \ protobuf-java-2.4.1.jar \ Java directory.
[Note] Maven must be installed on the machine.
(4) Add the protobuf-java-2.4.1.jar to classpath.

In D: \ doc \ protobuf \ protobuf-2.4.1 \ examples contains a demo:
(1) switch to D: \ doc \ protobuf \ protobuf-2.4.1 \ examples, you can see there is a addressbook. proto
Package tutorial;
Option java_package = "com. example. Tutorial ";
Option java_outer_classname = "addressbookprotos ";

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 addressbook {
Repeated person = 1;
}
Command line switch to D: \ doc \ protobuf \ protobuf-2.4.1 \ examples, execute: protoc -- java_out =. addressbook. proto
COM \ example \ tutorial \ addressbookprotos. Java is generated under the D: \ doc \ protobuf \ protobuf-2.4.1 \ examples directory
Continue: javac-cp d: \ doc \ protobuf \ protobuf-2.4.1 \ examples \ protobuf-java-2.4.1.jar com \ example \ tutorial \ addressbookprotos. Java
A large number of class files are generated, all of which are internal classes.
(2) In the D: \ doc \ protobuf \ protobuf-2.4.1 \ examples directory, there is a addperson. Java
Import com. example. Tutorial. addressbookprotos. addressbook;
Import com. example. Tutorial. addressbookprotos. person;
Import java. Io. bufferedreader;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. inputstreamreader;
Import java. Io. ioexception;
Import java. Io. printstream;
Class addperson {// This function fills in a person message based on user
// Input.
Static person promptforaddress (bufferedreader stdin, printstream stdout)
Throws ioexception {
Person. Builder person = person. newbuilder ();
Stdout. Print ("Enter person ID :");
Person. setid (integer. valueof (stdin. Readline ()));
Stdout. Print ("Enter name :");
Person. setname (stdin. Readline ());
Stdout. Print ("enter email address (blank for none ):");
String email = stdin. Readline ();
If (email. Length ()> 0 ){
Person. setemail (email );
}
While (true ){
Stdout. Print ("enter a phone number (or leave blank to finish ):");
String number = stdin. Readline ();
If (number. Length () = 0 ){
Break;
}
Person. phonenumber. Builder phonenumber = person. phonenumber
. Newbuilder (). setnumber (number );
Stdout. Print ("Is this a mobile, home, or work phone? ");
String type = stdin. Readline ();
If (type. Equals ("mobile ")){
Phonenumber. settype (person. phonetype. Mobile );
} Else if (type. Equals ("home ")){
Phonenumber. settype (person. phonetype. Home );
} Else if (type. Equals ("work ")){
Phonenumber. settype (person. phonetype. Work );
} Else {
Stdout. println ("unknown phone type. Using default .");
}
Person. addphone (phonenumber );
}
Return person. Build ();
}

// Main function: reads the entire address book from a file,
// Adds one person based on user input, then writes it back out to the same
// File.
Public static void main (string [] ARGs) throws exception {
If (ARGs. length! = 1 ){
System. Err. println ("Usage: addperson address_book_file ");
System. Exit (-1 );
}
Addressbook. Builder addressbook = addressbook. newbuilder ();
// Read the existing address book.
Try {
Addressbook. mergefrom (New fileinputstream (ARGs [0]);
} Catch (filenotfoundexception e ){
System. Out. println (ARGs [0]
+ ": File not found. Creating a new file .");
}
// Add an address.
Addressbook. addperson (promptforaddress (New bufferedreader (
New inputstreamreader (system. In), system. Out ));
// Write the new address book back to disk.
Fileoutputstream output = new fileoutputstream (ARGs [0]);
Addressbook. Build (). writeto (output );
Output. Close ();
}
}
Enter: javac-cp d: \ doc \ protobuf \ protobuf-2.4.1 \ examples \ protobuf-java-2.4.1.jar; D: \ doc \ protobuf \ protobuf-2.4.1 \ examples addperson. Java
Generated under D: \ doc \ protobuf \ protobuf-2.4.1 \ examples: addperson. Class
In the command line enter: Java-CP javac-cp d: \ doc \ protobuf \ protobuf-2.4.1 \ examples \ protobuf-java-2.4.1.jar; D: \ doc \ protobuf \ protobuf-2.4.1 \ examples addperson 1.txt
Command Line prompt:
1. txt: file not found. Creating a new file.
Enter person ID: 1
Enter name: xujsh
Enter email address (blank for none): xjs@163.com
Enter a phone number (or leave blank to finish): 13612341234
Is this a mobile, home, or work phone? Mobile
Enter a phone number (or leave blank to finish): 01012345678
Is this a mobile, home, or work phone? Home
Enter a phone number (or leave blank to finish ):

Generate: 1.txt under D: \ doc \ protobuf \ protobuf-2.4.1 \ examples

(3) In the D: \ doc \ protobuf \ protobuf-2.4.1 \ examples directory, there is a listpeople. Java
Import java. Io. fileinputstream;
Import com. example. Tutorial. addressbookprotos. addressbook;
Import com. example. Tutorial. addressbookprotos. person;

Class listpeople {
// Iterates though all people in the addressbook and prints
// Info about them.
Static void print (addressbook ){
For (person: addressbook. getpersonlist ()){
System. Out. println ("person ID:" + person. GETID ());
System. Out. println ("name:" + person. getname ());
If (person. hasemail ()){
System. Out. println ("e-mail address:" + person. getemail ());
}
For (person. phonenumber: person. getphonelist ()){
Switch (phonenumber. GetType ()){
Case Mobile:
System. Out. Print ("Mobile Phone #:");
Break;
Case home:
System. Out. Print ("Home Phone #:");
Break;
Case Work:
System. Out. Print ("work phone #:");
Break;
}
System. Out. println (phonenumber. getnumber ());
}
}
}

// Main function: reads the entire address book from a file and prints all
// The information inside.
Public static void main (string [] ARGs) throws exception {
If (ARGs. length! = 1 ){
System. Err. println ("Usage: listpeople address_book_file ");
System. Exit (-1 );
}

// Read the existing address book.
Addressbook = addressbook. parsefrom (New fileinputstream (
ARGs [0]);
Print (addressbook );
}
}
Enter: javac-cp d: \ doc \ protobuf \ protobuf-2.4.1 \ examples \ protobuf-java-2.4.1.jar; D: \ doc \ protobuf \ protobuf-2.4.1 \ examples listpeople. Java
Generated under D: \ doc \ protobuf \ protobuf-2.4.1 \ examples: listpeople. Class
In the command line enter: Java-CP javac-cp d: \ doc \ protobuf \ protobuf-2.4.1 \ examples \ protobuf-java-2.4.1.jar; D: \ doc \ protobuf \ protobuf-2.4.1 \ examples listpeople 1.txt
Output result:
Person ID: 1
Name: xujsh
E-mail address: xjs@163.com
Mobile Phone #: 13612341234
Home Phone #: 01012345678

 

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.