天天看點

Protocol Buffer實踐- Android 實作Protocol Buffer實踐- Android 實作

Protocol Buffer實踐- Android 實作

data

say_hi.proto

syntax = "proto2";

package hello_word;

message SayHi {
    required int32 id = 1;
    required string something = 2;
    optional string extra_info = 3;
}
           

compile

protoc -I . --java_out=.  say_hi.proto
           

輸出結果

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: say_hi.proto

package hello_word;

public final class SayHiOuterClass {
  private SayHiOuterClass() {}
  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistryLite registry) {
  }

  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistry registry) {
    registerAllExtensions(
        (com.google.protobuf.ExtensionRegistryLite) registry);
  }
  public interface SayHiOrBuilder extends
      // @@protoc_insertion_point(interface_extends:hello_word.SayHi)
      com.google.protobuf.MessageOrBuilder {

    /**
     * <code>required int32 id = 1;</code>
     * @return Whether the id field is set.
     */
    boolean hasId();
    /**
     * <code>required int32 id = 1;</code>
     * @return The id.
     */
    int getId();

    /**
     * <code>required string something = 2;</code>
     * @return Whether the something field is set.
     */
    boolean hasSomething();
    /**
     * <code>required string something = 2;</code>
     * @return The something.
     */
    java.lang.String getSomething();
    /**
     * <code>required string something = 2;</code>
     * @return The bytes for something.
     */
    com.google.protobuf.ByteString
        getSomethingBytes();

    /**
     * <code>optional string extra_info = 3;</code>
     * @return Whether the extraInfo field is set.
     */
    boolean hasExtraInfo();
    /**
     * <code>optional string extra_info = 3;</code>
     * @return The extraInfo.
     */
    java.lang.String getExtraInfo();
    /**
     * <code>optional string extra_info = 3;</code>
     * @return The bytes for extraInfo.
     */
    com.google.protobuf.ByteString
        getExtraInfoBytes();
  }
  /**
   * Protobuf type {@code hello_word.SayHi}
   */
  public static final class SayHi extends
      com.google.protobuf.GeneratedMessageV3 implements
      // @@protoc_insertion_point(message_implements:hello_word.SayHi)
      SayHiOrBuilder {
  private static final long serialVersionUID = 0L;
    // Use SayHi.newBuilder() to construct.
    private SayHi(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
      super(builder);
    }
    private SayHi() {
      something_ = "";
      extraInfo_ = "";
    }

    @java.lang.Override
    @SuppressWarnings({"unused"})
    protected java.lang.Object newInstance(
        UnusedPrivateParameter unused) {
      return new SayHi();
    }

    @java.lang.Override
    public final com.google.protobuf.UnknownFieldSet
    getUnknownFields() {
      return this.unknownFields;
    }
    private SayHi(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      this();
      if (extensionRegistry == null) {
        throw new java.lang.NullPointerException();
      }
      int mutable_bitField0_ = 0;
      com.google.protobuf.UnknownFieldSet.Builder unknownFields =
          com.google.protobuf.UnknownFieldSet.newBuilder();
      try {
        boolean done = false;
        while (!done) {
          int tag = input.readTag();
          switch (tag) {
            case 0:
              done = true;
              break;
            case 8: {
              bitField0_ |= 0x00000001;
              id_ = input.readInt32();
              break;
            }
            case 18: {
              com.google.protobuf.ByteString bs = input.readBytes();
              bitField0_ |= 0x00000002;
              something_ = bs;
              break;
            }
            case 26: {
              com.google.protobuf.ByteString bs = input.readBytes();
              bitField0_ |= 0x00000004;
              extraInfo_ = bs;
              break;
            }
            default: {
              if (!parseUnknownField(
                  input, unknownFields, extensionRegistry, tag)) {
                done = true;
              }
              break;
            }
          }
        }
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw e.setUnfinishedMessage(this);
      } catch (java.io.IOException e) {
        throw new com.google.protobuf.InvalidProtocolBufferException(
            e).setUnfinishedMessage(this);
      } finally {
        this.unknownFields = unknownFields.build();
        makeExtensionsImmutable();
      }
    }
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return hello_word.SayHiOuterClass.internal_static_hello_word_SayHi_descriptor;
    }

    @java.lang.Override
    protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return hello_word.SayHiOuterClass.internal_static_hello_word_SayHi_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              hello_word.SayHiOuterClass.SayHi.class, hello_word.SayHiOuterClass.SayHi.Builder.class);
    }

    private int bitField0_;
    public static final int ID_FIELD_NUMBER = 1;
    private int id_;
    /**
     * <code>required int32 id = 1;</code>
     * @return Whether the id field is set.
     */
    @java.lang.Override
    public boolean hasId() {
      return ((bitField0_ & 0x00000001) != 0);
    }
    /**
     * <code>required int32 id = 1;</code>
     * @return The id.
     */
    @java.lang.Override
    public int getId() {
      return id_;
    }

    public static final int SOMETHING_FIELD_NUMBER = 2;
    private volatile java.lang.Object something_;
    /**
     * <code>required string something = 2;</code>
     * @return Whether the something field is set.
     */
    @java.lang.Override
    public boolean hasSomething() {
      return ((bitField0_ & 0x00000002) != 0);
    }
    /**
     * <code>required string something = 2;</code>
     * @return The something.
     */
    @java.lang.Override
    public java.lang.String getSomething() {
      java.lang.Object ref = something_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          something_ = s;
        }
        return s;
      }
    }
    /**
     * <code>required string something = 2;</code>
     * @return The bytes for something.
     */
    @java.lang.Override
    public com.google.protobuf.ByteString
        getSomethingBytes() {
      java.lang.Object ref = something_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        something_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    public static final int EXTRA_INFO_FIELD_NUMBER = 3;
    private volatile java.lang.Object extraInfo_;
    /**
     * <code>optional string extra_info = 3;</code>
     * @return Whether the extraInfo field is set.
     */
    @java.lang.Override
    public boolean hasExtraInfo() {
      return ((bitField0_ & 0x00000004) != 0);
    }
    /**
     * <code>optional string extra_info = 3;</code>
     * @return The extraInfo.
     */
    @java.lang.Override
    public java.lang.String getExtraInfo() {
      java.lang.Object ref = extraInfo_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          extraInfo_ = s;
        }
        return s;
      }
    }
    /**
     * <code>optional string extra_info = 3;</code>
     * @return The bytes for extraInfo.
     */
    @java.lang.Override
    public com.google.protobuf.ByteString
        getExtraInfoBytes() {
      java.lang.Object ref = extraInfo_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        extraInfo_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    private byte memoizedIsInitialized = -1;
    @java.lang.Override
    public final boolean isInitialized() {
      byte isInitialized = memoizedIsInitialized;
      if (isInitialized == 1) return true;
      if (isInitialized == 0) return false;

      if (!hasId()) {
        memoizedIsInitialized = 0;
        return false;
      }
      if (!hasSomething()) {
        memoizedIsInitialized = 0;
        return false;
      }
      memoizedIsInitialized = 1;
      return true;
    }

    @java.lang.Override
    public void writeTo(com.google.protobuf.CodedOutputStream output)
                        throws java.io.IOException {
      if (((bitField0_ & 0x00000001) != 0)) {
        output.writeInt32(1, id_);
      }
      if (((bitField0_ & 0x00000002) != 0)) {
        com.google.protobuf.GeneratedMessageV3.writeString(output, 2, something_);
      }
      if (((bitField0_ & 0x00000004) != 0)) {
        com.google.protobuf.GeneratedMessageV3.writeString(output, 3, extraInfo_);
      }
      unknownFields.writeTo(output);
    }

    @java.lang.Override
    public int getSerializedSize() {
      int size = memoizedSize;
      if (size != -1) return size;

      size = 0;
      if (((bitField0_ & 0x00000001) != 0)) {
        size += com.google.protobuf.CodedOutputStream
          .computeInt32Size(1, id_);
      }
      if (((bitField0_ & 0x00000002) != 0)) {
        size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, something_);
      }
      if (((bitField0_ & 0x00000004) != 0)) {
        size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, extraInfo_);
      }
      size += unknownFields.getSerializedSize();
      memoizedSize = size;
      return size;
    }

    @java.lang.Override
    public boolean equals(final java.lang.Object obj) {
      if (obj == this) {
       return true;
      }
      if (!(obj instanceof hello_word.SayHiOuterClass.SayHi)) {
        return super.equals(obj);
      }
      hello_word.SayHiOuterClass.SayHi other = (hello_word.SayHiOuterClass.SayHi) obj;

      if (hasId() != other.hasId()) return false;
      if (hasId()) {
        if (getId()
            != other.getId()) return false;
      }
      if (hasSomething() != other.hasSomething()) return false;
      if (hasSomething()) {
        if (!getSomething()
            .equals(other.getSomething())) return false;
      }
      if (hasExtraInfo() != other.hasExtraInfo()) return false;
      if (hasExtraInfo()) {
        if (!getExtraInfo()
            .equals(other.getExtraInfo())) return false;
      }
      if (!unknownFields.equals(other.unknownFields)) return false;
      return true;
    }

    @java.lang.Override
    public int hashCode() {
      if (memoizedHashCode != 0) {
        return memoizedHashCode;
      }
      int hash = 41;
      hash = (19 * hash) + getDescriptor().hashCode();
      if (hasId()) {
        hash = (37 * hash) + ID_FIELD_NUMBER;
        hash = (53 * hash) + getId();
      }
      if (hasSomething()) {
        hash = (37 * hash) + SOMETHING_FIELD_NUMBER;
        hash = (53 * hash) + getSomething().hashCode();
      }
      if (hasExtraInfo()) {
        hash = (37 * hash) + EXTRA_INFO_FIELD_NUMBER;
        hash = (53 * hash) + getExtraInfo().hashCode();
      }
      hash = (29 * hash) + unknownFields.hashCode();
      memoizedHashCode = hash;
      return hash;
    }

    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        java.nio.ByteBuffer data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        java.nio.ByteBuffer data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        com.google.protobuf.ByteString data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        com.google.protobuf.ByteString data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(byte[] data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        byte[] data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(java.io.InputStream input)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input, extensionRegistry);
    }
    public static hello_word.SayHiOuterClass.SayHi parseDelimitedFrom(java.io.InputStream input)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseDelimitedWithIOException(PARSER, input);
    }
    public static hello_word.SayHiOuterClass.SayHi parseDelimitedFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        com.google.protobuf.CodedInputStream input)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input);
    }
    public static hello_word.SayHiOuterClass.SayHi parseFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input, extensionRegistry);
    }

    @java.lang.Override
    public Builder newBuilderForType() { return newBuilder(); }
    public static Builder newBuilder() {
      return DEFAULT_INSTANCE.toBuilder();
    }
    public static Builder newBuilder(hello_word.SayHiOuterClass.SayHi prototype) {
      return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
    }
    @java.lang.Override
    public Builder toBuilder() {
      return this == DEFAULT_INSTANCE
          ? new Builder() : new Builder().mergeFrom(this);
    }

    @java.lang.Override
    protected Builder newBuilderForType(
        com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
      Builder builder = new Builder(parent);
      return builder;
    }
    /**
     * Protobuf type {@code hello_word.SayHi}
     */
    public static final class Builder extends
        com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
        // @@protoc_insertion_point(builder_implements:hello_word.SayHi)
        hello_word.SayHiOuterClass.SayHiOrBuilder {
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return hello_word.SayHiOuterClass.internal_static_hello_word_SayHi_descriptor;
      }

      @java.lang.Override
      protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return hello_word.SayHiOuterClass.internal_static_hello_word_SayHi_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                hello_word.SayHiOuterClass.SayHi.class, hello_word.SayHiOuterClass.SayHi.Builder.class);
      }

      // Construct using hello_word.SayHiOuterClass.SayHi.newBuilder()
      private Builder() {
        maybeForceBuilderInitialization();
      }

      private Builder(
          com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
        super(parent);
        maybeForceBuilderInitialization();
      }
      private void maybeForceBuilderInitialization() {
        if (com.google.protobuf.GeneratedMessageV3
                .alwaysUseFieldBuilders) {
        }
      }
      @java.lang.Override
      public Builder clear() {
        super.clear();
        id_ = 0;
        bitField0_ = (bitField0_ & ~0x00000001);
        something_ = "";
        bitField0_ = (bitField0_ & ~0x00000002);
        extraInfo_ = "";
        bitField0_ = (bitField0_ & ~0x00000004);
        return this;
      }

      @java.lang.Override
      public com.google.protobuf.Descriptors.Descriptor
          getDescriptorForType() {
        return hello_word.SayHiOuterClass.internal_static_hello_word_SayHi_descriptor;
      }

      @java.lang.Override
      public hello_word.SayHiOuterClass.SayHi getDefaultInstanceForType() {
        return hello_word.SayHiOuterClass.SayHi.getDefaultInstance();
      }

      @java.lang.Override
      public hello_word.SayHiOuterClass.SayHi build() {
        hello_word.SayHiOuterClass.SayHi result = buildPartial();
        if (!result.isInitialized()) {
          throw newUninitializedMessageException(result);
        }
        return result;
      }

      @java.lang.Override
      public hello_word.SayHiOuterClass.SayHi buildPartial() {
        hello_word.SayHiOuterClass.SayHi result = new hello_word.SayHiOuterClass.SayHi(this);
        int from_bitField0_ = bitField0_;
        int to_bitField0_ = 0;
        if (((from_bitField0_ & 0x00000001) != 0)) {
          result.id_ = id_;
          to_bitField0_ |= 0x00000001;
        }
        if (((from_bitField0_ & 0x00000002) != 0)) {
          to_bitField0_ |= 0x00000002;
        }
        result.something_ = something_;
        if (((from_bitField0_ & 0x00000004) != 0)) {
          to_bitField0_ |= 0x00000004;
        }
        result.extraInfo_ = extraInfo_;
        result.bitField0_ = to_bitField0_;
        onBuilt();
        return result;
      }

      @java.lang.Override
      public Builder clone() {
        return super.clone();
      }
      @java.lang.Override
      public Builder setField(
          com.google.protobuf.Descriptors.FieldDescriptor field,
          java.lang.Object value) {
        return super.setField(field, value);
      }
      @java.lang.Override
      public Builder clearField(
          com.google.protobuf.Descriptors.FieldDescriptor field) {
        return super.clearField(field);
      }
      @java.lang.Override
      public Builder clearOneof(
          com.google.protobuf.Descriptors.OneofDescriptor oneof) {
        return super.clearOneof(oneof);
      }
      @java.lang.Override
      public Builder setRepeatedField(
          com.google.protobuf.Descriptors.FieldDescriptor field,
          int index, java.lang.Object value) {
        return super.setRepeatedField(field, index, value);
      }
      @java.lang.Override
      public Builder addRepeatedField(
          com.google.protobuf.Descriptors.FieldDescriptor field,
          java.lang.Object value) {
        return super.addRepeatedField(field, value);
      }
      @java.lang.Override
      public Builder mergeFrom(com.google.protobuf.Message other) {
        if (other instanceof hello_word.SayHiOuterClass.SayHi) {
          return mergeFrom((hello_word.SayHiOuterClass.SayHi)other);
        } else {
          super.mergeFrom(other);
          return this;
        }
      }

      public Builder mergeFrom(hello_word.SayHiOuterClass.SayHi other) {
        if (other == hello_word.SayHiOuterClass.SayHi.getDefaultInstance()) return this;
        if (other.hasId()) {
          setId(other.getId());
        }
        if (other.hasSomething()) {
          bitField0_ |= 0x00000002;
          something_ = other.something_;
          onChanged();
        }
        if (other.hasExtraInfo()) {
          bitField0_ |= 0x00000004;
          extraInfo_ = other.extraInfo_;
          onChanged();
        }
        this.mergeUnknownFields(other.unknownFields);
        onChanged();
        return this;
      }

      @java.lang.Override
      public final boolean isInitialized() {
        if (!hasId()) {
          return false;
        }
        if (!hasSomething()) {
          return false;
        }
        return true;
      }

      @java.lang.Override
      public Builder mergeFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        hello_word.SayHiOuterClass.SayHi parsedMessage = null;
        try {
          parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          parsedMessage = (hello_word.SayHiOuterClass.SayHi) e.getUnfinishedMessage();
          throw e.unwrapIOException();
        } finally {
          if (parsedMessage != null) {
            mergeFrom(parsedMessage);
          }
        }
        return this;
      }
      private int bitField0_;

      private int id_ ;
      /**
       * <code>required int32 id = 1;</code>
       * @return Whether the id field is set.
       */
      @java.lang.Override
      public boolean hasId() {
        return ((bitField0_ & 0x00000001) != 0);
      }
      /**
       * <code>required int32 id = 1;</code>
       * @return The id.
       */
      @java.lang.Override
      public int getId() {
        return id_;
      }
      /**
       * <code>required int32 id = 1;</code>
       * @param value The id to set.
       * @return This builder for chaining.
       */
      public Builder setId(int value) {
        bitField0_ |= 0x00000001;
        id_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>required int32 id = 1;</code>
       * @return This builder for chaining.
       */
      public Builder clearId() {
        bitField0_ = (bitField0_ & ~0x00000001);
        id_ = 0;
        onChanged();
        return this;
      }

      private java.lang.Object something_ = "";
      /**
       * <code>required string something = 2;</code>
       * @return Whether the something field is set.
       */
      public boolean hasSomething() {
        return ((bitField0_ & 0x00000002) != 0);
      }
      /**
       * <code>required string something = 2;</code>
       * @return The something.
       */
      public java.lang.String getSomething() {
        java.lang.Object ref = something_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          if (bs.isValidUtf8()) {
            something_ = s;
          }
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>required string something = 2;</code>
       * @return The bytes for something.
       */
      public com.google.protobuf.ByteString
          getSomethingBytes() {
        java.lang.Object ref = something_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          something_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>required string something = 2;</code>
       * @param value The something to set.
       * @return This builder for chaining.
       */
      public Builder setSomething(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000002;
        something_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>required string something = 2;</code>
       * @return This builder for chaining.
       */
      public Builder clearSomething() {
        bitField0_ = (bitField0_ & ~0x00000002);
        something_ = getDefaultInstance().getSomething();
        onChanged();
        return this;
      }
      /**
       * <code>required string something = 2;</code>
       * @param value The bytes for something to set.
       * @return This builder for chaining.
       */
      public Builder setSomethingBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000002;
        something_ = value;
        onChanged();
        return this;
      }

      private java.lang.Object extraInfo_ = "";
      /**
       * <code>optional string extra_info = 3;</code>
       * @return Whether the extraInfo field is set.
       */
      public boolean hasExtraInfo() {
        return ((bitField0_ & 0x00000004) != 0);
      }
      /**
       * <code>optional string extra_info = 3;</code>
       * @return The extraInfo.
       */
      public java.lang.String getExtraInfo() {
        java.lang.Object ref = extraInfo_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          if (bs.isValidUtf8()) {
            extraInfo_ = s;
          }
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>optional string extra_info = 3;</code>
       * @return The bytes for extraInfo.
       */
      public com.google.protobuf.ByteString
          getExtraInfoBytes() {
        java.lang.Object ref = extraInfo_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          extraInfo_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>optional string extra_info = 3;</code>
       * @param value The extraInfo to set.
       * @return This builder for chaining.
       */
      public Builder setExtraInfo(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000004;
        extraInfo_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional string extra_info = 3;</code>
       * @return This builder for chaining.
       */
      public Builder clearExtraInfo() {
        bitField0_ = (bitField0_ & ~0x00000004);
        extraInfo_ = getDefaultInstance().getExtraInfo();
        onChanged();
        return this;
      }
      /**
       * <code>optional string extra_info = 3;</code>
       * @param value The bytes for extraInfo to set.
       * @return This builder for chaining.
       */
      public Builder setExtraInfoBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000004;
        extraInfo_ = value;
        onChanged();
        return this;
      }
      @java.lang.Override
      public final Builder setUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return super.setUnknownFields(unknownFields);
      }

      @java.lang.Override
      public final Builder mergeUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return super.mergeUnknownFields(unknownFields);
      }


      // @@protoc_insertion_point(builder_scope:hello_word.SayHi)
    }

    // @@protoc_insertion_point(class_scope:hello_word.SayHi)
    private static final hello_word.SayHiOuterClass.SayHi DEFAULT_INSTANCE;
    static {
      DEFAULT_INSTANCE = new hello_word.SayHiOuterClass.SayHi();
    }

    public static hello_word.SayHiOuterClass.SayHi getDefaultInstance() {
      return DEFAULT_INSTANCE;
    }

    @java.lang.Deprecated public static final com.google.protobuf.Parser<SayHi>
        PARSER = new com.google.protobuf.AbstractParser<SayHi>() {
      @java.lang.Override
      public SayHi parsePartialFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return new SayHi(input, extensionRegistry);
      }
    };

    public static com.google.protobuf.Parser<SayHi> parser() {
      return PARSER;
    }

    @java.lang.Override
    public com.google.protobuf.Parser<SayHi> getParserForType() {
      return PARSER;
    }

    @java.lang.Override
    public hello_word.SayHiOuterClass.SayHi getDefaultInstanceForType() {
      return DEFAULT_INSTANCE;
    }

  }

  private static final com.google.protobuf.Descriptors.Descriptor
    internal_static_hello_word_SayHi_descriptor;
  private static final 
    com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
      internal_static_hello_word_SayHi_fieldAccessorTable;

  public static com.google.protobuf.Descriptors.FileDescriptor
      getDescriptor() {
    return descriptor;
  }
  private static  com.google.protobuf.Descriptors.FileDescriptor
      descriptor;
  static {
    java.lang.String[] descriptorData = {
      "\n\014say_hi.proto\022\nhello_word\":\n\005SayHi\022\n\n\002i" +
      "d\030\001 \002(\005\022\021\n\tsomething\030\002 \002(\t\022\022\n\nextra_info" +
      "\030\003 \001(\t"
    };
    descriptor = com.google.protobuf.Descriptors.FileDescriptor
      .internalBuildGeneratedFileFrom(descriptorData,
        new com.google.protobuf.Descriptors.FileDescriptor[] {
        });
    internal_static_hello_word_SayHi_descriptor =
      getDescriptor().getMessageTypes().get(0);
    internal_static_hello_word_SayHi_fieldAccessorTable = new
      com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
        internal_static_hello_word_SayHi_descriptor,
        new java.lang.String[] { "Id", "Something", "ExtraInfo", });
  }

  // @@protoc_insertion_point(outer_class_scope)
}

           

