zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Leetcode 3. Longest Substring Without Repeating Characters/ 4. Median of Two Sorted Arrays[二分]
    最大流算法思想和理论的简单理解
    数值线性代数实验-共轭梯度法
    运筹学上机实验
    HDU 1542 Atlantis[扫描线]
    数值实验-高斯消元LU分解
    PAT 1143-Lowest Common Ancestor (30) 二叉树还原
    L2-006. 树的遍历
    hdu-3038 How Many Answers Are Wrong[并查集]
    poj-3253 Fence Repair[霍夫曼树]
  • 原文地址:https://www.cnblogs.com/dyufei/p/7466906.html
Copyright © 2011-2022 走看看