protoc.exe 工 具 主 要 根 据 .prot 。 文 件 生 成 代 码 , 下 面 我 们 以 商 品 订 购 例 程 为 例 , 定 义
SubscribeReq.proto 和 SubscribeResp.proto , 数 据 文 件 定 义 如 下 。
SubscribeReq.proto
syntax = "proto3";
option java_package= "com.viagra.chapter08.protobuf";
option java_outer_classname= "SubscribeReqProto";
message SubcribeReq {
int32 subReqID = 1;
string userName = 2;
string productName = 3;
repeated string address = 4;
}
SubscribeResp.proto
syntax = "proto3";
option java_package= "com.viagra.chapter08.protobuf";
option java_outer_classname= "SubscribeRespProto";
message SubcribeResp {
int32 subReqID = 1;
int32 respCode = 2;
string desc = 3;
}
通过命令生成对应的实体类:
SubscribeReqProto.java
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: SubscribeReq.proto
package com.viagra.chapter08.protobuf;
public final class SubscribeReqProto {
private SubscribeReqProto() {}
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 SubscribeReqOrBuilder extends
// @@protoc_insertion_point(interface_extends:SubscribeReq)
com.google.protobuf.MessageOrBuilder {
/**
* <code>required int32 subReqID = 1;</code>
*/
boolean hasSubReqID();
/**
* <code>required int32 subReqID = 1;</code>
*/
int getSubReqID();
/**
* <code>required string userName = 2;</code>
*/
boolean hasUserName();
/**
* <code>required string userName = 2;</code>
*/
String getUserName();
/**
* <code>required string userName = 2;</code>
*/
com.google.protobuf.ByteString
getUserNameBytes();
/**
* <code>required string productName = 3;</code>
*/
boolean hasProductName();
/**
* <code>required string productName = 3;</code>
*/
String getProductName();
/**
* <code>required string productName = 3;</code>
*/
com.google.protobuf.ByteString
getProductNameBytes();
/**
* <code>repeated string address = 4;</code>
*/
java.util.List<String>
getAddressList();
/**
* <code>repeated string address = 4;</code>
*/
int getAddressCount();
/**
* <code>repeated string address = 4;</code>
*/
String getAddress(int index);
/**
* <code>repeated string address = 4;</code>
*/
com.google.protobuf.ByteString
getAddressBytes(int index);
}
/**
* Protobuf type {@code SubscribeReq}
*/
public static final class SubscribeReq extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:SubscribeReq)
SubscribeReqOrBuilder {
private static final long serialVersionUID = 0L;
// Use SubscribeReq.newBuilder() to construct.
private SubscribeReq(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private SubscribeReq() {
userName_ = "";
productName_ = "";
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
}
@Override
@SuppressWarnings({"unused"})
protected Object newInstance(
UnusedPrivateParameter unused) {
return new SubscribeReq();
}
@Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private SubscribeReq(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
this();
if (extensionRegistry == null) {
throw new 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;
subReqID_ = input.readInt32();
break;
}
case 18: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000002;
userName_ = bs;
break;
}
case 26: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000004;
productName_ = bs;
break;
}
case 34: {
com.google.protobuf.ByteString bs = input.readBytes();
if (!((mutable_bitField0_ & 0x00000008) != 0)) {
address_ = new com.google.protobuf.LazyStringArrayList();
mutable_bitField0_ |= 0x00000008;
}
address_.add(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 {
if (((mutable_bitField0_ & 0x00000008) != 0)) {
address_ = address_.getUnmodifiableView();
}
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return SubscribeReqProto.internal_static_SubscribeReq_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeReqProto.internal_static_SubscribeReq_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeReq.class, Builder.class);
}
private int bitField0_;
public static final int SUBREQID_FIELD_NUMBER = 1;
private int subReqID_;
/**
* <code>required int32 subReqID = 1;</code>
*/
public boolean hasSubReqID() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
}
public static final int USERNAME_FIELD_NUMBER = 2;
private volatile Object userName_;
/**
* <code>required string userName = 2;</code>
*/
public boolean hasUserName() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>required string userName = 2;</code>
*/
public String getUserName() {
Object ref = userName_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
userName_ = s;
}
return s;
}
}
/**
* <code>required string userName = 2;</code>
*/
public com.google.protobuf.ByteString
getUserNameBytes() {
Object ref = userName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
userName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int PRODUCTNAME_FIELD_NUMBER = 3;
private volatile Object productName_;
/**
* <code>required string productName = 3;</code>
*/
public boolean hasProductName() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>required string productName = 3;</code>
*/
public String getProductName() {
Object ref = productName_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
productName_ = s;
}
return s;
}
}
/**
* <code>required string productName = 3;</code>
*/
public com.google.protobuf.ByteString
getProductNameBytes() {
Object ref = productName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
productName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int ADDRESS_FIELD_NUMBER = 4;
private com.google.protobuf.LazyStringList address_;
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ProtocolStringList
getAddressList() {
return address_;
}
/**
* <code>repeated string address = 4;</code>
*/
public int getAddressCount() {
return address_.size();
}
/**
* <code>repeated string address = 4;</code>
*/
public String getAddress(int index) {
return address_.get(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes(int index) {
return address_.getByteString(index);
}
private byte memoizedIsInitialized = -1;
@Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
if (!hasSubReqID()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasUserName()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasProductName()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
}
@Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (((bitField0_ & 0x00000001) != 0)) {
output.writeInt32(1, subReqID_);
}
if (((bitField0_ & 0x00000002) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 2, userName_);
}
if (((bitField0_ & 0x00000004) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, productName_);
}
for (int i = 0; i < address_.size(); i++) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 4, address_.getRaw(i));
}
unknownFields.writeTo(output);
}
@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, subReqID_);
}
if (((bitField0_ & 0x00000002) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, userName_);
}
if (((bitField0_ & 0x00000004) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, productName_);
}
{
int dataSize = 0;
for (int i = 0; i < address_.size(); i++) {
dataSize += computeStringSizeNoTag(address_.getRaw(i));
}
size += dataSize;
size += 1 * getAddressList().size();
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
}
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SubscribeReq)) {
return super.equals(obj);
}
SubscribeReq other = (SubscribeReq) obj;
if (hasSubReqID() != other.hasSubReqID()) return false;
if (hasSubReqID()) {
if (getSubReqID()
!= other.getSubReqID()) return false;
}
if (hasUserName() != other.hasUserName()) return false;
if (hasUserName()) {
if (!getUserName()
.equals(other.getUserName())) return false;
}
if (hasProductName() != other.hasProductName()) return false;
if (hasProductName()) {
if (!getProductName()
.equals(other.getProductName())) return false;
}
if (!getAddressList()
.equals(other.getAddressList())) return false;
if (!unknownFields.equals(other.unknownFields)) return false;
return true;
}
@Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
if (hasSubReqID()) {
hash = (37 * hash) + SUBREQID_FIELD_NUMBER;
hash = (53 * hash) + getSubReqID();
}
if (hasUserName()) {
hash = (37 * hash) + USERNAME_FIELD_NUMBER;
hash = (53 * hash) + getUserName().hashCode();
}
if (hasProductName()) {
hash = (37 * hash) + PRODUCTNAME_FIELD_NUMBER;
hash = (53 * hash) + getProductName().hashCode();
}
if (getAddressCount() > 0) {
hash = (37 * hash) + ADDRESS_FIELD_NUMBER;
hash = (53 * hash) + getAddressList().hashCode();
}
hash = (29 * hash) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
}
public static SubscribeReq parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeReq parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeReq parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeReq parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeReq parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeReq parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeReq parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeReq 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 SubscribeReq parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static SubscribeReq 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 SubscribeReq parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeReq parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
}
@Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(SubscribeReq prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
@Override
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
}
@Override
protected Builder newBuilderForType(
BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code SubscribeReq}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:SubscribeReq)
SubscribeReqOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return SubscribeReqProto.internal_static_SubscribeReq_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeReqProto.internal_static_SubscribeReq_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeReq.class, Builder.class);
}
// Construct using com.viagra.chapter08.protobuf.SubscribeReqProto.SubscribeReq.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3
.alwaysUseFieldBuilders) {
}
}
@Override
public Builder clear() {
super.clear();
subReqID_ = 0;
bitField0_ = (bitField0_ & ~0x00000001);
userName_ = "";
bitField0_ = (bitField0_ & ~0x00000002);
productName_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000008);
return this;
}
@Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return SubscribeReqProto.internal_static_SubscribeReq_descriptor;
}
@Override
public SubscribeReq getDefaultInstanceForType() {
return SubscribeReq.getDefaultInstance();
}
@Override
public SubscribeReq build() {
SubscribeReq result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@Override
public SubscribeReq buildPartial() {
SubscribeReq result = new SubscribeReq(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) != 0)) {
result.subReqID_ = subReqID_;
to_bitField0_ |= 0x00000001;
}
if (((from_bitField0_ & 0x00000002) != 0)) {
to_bitField0_ |= 0x00000002;
}
result.userName_ = userName_;
if (((from_bitField0_ & 0x00000004) != 0)) {
to_bitField0_ |= 0x00000004;
}
result.productName_ = productName_;
if (((bitField0_ & 0x00000008) != 0)) {
address_ = address_.getUnmodifiableView();
bitField0_ = (bitField0_ & ~0x00000008);
}
result.address_ = address_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
@Override
public Builder clone() {
return super.clone();
}
@Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return super.setField(field, value);
}
@Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return super.clearField(field);
}
@Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return super.clearOneof(oneof);
}
@Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, Object value) {
return super.setRepeatedField(field, index, value);
}
@Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return super.addRepeatedField(field, value);
}
@Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof SubscribeReq) {
return mergeFrom((SubscribeReq)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(SubscribeReq other) {
if (other == SubscribeReq.getDefaultInstance()) return this;
if (other.hasSubReqID()) {
setSubReqID(other.getSubReqID());
}
if (other.hasUserName()) {
bitField0_ |= 0x00000002;
userName_ = other.userName_;
onChanged();
}
if (other.hasProductName()) {
bitField0_ |= 0x00000004;
productName_ = other.productName_;
onChanged();
}
if (!other.address_.isEmpty()) {
if (address_.isEmpty()) {
address_ = other.address_;
bitField0_ = (bitField0_ & ~0x00000008);
} else {
ensureAddressIsMutable();
address_.addAll(other.address_);
}
onChanged();
}
this.mergeUnknownFields(other.unknownFields);
onChanged();
return this;
}
@Override
public final boolean isInitialized() {
if (!hasSubReqID()) {
return false;
}
if (!hasUserName()) {
return false;
}
if (!hasProductName()) {
return false;
}
return true;
}
@Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
SubscribeReq parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (SubscribeReq) e.getUnfinishedMessage();
throw e.unwrapIOException();
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
private int subReqID_ ;
/**
* <code>required int32 subReqID = 1;</code>
*/
public boolean hasSubReqID() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public Builder setSubReqID(int value) {
bitField0_ |= 0x00000001;
subReqID_ = value;
onChanged();
return this;
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public Builder clearSubReqID() {
bitField0_ = (bitField0_ & ~0x00000001);
subReqID_ = 0;
onChanged();
return this;
}
private Object userName_ = "";
/**
* <code>required string userName = 2;</code>
*/
public boolean hasUserName() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>required string userName = 2;</code>
*/
public String getUserName() {
Object ref = userName_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
userName_ = s;
}
return s;
} else {
return (String) ref;
}
}
/**
* <code>required string userName = 2;</code>
*/
public com.google.protobuf.ByteString
getUserNameBytes() {
Object ref = userName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
userName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string userName = 2;</code>
*/
public Builder setUserName(
String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000002;
userName_ = value;
onChanged();
return this;
}
/**
* <code>required string userName = 2;</code>
*/
public Builder clearUserName() {
bitField0_ = (bitField0_ & ~0x00000002);
userName_ = getDefaultInstance().getUserName();
onChanged();
return this;
}
/**
* <code>required string userName = 2;</code>
*/
public Builder setUserNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000002;
userName_ = value;
onChanged();
return this;
}
private Object productName_ = "";
/**
* <code>required string productName = 3;</code>
*/
public boolean hasProductName() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>required string productName = 3;</code>
*/
public String getProductName() {
Object ref = productName_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
productName_ = s;
}
return s;
} else {
return (String) ref;
}
}
/**
* <code>required string productName = 3;</code>
*/
public com.google.protobuf.ByteString
getProductNameBytes() {
Object ref = productName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
productName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string productName = 3;</code>
*/
public Builder setProductName(
String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
productName_ = value;
onChanged();
return this;
}
/**
* <code>required string productName = 3;</code>
*/
public Builder clearProductName() {
bitField0_ = (bitField0_ & ~0x00000004);
productName_ = getDefaultInstance().getProductName();
onChanged();
return this;
}
/**
* <code>required string productName = 3;</code>
*/
public Builder setProductNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
productName_ = value;
onChanged();
return this;
}
private com.google.protobuf.LazyStringList address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
private void ensureAddressIsMutable() {
if (!((bitField0_ & 0x00000008) != 0)) {
address_ = new com.google.protobuf.LazyStringArrayList(address_);
bitField0_ |= 0x00000008;
}
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ProtocolStringList
getAddressList() {
return address_.getUnmodifiableView();
}
/**
* <code>repeated string address = 4;</code>
*/
public int getAddressCount() {
return address_.size();
}
/**
* <code>repeated string address = 4;</code>
*/
public String getAddress(int index) {
return address_.get(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes(int index) {
return address_.getByteString(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder setAddress(
int index, String value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.set(index, value);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAddress(
String value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.add(value);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAllAddress(
Iterable<String> values) {
ensureAddressIsMutable();
com.google.protobuf.AbstractMessageLite.Builder.addAll(
values, address_);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder clearAddress() {
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000008);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAddressBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.add(value);
onChanged();
return this;
}
@Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFields(unknownFields);
}
@Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
}
// @@protoc_insertion_point(builder_scope:SubscribeReq)
}
// @@protoc_insertion_point(class_scope:SubscribeReq)
private static final SubscribeReq DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new SubscribeReq();
}
public static SubscribeReq getDefaultInstance() {
return DEFAULT_INSTANCE;
}
@Deprecated public static final com.google.protobuf.Parser<SubscribeReq>
PARSER = new com.google.protobuf.AbstractParser<SubscribeReq>() {
@Override
public SubscribeReq parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new SubscribeReq(input, extensionRegistry);
}
};
public static com.google.protobuf.Parser<SubscribeReq> parser() {
return PARSER;
}
@Override
public com.google.protobuf.Parser<SubscribeReq> getParserForType() {
return PARSER;
}
@Override
public SubscribeReq getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_SubscribeReq_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_SubscribeReq_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\022SubscribeReq.proto\"X\n\014SubscribeReq\022\020\n\010" +
"subReqID\030\001 \002(\005\022\020\n\010userName\030\002 \002(\t\022\023\n\013prod" +
"uctName\030\003 \002(\t\022\017\n\007address\030\004 \003(\tB2\n\035com.vi" +
"agra.chapter08.protobufB\021SubscribeReqPro" +
"to"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_SubscribeReq_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_SubscribeReq_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_SubscribeReq_descriptor,
new String[] { "SubReqID", "UserName", "ProductName", "Address", });
}
// @@protoc_insertion_point(outer_class_scope)
}
SubscribeRespProto.java
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: SubscribeResp.proto
package com.viagra.chapter08.protobuf;
public final class SubscribeRespProto {
private SubscribeRespProto() {}
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 SubscribeRespOrBuilder extends
// @@protoc_insertion_point(interface_extends:SubscribeResp)
com.google.protobuf.MessageOrBuilder {
/**
* <code>required int32 subReqID = 1;</code>
*/
boolean hasSubReqID();
/**
* <code>required int32 subReqID = 1;</code>
*/
int getSubReqID();
/**
* <code>required int32 respCode = 2;</code>
*/
boolean hasRespCode();
/**
* <code>required int32 respCode = 2;</code>
*/
int getRespCode();
/**
* <code>required string desc = 3;</code>
*/
boolean hasDesc();
/**
* <code>required string desc = 3;</code>
*/
String getDesc();
/**
* <code>required string desc = 3;</code>
*/
com.google.protobuf.ByteString
getDescBytes();
}
/**
* Protobuf type {@code SubscribeResp}
*/
public static final class SubscribeResp extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:SubscribeResp)
SubscribeRespOrBuilder {
private static final long serialVersionUID = 0L;
// Use SubscribeResp.newBuilder() to construct.
private SubscribeResp(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private SubscribeResp() {
desc_ = "";
}
@Override
@SuppressWarnings({"unused"})
protected Object newInstance(
UnusedPrivateParameter unused) {
return new SubscribeResp();
}
@Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private SubscribeResp(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
this();
if (extensionRegistry == null) {
throw new 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;
subReqID_ = input.readInt32();
break;
}
case 16: {
bitField0_ |= 0x00000002;
respCode_ = input.readInt32();
break;
}
case 26: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000004;
desc_ = 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 SubscribeRespProto.internal_static_SubscribeResp_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeRespProto.internal_static_SubscribeResp_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeResp.class, Builder.class);
}
private int bitField0_;
public static final int SUBREQID_FIELD_NUMBER = 1;
private int subReqID_;
/**
* <code>required int32 subReqID = 1;</code>
*/
public boolean hasSubReqID() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
}
public static final int RESPCODE_FIELD_NUMBER = 2;
private int respCode_;
/**
* <code>required int32 respCode = 2;</code>
*/
public boolean hasRespCode() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>required int32 respCode = 2;</code>
*/
public int getRespCode() {
return respCode_;
}
public static final int DESC_FIELD_NUMBER = 3;
private volatile Object desc_;
/**
* <code>required string desc = 3;</code>
*/
public boolean hasDesc() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>required string desc = 3;</code>
*/
public String getDesc() {
Object ref = desc_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
desc_ = s;
}
return s;
}
}
/**
* <code>required string desc = 3;</code>
*/
public com.google.protobuf.ByteString
getDescBytes() {
Object ref = desc_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
desc_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
private byte memoizedIsInitialized = -1;
@Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
if (!hasSubReqID()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasRespCode()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasDesc()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
}
@Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (((bitField0_ & 0x00000001) != 0)) {
output.writeInt32(1, subReqID_);
}
if (((bitField0_ & 0x00000002) != 0)) {
output.writeInt32(2, respCode_);
}
if (((bitField0_ & 0x00000004) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, desc_);
}
unknownFields.writeTo(output);
}
@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, subReqID_);
}
if (((bitField0_ & 0x00000002) != 0)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, respCode_);
}
if (((bitField0_ & 0x00000004) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, desc_);
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
}
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SubscribeResp)) {
return super.equals(obj);
}
SubscribeResp other = (SubscribeResp) obj;
if (hasSubReqID() != other.hasSubReqID()) return false;
if (hasSubReqID()) {
if (getSubReqID()
!= other.getSubReqID()) return false;
}
if (hasRespCode() != other.hasRespCode()) return false;
if (hasRespCode()) {
if (getRespCode()
!= other.getRespCode()) return false;
}
if (hasDesc() != other.hasDesc()) return false;
if (hasDesc()) {
if (!getDesc()
.equals(other.getDesc())) return false;
}
if (!unknownFields.equals(other.unknownFields)) return false;
return true;
}
@Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
if (hasSubReqID()) {
hash = (37 * hash) + SUBREQID_FIELD_NUMBER;
hash = (53 * hash) + getSubReqID();
}
if (hasRespCode()) {
hash = (37 * hash) + RESPCODE_FIELD_NUMBER;
hash = (53 * hash) + getRespCode();
}
if (hasDesc()) {
hash = (37 * hash) + DESC_FIELD_NUMBER;
hash = (53 * hash) + getDesc().hashCode();
}
hash = (29 * hash) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
}
public static SubscribeResp parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeResp parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeResp parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeResp parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeResp parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeResp parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeResp parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeResp 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 SubscribeResp parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static SubscribeResp 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 SubscribeResp parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeResp parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
}
@Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(SubscribeResp prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
@Override
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
}
@Override
protected Builder newBuilderForType(
BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code SubscribeResp}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:SubscribeResp)
SubscribeRespOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return SubscribeRespProto.internal_static_SubscribeResp_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeRespProto.internal_static_SubscribeResp_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeResp.class, Builder.class);
}
// Construct using com.viagra.chapter08.protobuf.SubscribeRespProto.SubscribeResp.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3
.alwaysUseFieldBuilders) {
}
}
@Override
public Builder clear() {
super.clear();
subReqID_ = 0;
bitField0_ = (bitField0_ & ~0x00000001);
respCode_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
desc_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
return this;
}
@Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return SubscribeRespProto.internal_static_SubscribeResp_descriptor;
}
@Override
public SubscribeResp getDefaultInstanceForType() {
return SubscribeResp.getDefaultInstance();
}
@Override
public SubscribeResp build() {
SubscribeResp result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@Override
public SubscribeResp buildPartial() {
SubscribeResp result = new SubscribeResp(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) != 0)) {
result.subReqID_ = subReqID_;
to_bitField0_ |= 0x00000001;
}
if (((from_bitField0_ & 0x00000002) != 0)) {
result.respCode_ = respCode_;
to_bitField0_ |= 0x00000002;
}
if (((from_bitField0_ & 0x00000004) != 0)) {
to_bitField0_ |= 0x00000004;
}
result.desc_ = desc_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
@Override
public Builder clone() {
return super.clone();
}
@Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return super.setField(field, value);
}
@Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return super.clearField(field);
}
@Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return super.clearOneof(oneof);
}
@Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, Object value) {
return super.setRepeatedField(field, index, value);
}
@Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return super.addRepeatedField(field, value);
}
@Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof SubscribeResp) {
return mergeFrom((SubscribeResp)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(SubscribeResp other) {
if (other == SubscribeResp.getDefaultInstance()) return this;
if (other.hasSubReqID()) {
setSubReqID(other.getSubReqID());
}
if (other.hasRespCode()) {
setRespCode(other.getRespCode());
}
if (other.hasDesc()) {
bitField0_ |= 0x00000004;
desc_ = other.desc_;
onChanged();
}
this.mergeUnknownFields(other.unknownFields);
onChanged();
return this;
}
@Override
public final boolean isInitialized() {
if (!hasSubReqID()) {
return false;
}
if (!hasRespCode()) {
return false;
}
if (!hasDesc()) {
return false;
}
return true;
}
@Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
SubscribeResp parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (SubscribeResp) e.getUnfinishedMessage();
throw e.unwrapIOException();
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
private int subReqID_ ;
/**
* <code>required int32 subReqID = 1;</code>
*/
public boolean hasSubReqID() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public Builder setSubReqID(int value) {
bitField0_ |= 0x00000001;
subReqID_ = value;
onChanged();
return this;
}
/**
* <code>required int32 subReqID = 1;</code>
*/
public Builder clearSubReqID() {
bitField0_ = (bitField0_ & ~0x00000001);
subReqID_ = 0;
onChanged();
return this;
}
private int respCode_ ;
/**
* <code>required int32 respCode = 2;</code>
*/
public boolean hasRespCode() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>required int32 respCode = 2;</code>
*/
public int getRespCode() {
return respCode_;
}
/**
* <code>required int32 respCode = 2;</code>
*/
public Builder setRespCode(int value) {
bitField0_ |= 0x00000002;
respCode_ = value;
onChanged();
return this;
}
/**
* <code>required int32 respCode = 2;</code>
*/
public Builder clearRespCode() {
bitField0_ = (bitField0_ & ~0x00000002);
respCode_ = 0;
onChanged();
return this;
}
private Object desc_ = "";
/**
* <code>required string desc = 3;</code>
*/
public boolean hasDesc() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>required string desc = 3;</code>
*/
public String getDesc() {
Object ref = desc_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
desc_ = s;
}
return s;
} else {
return (String) ref;
}
}
/**
* <code>required string desc = 3;</code>
*/
public com.google.protobuf.ByteString
getDescBytes() {
Object ref = desc_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
desc_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string desc = 3;</code>
*/
public Builder setDesc(
String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
desc_ = value;
onChanged();
return this;
}
/**
* <code>required string desc = 3;</code>
*/
public Builder clearDesc() {
bitField0_ = (bitField0_ & ~0x00000004);
desc_ = getDefaultInstance().getDesc();
onChanged();
return this;
}
/**
* <code>required string desc = 3;</code>
*/
public Builder setDescBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
desc_ = value;
onChanged();
return this;
}
@Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFields(unknownFields);
}
@Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
}
// @@protoc_insertion_point(builder_scope:SubscribeResp)
}
// @@protoc_insertion_point(class_scope:SubscribeResp)
private static final SubscribeResp DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new SubscribeResp();
}
public static SubscribeResp getDefaultInstance() {
return DEFAULT_INSTANCE;
}
@Deprecated public static final com.google.protobuf.Parser<SubscribeResp>
PARSER = new com.google.protobuf.AbstractParser<SubscribeResp>() {
@Override
public SubscribeResp parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new SubscribeResp(input, extensionRegistry);
}
};
public static com.google.protobuf.Parser<SubscribeResp> parser() {
return PARSER;
}
@Override
public com.google.protobuf.Parser<SubscribeResp> getParserForType() {
return PARSER;
}
@Override
public SubscribeResp getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_SubscribeResp_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_SubscribeResp_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\023SubscribeResp.proto\"A\n\rSubscribeResp\022\020" +
"\n\010subReqID\030\001 \002(\005\022\020\n\010respCode\030\002 \002(\005\022\014\n\004de" +
"sc\030\003 \002(\tB3\n\035com.viagra.chapter08.protobu" +
"fB\022SubscribeRespProto"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_SubscribeResp_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_SubscribeResp_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_SubscribeResp_descriptor,
new String[] { "SubReqID", "RespCode", "Desc", });
}
// @@protoc_insertion_point(outer_class_scope)
}
最后运行TestSubscribeReqProto.java
package com.viagra.chapter08.protobuf;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: viagra
* @Date: 2019/8/1 14:54
* @Description:
*/
public class TestSubscribeReqProto {
private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
return req.toByteArray();
}
private static SubscribeReqProto.SubscribeReq decode(byte[] body)
throws InvalidProtocolBufferException {
return SubscribeReqProto.SubscribeReq.parseFrom(body);
}
private static SubscribeReqProto.SubscribeReq createSubscribeReq() {
SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq
.newBuilder();
builder.setSubReqID(1);
builder.setUserName("Lilinfeng");
builder.setProductName("Netty Book");
List<String> address = new ArrayList<>();
address.add("NanJing YuHuaTai");
address.add("BeiJing LiuLiChang");
address.add("ShenZhen HongShuLin");
builder.addAllAddress(address);
return builder.build();
}
/**
* @param args
* @throws InvalidProtocolBufferException
*/
public static void main(String[] args)
throws InvalidProtocolBufferException {
SubscribeReqProto.SubscribeReq req = createSubscribeReq();
System.out.println("Before encode : " + req.toString());
SubscribeReqProto.SubscribeReq req2 = decode(encode(req));
System.out.println("After decode : " + req.toString());
System.out.println("Assert equal : --> " + req2.equals(req));
}
}
``
我 们 首 先 看 如 何 创 建 `SubscribeReqProto.SubscribeReq` 实 例 , 第 24 行 通 过 `SubscribeReqProto.SubscribeReq` 的 静 态 方 法 newBuilder 创 建 SubscribeReqProto.SubscribeReq 的 Builder实 例 , 通 过 Builder 构 建 器 对 SubscribeReq 的 属 性 进 行 设 置 , 对 于 集 合 类 型 , 通 过addAllXXX() 方 法 可 以 将 集 合 对 象 设 置 到 对 应 的 属 性 中编 码 时 通 过 调 用 SubscribeReqProto.SubscribeReq 实 例 的 toByteArray 即 可 将SubscribeReq 编 码 为 byte 数 组 , 使 用 非 常 方 便 。
编 码 时 通 过 调 用 `SubscribeReqProto.SubscribeReq` 实 例 的 toByteArray 即 可 将Subscri be Req 编 码 为 byte 数 组 , 使 用 非 常 方 便 。
解 码 时 通 过 SubscribeReqProto.SubscribeReq 的 静 态 方 法 parseFrom 将 二 进 制 byte 数 组解 码 为 原 始 的 对 象 。
运行结果:
```yml
"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe" "-javaagent:E:\software\JetBrains\IntelliJ IDEA 2019.1.2\IntelliJ IDEA 2018.3\lib\idea_rt.jar=8807:E:\software\JetBrains\IntelliJ IDEA 2019.1.2\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;D:\java\netty\cookbook\netty-UserGuide\target\classes;E:\ApacheSoftwareFoundation\maven_Repository\org\projectlombok\lombok\1.16.20\lombok-1.16.20.jar;E:\ApacheSoftwareFoundation\maven_Repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\ApacheSoftwareFoundation\maven_Repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;E:\ApacheSoftwareFoundation\maven_Repository\io\netty\netty-all\5.0.0.Alpha2\netty-all-5.0.0.Alpha2.jar;E:\ApacheSoftwareFoundation\maven_Repository\org\msgpack\msgpack\0.6.12\msgpack-0.6.12.jar;E:\ApacheSoftwareFoundation\maven_Repository\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar;E:\ApacheSoftwareFoundation\maven_Repository\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;E:\ApacheSoftwareFoundation\maven_Repository\com\google\protobuf\protobuf-java\3.9.0\protobuf-java-3.9.0.jar" com.viagra.chapter08.protobuf.TestSubscribeReqProto
Before encode : subReqID: 1
userName: "Lilinfeng"
productName: "Netty Book"
address: "NanJing YuHuaTai"
address: "BeiJing LiuLiChang"
address: "ShenZhen HongShuLin"
After decode : subReqID: 1
userName: "Lilinfeng"
productName: "Netty Book"
address: "NanJing YuHuaTai"
address: "BeiJing LiuLiChang"
address: "ShenZhen HongShuLin"
Assert equal : --> true
Process finished with exit code 0
8.2 Netty的protobuf 服务器端编程
SubRespProServer.java
package com.viagra.chapter08.protobufDemo;
import com.viagra.chapter08.protobuf.SubscribeReqProto;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* @Auther: viagra
* @Date: 2019/8/1 21:58
* @Description:
*/
public class SubRespProServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap s = new ServerBootstrap();
s.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ProtobufVarint32FrameDecoder());
sc.pipeline().addLast(new ProtobufDecoder(SubscribeReqProto.SubcribeReq.getDefaultInstance()));
sc.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
sc.pipeline().addLast(new ProtobufEncoder());
sc.pipeline().addLast(new SubRespProHandler());
}
});
try {
ChannelFuture cf = s.bind(9989).sync();
cf.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
SubRespProHandler.java
package com.viagra.chapter08.protobufDemo;
import com.viagra.chapter08.protobuf.SubscribeReqProto;
import com.viagra.chapter08.protobuf.SubscribeRespProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerAdapter;
/**
* @Auther: viagra
* @Date: 2019/8/1 21:59
* @Description:
*/
public class SubRespProHandler extends ChannelHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
private SubscribeRespProto.SubcribeResp createSubcribeResp(int subReqID) {
SubscribeRespProto.SubcribeResp.Builder builder = SubscribeRespProto.SubcribeResp.newBuilder();
builder.setSubReqID(subReqID);
builder.setRespCode(0);
builder.setDesc("Order success.");
return builder.build();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
SubscribeReqProto.SubcribeReq req = (SubscribeReqProto.SubcribeReq) msg;
System.out.println("接收到客户端请求:" + req.getSubReqID() + ",userName:" + req.getUserName() + ",productName:" + req.getProductName());
SubscribeRespProto.SubcribeResp resp = createSubcribeResp(req.getSubReqID());
ctx.writeAndFlush(resp);
}
}
SubReqProClient.java
package com.viagra.chapter08.protobufDemo;
import com.viagra.chapter08.protobuf.SubscribeRespProto;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
/**
* @Auther: viagra
* @Date: 2019/8/1 22:00
* @Description:
*/
public class SubReqProClient {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ProtobufVarint32FrameDecoder());
sc.pipeline().addLast(new ProtobufDecoder(SubscribeRespProto.SubcribeResp.getDefaultInstance()));
sc.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
sc.pipeline().addLast(new ProtobufEncoder());
sc.pipeline().addLast(new SubReqProHandler());
}
});
try {
ChannelFuture f = b.connect("127.0.0.1", 9989).sync();
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
SubReqProHandler.java
package com.viagra.chapter08.protobufDemo;
import com.viagra.chapter08.protobuf.SubscribeReqProto;
import com.viagra.chapter08.protobuf.SubscribeRespProto;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: viagra
* @Date: 2019/8/1 22:00
* @Description:
*/
public class SubReqProHandler extends ChannelHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
SubscribeReqProto.SubcribeReq req = null;
for (int i=0;i<10;i++) {
req = createSubcribeReq(i);
ctx.write(req);
}
ctx.flush();
}
private SubscribeReqProto.SubcribeReq createSubcribeReq(int subSeqID) {
SubscribeReqProto.SubcribeReq.Builder builder = SubscribeReqProto.SubcribeReq.newBuilder();
builder.setSubReqID(subSeqID);
builder.setUserName("j.tommy");
builder.setProductName("netty权威指南");
List<String> addressList = new ArrayList<String>();
addressList.add("北京市");
addressList.add("西安市");
addressList.add("深圳市");
return builder.build();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
SubscribeRespProto.SubcribeResp resp = (SubscribeRespProto.SubcribeResp) msg;
System.out.println("接收到服务端响应:" + resp.getSubReqID() + ",responseCode:" + resp.getRespCode() + ",desc:" + resp.getDesc());
}
}
运行结果:
服务端
2019-08-02 09:55:37 DEBUG Recycler:56 - -Dio.netty.recycler.maxCapacity: 262144
接收到客户端请求:0,userName:j.tommy,productName:netty权威指南
接收到客户端请求:1,userName:j.tommy,productName:netty权威指南
接收到客户端请求:2,userName:j.tommy,productName:netty权威指南
接收到客户端请求:3,userName:j.tommy,productName:netty权威指南
接收到客户端请求:4,userName:j.tommy,productName:netty权威指南
接收到客户端请求:5,userName:j.tommy,productName:netty权威指南
接收到客户端请求:6,userName:j.tommy,productName:netty权威指南
接收到客户端请求:7,userName:j.tommy,productName:netty权威指南
接收到客户端请求:8,userName:j.tommy,productName:netty权威指南
接收到客户端请求:9,userName:j.tommy,productName:netty权威指南
客户端
2019-08-02 09:55:37 DEBUG ResourceLeakDetector:79 - -Dio.netty.leakDetectionLevel: simple
接收到服务端响应:0,responseCode:0,desc:Order success.
接收到服务端响应:1,responseCode:0,desc:Order success.
接收到服务端响应:2,responseCode:0,desc:Order success.
接收到服务端响应:3,responseCode:0,desc:Order success.
接收到服务端响应:4,responseCode:0,desc:Order success.
接收到服务端响应:5,responseCode:0,desc:Order success.
接收到服务端响应:6,responseCode:0,desc:Order success.
接收到服务端响应:7,responseCode:0,desc:Order success.
接收到服务端响应:8,responseCode:0,desc:Order success.
接收到服务端响应:9,responseCode:0,desc:Order success.
8.3.Protobuf的使用注意事项
ProtobufDecoder仅仅负责解码,它不支持读半包。因此在ProtobufDecoder的前面,一定要有能够处理半包消息的解码器。
有3种方式可以选择:
1.使用Netty提供的ProtobufVarint32FrameDecoder,它可以处理半包消息;
2.继承Netty提供的通用半包解码器LengthFieldBasedFrameDecoder;
3.继承ByteToMessageDecoder类,自己处理半包消息。
如果只使用ProtobufDecoder解码器,而忽略对半包消息的处理,程序没法正常工作。