Android中的使用

Gradle

If you are using Gradle, add the following to your

build.gradle

file’s dependencies:

implementation 'com.google.protobuf:protobuf-java:3.15.3'
           

接收socket

/**
     * 處理資料
     *
     * @param inputStream
     */
    public void handlerCmd(ByteBufInputStream inputStream) {
        try {
            //synchronized (CmdHandler.class) {
	 				handProcessCmd(protoId, inputStream);
                }
           // }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void handProcessModel(int protoId, ByteBufInputStream inputStream) {
        try {
            switch (protoId) {
                case PrototocalTools.IProtoClientIndex.call_phone_state: // 已完成
                    CallPhoneStateProtos.CallPhoneState callPhoneState = CallPhoneStateProtos.CallPhoneState.parseDelimitedFrom(inputStream);
                    pasreCallPhoneState(protoId, callPhoneState);
                    break;
                    ...
                    }
               ....
        }
     
    /**
     * 解析電話狀态并發送到ui
     *
     * @param protoId
     * @param callPhoneState
     */
    private void pasreCallPhoneState(int protoId, CallPhoneStateProtos.CallPhoneState callPhoneState) {
      
            CallPhoneStateProtos.CallPhoneState.PhoneState state = callPhoneState.getPhoneState();
            if(currentPhoneState != null && state != null){
                LogUtils.i("currentPhoneState = " + currentPhoneState.ordinal() + "    state = " + state.ordinal());
            }   
             
    }

    ```