日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Protocol Buffers java

發布時間:2025/7/14 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Protocol Buffers java 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Protocol Buffers

https://developers.google.cn/protocol-buffers/

一. 例

addressbook.proto.syntax = "proto2";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 phones = 4; }message AddressBook {repeated Person people = 1; }

二.編譯

protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto

三. 生成文件的常用接口

Person class (implementations omitted for brevity):// required string name = 1; public boolean hasName(); public String getName();// required int32 id = 2; public boolean hasId(); public int getId();// optional string email = 3; public boolean hasEmail(); public String getEmail();// repeated .tutorial.Person.PhoneNumber phones = 4; public List<PhoneNumber> getPhonesList(); public int getPhonesCount(); public PhoneNumber getPhones(int index);

四. Builders vs. Parsing

Here's an example of how you would create an instance of Person:

Person john =Person.newBuilder().setId(1234).setName("John Doe").setEmail("jdoe@example.com").addPhones(Person.PhoneNumber.newBuilder().setNumber("555-4321").setType(Person.PhoneType.HOME)).build();

Parsing

Person john; fstream input(argv[1],ios::in | ios::binary); john.ParseFromIstream(&input); id = john.id(); name = john.name(); email = john.email();

五.Standard Message Methods

Each message and builder class also contains a number of other methods that let you check or manipulate the entire message, including:

isInitialized(): checks if all the required fields have been set.
toString(): returns a human-readable representation of the message, particularly useful for debugging.
mergeFrom(Message other): (builder only) merges the contents of other into this message, overwriting singular scalar fields, merging composite fields, and concatenating repeated fields.
clear(): (builder only) clears all the fields back to the empty state.
These methods implement the Message and Message.Builder interfaces shared by all Java messages and builders. For more information, see the complete API documentation for Message.

六.Parsing and Serialization

Finally, each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:

byte[] toByteArray();: serializes the message and returns a byte array containing its raw bytes.
static Person parseFrom(byte[] data);: parses a message from the given byte array.
void writeTo(OutputStream output);: serializes the message and writes it to an OutputStream.
static Person parseFrom(InputStream input);: reads and parses a message from an InputStream.
These are just a couple of the options provided for parsing and serialization. Again, see the Message API reference for a complete list.

總結

以上是生活随笔為你收集整理的Protocol Buffers java的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。