一、Protobuf入门案例2
完善程序,使Protobuf完成如下功能
1、 客户端可以随机发送StudentPOJO/WorkerPOJO对象到服务器(通过Protobuf编码);
2、 服务端能接收StudentPOJO/WorkerPOJO对象(需要判断是哪种类型),并显示信息(通过Protobuf编码);
3、 代码;
二、修改代码
1、 建立Student.proto文件;
syntax = "proto3";
option optimize_for = SPEED; //加快解析
option java_package = "netty.protobuf2"; //指定生成到哪个包下
option java_outer_classname = "MyDataInfo"; //外部类名称
//protobuf可以使用message管理其他的message
message MyMessage {
//定义一个枚举类型
enum DataType {
StudentType = 0; //在proto3,要求enum的编号从0开始
WorkerType = 1; //
}
//用data_type来标识传的是哪一个枚举类型
//这个data_type=1的1 是MyMessage的属性编号!
DataType data_type = 1;
//表示每次提交的枚举类型最多只能出现其中的一个,节省空间
oneof dataBody {
Student student = 2;
Worker worker = 3;
}
//enum是定义枚举,data_type是使用枚举组为一个属性,oneof是再定义一个属性,但只能是其中一个message对象类型
}
message Student {
int32 id = 1; //Student类的属性
string name = 2;
}
message Worker {
string name = 1;
int32 age = 2;
}
2、 转换后MyDataInfo.java;
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: Student.proto
package netty.protobuf2;
public final class MyDataInfo {
private MyDataInfo() {}
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 MyMessageOrBuilder extends
// @@protoc_insertion_point(interface_extends:MyMessage)
com.google.protobuf.MessageOrBuilder {
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return The enum numeric value on the wire for dataType.
*/
int getDataTypeValue();
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return The dataType.
*/
netty.protobuf2.MyDataInfo.MyMessage.DataType getDataType();
/**
* <code>.Student student = 2;</code>
* @return Whether the student field is set.
*/
boolean hasStudent();
/**
* <code>.Student student = 2;</code>
* @return The student.
*/
netty.protobuf2.MyDataInfo.Student getStudent();
/**
* <code>.Student student = 2;</code>
*/
netty.protobuf2.MyDataInfo.StudentOrBuilder getStudentOrBuilder();
/**
* <code>.Worker worker = 3;</code>
* @return Whether the worker field is set.
*/
boolean hasWorker();
/**
* <code>.Worker worker = 3;</code>
* @return The worker.
*/
netty.protobuf2.MyDataInfo.Worker getWorker();
/**
* <code>.Worker worker = 3;</code>
*/
netty.protobuf2.MyDataInfo.WorkerOrBuilder getWorkerOrBuilder();
public netty.protobuf2.MyDataInfo.MyMessage.DataBodyCase getDataBodyCase();
}
/**
* <pre>
*protobuf可以使用message管理其他的message
* </pre>
*
* Protobuf type {@code MyMessage}
*/
public static final class MyMessage extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:MyMessage)
MyMessageOrBuilder {
private static final long serialVersionUID = 0L;
// Use MyMessage.newBuilder() to construct.
private MyMessage(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private MyMessage() {
dataType_ = 0;
}
@java.lang.Override
@SuppressWarnings({"unused"})
protected java.lang.Object newInstance(
UnusedPrivateParameter unused) {
return new MyMessage();
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.internal_static_MyMessage_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return netty.protobuf2.MyDataInfo.internal_static_MyMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
netty.protobuf2.MyDataInfo.MyMessage.class, netty.protobuf2.MyDataInfo.MyMessage.Builder.class);
}
/**
* <pre>
*定义一个枚举类型
* </pre>
*
* Protobuf enum {@code MyMessage.DataType}
*/
public enum DataType
implements com.google.protobuf.ProtocolMessageEnum {
/**
* <pre>
*在proto3,要求enum的编号从0开始
* </pre>
*
* <code>StudentType = 0;</code>
*/
StudentType(0),
/**
* <pre>
* </pre>
*
* <code>WorkerType = 1;</code>
*/
WorkerType(1),
UNRECOGNIZED(-1),
;
/**
* <pre>
*在proto3,要求enum的编号从0开始
* </pre>
*
* <code>StudentType = 0;</code>
*/
public static final int StudentType_VALUE = 0;
/**
* <pre>
* </pre>
*
* <code>WorkerType = 1;</code>
*/
public static final int WorkerType_VALUE = 1;
public final int getNumber() {
if (this == UNRECOGNIZED) {
throw new java.lang.IllegalArgumentException(
"Can't get the number of an unknown enum value.");
}
return value;
}
/**
* @param value The numeric wire value of the corresponding enum entry.
* @return The enum associated with the given numeric wire value.
* @deprecated Use {@linkforNumber(int)} instead.
*/
@java.lang.Deprecated
public static DataType valueOf(int value) {
return forNumber(value);
}
/**
* @param value The numeric wire value of the corresponding enum entry.
* @return The enum associated with the given numeric wire value.
*/
public static DataType forNumber(int value) {
switch (value) {
case 0: return StudentType;
case 1: return WorkerType;
default: return null;
}
}
public static com.google.protobuf.Internal.EnumLiteMap<DataType>
internalGetValueMap() {
return internalValueMap;
}
private static final com.google.protobuf.Internal.EnumLiteMap<
DataType> internalValueMap =
new com.google.protobuf.Internal.EnumLiteMap<DataType>() {
public DataType findValueByNumber(int number) {
return DataType.forNumber(number);
}
};
public final com.google.protobuf.Descriptors.EnumValueDescriptor
getValueDescriptor() {
if (this == UNRECOGNIZED) {
throw new java.lang.IllegalStateException(
"Can't get the descriptor of an unrecognized enum value.");
}
return getDescriptor().getValues().get(ordinal());
}
public final com.google.protobuf.Descriptors.EnumDescriptor
getDescriptorForType() {
return getDescriptor();
}
public static final com.google.protobuf.Descriptors.EnumDescriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.MyMessage.getDescriptor().getEnumTypes().get(0);
}
private static final DataType[] VALUES = values();
public static DataType valueOf(
com.google.protobuf.Descriptors.EnumValueDescriptor desc) {
if (desc.getType() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"EnumValueDescriptor is not for this type.");
}
if (desc.getIndex() == -1) {
return UNRECOGNIZED;
}
return VALUES[desc.getIndex()];
}
private final int value;
private DataType(int value) {
this.value = value;
}
// @@protoc_insertion_point(enum_scope:MyMessage.DataType)
}
private int dataBodyCase_ = 0;
private java.lang.Object dataBody_;
public enum DataBodyCase
implements com.google.protobuf.Internal.EnumLite,
com.google.protobuf.AbstractMessage.InternalOneOfEnum {
STUDENT(2),
WORKER(3),
DATABODY_NOT_SET(0);
private final int value;
private DataBodyCase(int value) {
this.value = value;
}
/**
* @param value The number of the enum to look for.
* @return The enum associated with the given number.
* @deprecated Use {@linkforNumber(int)} instead.
*/
@java.lang.Deprecated
public static DataBodyCase valueOf(int value) {
return forNumber(value);
}
public static DataBodyCase forNumber(int value) {
switch (value) {
case 2: return STUDENT;
case 3: return WORKER;
case 0: return DATABODY_NOT_SET;
default: return null;
}
}
public int getNumber() {
return this.value;
}
};
public DataBodyCase
getDataBodyCase() {
return DataBodyCase.forNumber(
dataBodyCase_);
}
public static final int DATA_TYPE_FIELD_NUMBER = 1;
private int dataType_ = 0;
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return The enum numeric value on the wire for dataType.
*/
@java.lang.Override public int getDataTypeValue() {
return dataType_;
}
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return The dataType.
*/
@java.lang.Override public netty.protobuf2.MyDataInfo.MyMessage.DataType getDataType() {
netty.protobuf2.MyDataInfo.MyMessage.DataType result = netty.protobuf2.MyDataInfo.MyMessage.DataType.forNumber(dataType_);
return result == null ? netty.protobuf2.MyDataInfo.MyMessage.DataType.UNRECOGNIZED : result;
}
public static final int STUDENT_FIELD_NUMBER = 2;
/**
* <code>.Student student = 2;</code>
* @return Whether the student field is set.
*/
@java.lang.Override
public boolean hasStudent() {
return dataBodyCase_ == 2;
}
/**
* <code>.Student student = 2;</code>
* @return The student.
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.Student getStudent() {
if (dataBodyCase_ == 2) {
return (netty.protobuf2.MyDataInfo.Student) dataBody_;
}
return netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
}
/**
* <code>.Student student = 2;</code>
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.StudentOrBuilder getStudentOrBuilder() {
if (dataBodyCase_ == 2) {
return (netty.protobuf2.MyDataInfo.Student) dataBody_;
}
return netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
}
public static final int WORKER_FIELD_NUMBER = 3;
/**
* <code>.Worker worker = 3;</code>
* @return Whether the worker field is set.
*/
@java.lang.Override
public boolean hasWorker() {
return dataBodyCase_ == 3;
}
/**
* <code>.Worker worker = 3;</code>
* @return The worker.
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.Worker getWorker() {
if (dataBodyCase_ == 3) {
return (netty.protobuf2.MyDataInfo.Worker) dataBody_;
}
return netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
}
/**
* <code>.Worker worker = 3;</code>
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.WorkerOrBuilder getWorkerOrBuilder() {
if (dataBodyCase_ == 3) {
return (netty.protobuf2.MyDataInfo.Worker) dataBody_;
}
return netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
}
private byte memoizedIsInitialized = -1;
@java.lang.Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
@java.lang.Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (dataType_ != netty.protobuf2.MyDataInfo.MyMessage.DataType.StudentType.getNumber()) {
output.writeEnum(1, dataType_);
}
if (dataBodyCase_ == 2) {
output.writeMessage(2, (netty.protobuf2.MyDataInfo.Student) dataBody_);
}
if (dataBodyCase_ == 3) {
output.writeMessage(3, (netty.protobuf2.MyDataInfo.Worker) dataBody_);
}
getUnknownFields().writeTo(output);
}
@java.lang.Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (dataType_ != netty.protobuf2.MyDataInfo.MyMessage.DataType.StudentType.getNumber()) {
size += com.google.protobuf.CodedOutputStream
.computeEnumSize(1, dataType_);
}
if (dataBodyCase_ == 2) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(2, (netty.protobuf2.MyDataInfo.Student) dataBody_);
}
if (dataBodyCase_ == 3) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(3, (netty.protobuf2.MyDataInfo.Worker) dataBody_);
}
size += getUnknownFields().getSerializedSize();
memoizedSize = size;
return size;
}
@java.lang.Override
public boolean equals(final java.lang.Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof netty.protobuf2.MyDataInfo.MyMessage)) {
return super.equals(obj);
}
netty.protobuf2.MyDataInfo.MyMessage other = (netty.protobuf2.MyDataInfo.MyMessage) obj;
if (dataType_ != other.dataType_) return false;
if (!getDataBodyCase().equals(other.getDataBodyCase())) return false;
switch (dataBodyCase_) {
case 2:
if (!getStudent()
.equals(other.getStudent())) return false;
break;
case 3:
if (!getWorker()
.equals(other.getWorker())) return false;
break;
case 0:
default:
}
if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true;
}
@java.lang.Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
hash = (37 * hash) + DATA_TYPE_FIELD_NUMBER;
hash = (53 * hash) + dataType_;
switch (dataBodyCase_) {
case 2:
hash = (37 * hash) + STUDENT_FIELD_NUMBER;
hash = (53 * hash) + getStudent().hashCode();
break;
case 3:
hash = (37 * hash) + WORKER_FIELD_NUMBER;
hash = (53 * hash) + getWorker().hashCode();
break;
case 0:
default:
}
hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash;
return hash;
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.MyMessage parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.MyMessage 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 netty.protobuf2.MyDataInfo.MyMessage parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.MyMessage 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 netty.protobuf2.MyDataInfo.MyMessage parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.MyMessage 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(netty.protobuf2.MyDataInfo.MyMessage 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;
}
/**
* <pre>
*protobuf可以使用message管理其他的message
* </pre>
*
* Protobuf type {@code MyMessage}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:MyMessage)
netty.protobuf2.MyDataInfo.MyMessageOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.internal_static_MyMessage_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return netty.protobuf2.MyDataInfo.internal_static_MyMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
netty.protobuf2.MyDataInfo.MyMessage.class, netty.protobuf2.MyDataInfo.MyMessage.Builder.class);
}
// Construct using netty.protobuf2.MyDataInfo.MyMessage.newBuilder()
private Builder() {
}
private Builder(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
super(parent);
}
@java.lang.Override
public Builder clear() {
super.clear();
bitField0_ = 0;
dataType_ = 0;
if (studentBuilder_ != null) {
studentBuilder_.clear();
}
if (workerBuilder_ != null) {
workerBuilder_.clear();
}
dataBodyCase_ = 0;
dataBody_ = null;
return this;
}
@java.lang.Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return netty.protobuf2.MyDataInfo.internal_static_MyMessage_descriptor;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.MyMessage getDefaultInstanceForType() {
return netty.protobuf2.MyDataInfo.MyMessage.getDefaultInstance();
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.MyMessage build() {
netty.protobuf2.MyDataInfo.MyMessage result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.MyMessage buildPartial() {
netty.protobuf2.MyDataInfo.MyMessage result = new netty.protobuf2.MyDataInfo.MyMessage(this);
if (bitField0_ != 0) { buildPartial0(result); }
buildPartialOneofs(result);
onBuilt();
return result;
}
private void buildPartial0(netty.protobuf2.MyDataInfo.MyMessage result) {
int from_bitField0_ = bitField0_;
if (((from_bitField0_ & 0x00000001) != 0)) {
result.dataType_ = dataType_;
}
}
private void buildPartialOneofs(netty.protobuf2.MyDataInfo.MyMessage result) {
result.dataBodyCase_ = dataBodyCase_;
result.dataBody_ = this.dataBody_;
if (dataBodyCase_ == 2 &&
studentBuilder_ != null) {
result.dataBody_ = studentBuilder_.build();
}
if (dataBodyCase_ == 3 &&
workerBuilder_ != null) {
result.dataBody_ = workerBuilder_.build();
}
}
@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 netty.protobuf2.MyDataInfo.MyMessage) {
return mergeFrom((netty.protobuf2.MyDataInfo.MyMessage)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(netty.protobuf2.MyDataInfo.MyMessage other) {
if (other == netty.protobuf2.MyDataInfo.MyMessage.getDefaultInstance()) return this;
if (other.dataType_ != 0) {
setDataTypeValue(other.getDataTypeValue());
}
switch (other.getDataBodyCase()) {
case STUDENT: {
mergeStudent(other.getStudent());
break;
}
case WORKER: {
mergeWorker(other.getWorker());
break;
}
case DATABODY_NOT_SET: {
break;
}
}
this.mergeUnknownFields(other.getUnknownFields());
onChanged();
return this;
}
@java.lang.Override
public final boolean isInitialized() {
return true;
}
@java.lang.Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
if (extensionRegistry == null) {
throw new java.lang.NullPointerException();
}
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 8: {
dataType_ = input.readEnum();
bitField0_ |= 0x00000001;
break;
} // case 8
case 18: {
input.readMessage(
getStudentFieldBuilder().getBuilder(),
extensionRegistry);
dataBodyCase_ = 2;
break;
} // case 18
case 26: {
input.readMessage(
getWorkerFieldBuilder().getBuilder(),
extensionRegistry);
dataBodyCase_ = 3;
break;
} // case 26
default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag
}
break;
} // default:
} // switch (tag)
} // while (!done)
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.unwrapIOException();
} finally {
onChanged();
} // finally
return this;
}
private int dataBodyCase_ = 0;
private java.lang.Object dataBody_;
public DataBodyCase
getDataBodyCase() {
return DataBodyCase.forNumber(
dataBodyCase_);
}
public Builder clearDataBody() {
dataBodyCase_ = 0;
dataBody_ = null;
onChanged();
return this;
}
private int bitField0_;
private int dataType_ = 0;
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return The enum numeric value on the wire for dataType.
*/
@java.lang.Override public int getDataTypeValue() {
return dataType_;
}
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @param value The enum numeric value on the wire for dataType to set.
* @return This builder for chaining.
*/
public Builder setDataTypeValue(int value) {
dataType_ = value;
bitField0_ |= 0x00000001;
onChanged();
return this;
}
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return The dataType.
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.MyMessage.DataType getDataType() {
netty.protobuf2.MyDataInfo.MyMessage.DataType result = netty.protobuf2.MyDataInfo.MyMessage.DataType.forNumber(dataType_);
return result == null ? netty.protobuf2.MyDataInfo.MyMessage.DataType.UNRECOGNIZED : result;
}
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @param value The dataType to set.
* @return This builder for chaining.
*/
public Builder setDataType(netty.protobuf2.MyDataInfo.MyMessage.DataType value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
dataType_ = value.getNumber();
onChanged();
return this;
}
/**
* <pre>
*用data_type来标识传的是哪一个枚举类型
*这个data_type=1的1 是MyMessage的属性编号!
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
* @return This builder for chaining.
*/
public Builder clearDataType() {
bitField0_ = (bitField0_ & ~0x00000001);
dataType_ = 0;
onChanged();
return this;
}
private com.google.protobuf.SingleFieldBuilderV3<
netty.protobuf2.MyDataInfo.Student, netty.protobuf2.MyDataInfo.Student.Builder, netty.protobuf2.MyDataInfo.StudentOrBuilder> studentBuilder_;
/**
* <code>.Student student = 2;</code>
* @return Whether the student field is set.
*/
@java.lang.Override
public boolean hasStudent() {
return dataBodyCase_ == 2;
}
/**
* <code>.Student student = 2;</code>
* @return The student.
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.Student getStudent() {
if (studentBuilder_ == null) {
if (dataBodyCase_ == 2) {
return (netty.protobuf2.MyDataInfo.Student) dataBody_;
}
return netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
} else {
if (dataBodyCase_ == 2) {
return studentBuilder_.getMessage();
}
return netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
}
}
/**
* <code>.Student student = 2;</code>
*/
public Builder setStudent(netty.protobuf2.MyDataInfo.Student value) {
if (studentBuilder_ == null) {
if (value == null) {
throw new NullPointerException();
}
dataBody_ = value;
onChanged();
} else {
studentBuilder_.setMessage(value);
}
dataBodyCase_ = 2;
return this;
}
/**
* <code>.Student student = 2;</code>
*/
public Builder setStudent(
netty.protobuf2.MyDataInfo.Student.Builder builderForValue) {
if (studentBuilder_ == null) {
dataBody_ = builderForValue.build();
onChanged();
} else {
studentBuilder_.setMessage(builderForValue.build());
}
dataBodyCase_ = 2;
return this;
}
/**
* <code>.Student student = 2;</code>
*/
public Builder mergeStudent(netty.protobuf2.MyDataInfo.Student value) {
if (studentBuilder_ == null) {
if (dataBodyCase_ == 2 &&
dataBody_ != netty.protobuf2.MyDataInfo.Student.getDefaultInstance()) {
dataBody_ = netty.protobuf2.MyDataInfo.Student.newBuilder((netty.protobuf2.MyDataInfo.Student) dataBody_)
.mergeFrom(value).buildPartial();
} else {
dataBody_ = value;
}
onChanged();
} else {
if (dataBodyCase_ == 2) {
studentBuilder_.mergeFrom(value);
} else {
studentBuilder_.setMessage(value);
}
}
dataBodyCase_ = 2;
return this;
}
/**
* <code>.Student student = 2;</code>
*/
public Builder clearStudent() {
if (studentBuilder_ == null) {
if (dataBodyCase_ == 2) {
dataBodyCase_ = 0;
dataBody_ = null;
onChanged();
}
} else {
if (dataBodyCase_ == 2) {
dataBodyCase_ = 0;
dataBody_ = null;
}
studentBuilder_.clear();
}
return this;
}
/**
* <code>.Student student = 2;</code>
*/
public netty.protobuf2.MyDataInfo.Student.Builder getStudentBuilder() {
return getStudentFieldBuilder().getBuilder();
}
/**
* <code>.Student student = 2;</code>
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.StudentOrBuilder getStudentOrBuilder() {
if ((dataBodyCase_ == 2) && (studentBuilder_ != null)) {
return studentBuilder_.getMessageOrBuilder();
} else {
if (dataBodyCase_ == 2) {
return (netty.protobuf2.MyDataInfo.Student) dataBody_;
}
return netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
}
}
/**
* <code>.Student student = 2;</code>
*/
private com.google.protobuf.SingleFieldBuilderV3<
netty.protobuf2.MyDataInfo.Student, netty.protobuf2.MyDataInfo.Student.Builder, netty.protobuf2.MyDataInfo.StudentOrBuilder>
getStudentFieldBuilder() {
if (studentBuilder_ == null) {
if (!(dataBodyCase_ == 2)) {
dataBody_ = netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
}
studentBuilder_ = new com.google.protobuf.SingleFieldBuilderV3<
netty.protobuf2.MyDataInfo.Student, netty.protobuf2.MyDataInfo.Student.Builder, netty.protobuf2.MyDataInfo.StudentOrBuilder>(
(netty.protobuf2.MyDataInfo.Student) dataBody_,
getParentForChildren(),
isClean());
dataBody_ = null;
}
dataBodyCase_ = 2;
onChanged();
return studentBuilder_;
}
private com.google.protobuf.SingleFieldBuilderV3<
netty.protobuf2.MyDataInfo.Worker, netty.protobuf2.MyDataInfo.Worker.Builder, netty.protobuf2.MyDataInfo.WorkerOrBuilder> workerBuilder_;
/**
* <code>.Worker worker = 3;</code>
* @return Whether the worker field is set.
*/
@java.lang.Override
public boolean hasWorker() {
return dataBodyCase_ == 3;
}
/**
* <code>.Worker worker = 3;</code>
* @return The worker.
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.Worker getWorker() {
if (workerBuilder_ == null) {
if (dataBodyCase_ == 3) {
return (netty.protobuf2.MyDataInfo.Worker) dataBody_;
}
return netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
} else {
if (dataBodyCase_ == 3) {
return workerBuilder_.getMessage();
}
return netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
}
}
/**
* <code>.Worker worker = 3;</code>
*/
public Builder setWorker(netty.protobuf2.MyDataInfo.Worker value) {
if (workerBuilder_ == null) {
if (value == null) {
throw new NullPointerException();
}
dataBody_ = value;
onChanged();
} else {
workerBuilder_.setMessage(value);
}
dataBodyCase_ = 3;
return this;
}
/**
* <code>.Worker worker = 3;</code>
*/
public Builder setWorker(
netty.protobuf2.MyDataInfo.Worker.Builder builderForValue) {
if (workerBuilder_ == null) {
dataBody_ = builderForValue.build();
onChanged();
} else {
workerBuilder_.setMessage(builderForValue.build());
}
dataBodyCase_ = 3;
return this;
}
/**
* <code>.Worker worker = 3;</code>
*/
public Builder mergeWorker(netty.protobuf2.MyDataInfo.Worker value) {
if (workerBuilder_ == null) {
if (dataBodyCase_ == 3 &&
dataBody_ != netty.protobuf2.MyDataInfo.Worker.getDefaultInstance()) {
dataBody_ = netty.protobuf2.MyDataInfo.Worker.newBuilder((netty.protobuf2.MyDataInfo.Worker) dataBody_)
.mergeFrom(value).buildPartial();
} else {
dataBody_ = value;
}
onChanged();
} else {
if (dataBodyCase_ == 3) {
workerBuilder_.mergeFrom(value);
} else {
workerBuilder_.setMessage(value);
}
}
dataBodyCase_ = 3;
return this;
}
/**
* <code>.Worker worker = 3;</code>
*/
public Builder clearWorker() {
if (workerBuilder_ == null) {
if (dataBodyCase_ == 3) {
dataBodyCase_ = 0;
dataBody_ = null;
onChanged();
}
} else {
if (dataBodyCase_ == 3) {
dataBodyCase_ = 0;
dataBody_ = null;
}
workerBuilder_.clear();
}
return this;
}
/**
* <code>.Worker worker = 3;</code>
*/
public netty.protobuf2.MyDataInfo.Worker.Builder getWorkerBuilder() {
return getWorkerFieldBuilder().getBuilder();
}
/**
* <code>.Worker worker = 3;</code>
*/
@java.lang.Override
public netty.protobuf2.MyDataInfo.WorkerOrBuilder getWorkerOrBuilder() {
if ((dataBodyCase_ == 3) && (workerBuilder_ != null)) {
return workerBuilder_.getMessageOrBuilder();
} else {
if (dataBodyCase_ == 3) {
return (netty.protobuf2.MyDataInfo.Worker) dataBody_;
}
return netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
}
}
/**
* <code>.Worker worker = 3;</code>
*/
private com.google.protobuf.SingleFieldBuilderV3<
netty.protobuf2.MyDataInfo.Worker, netty.protobuf2.MyDataInfo.Worker.Builder, netty.protobuf2.MyDataInfo.WorkerOrBuilder>
getWorkerFieldBuilder() {
if (workerBuilder_ == null) {
if (!(dataBodyCase_ == 3)) {
dataBody_ = netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
}
workerBuilder_ = new com.google.protobuf.SingleFieldBuilderV3<
netty.protobuf2.MyDataInfo.Worker, netty.protobuf2.MyDataInfo.Worker.Builder, netty.protobuf2.MyDataInfo.WorkerOrBuilder>(
(netty.protobuf2.MyDataInfo.Worker) dataBody_,
getParentForChildren(),
isClean());
dataBody_ = null;
}
dataBodyCase_ = 3;
onChanged();
return workerBuilder_;
}
@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:MyMessage)
}
// @@protoc_insertion_point(class_scope:MyMessage)
private static final netty.protobuf2.MyDataInfo.MyMessage DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new netty.protobuf2.MyDataInfo.MyMessage();
}
public static netty.protobuf2.MyDataInfo.MyMessage getDefaultInstance() {
return DEFAULT_INSTANCE;
}
private static final com.google.protobuf.Parser<MyMessage>
PARSER = new com.google.protobuf.AbstractParser<MyMessage>() {
@java.lang.Override
public MyMessage parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
Builder builder = newBuilder();
try {
builder.mergeFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(builder.buildPartial());
} catch (com.google.protobuf.UninitializedMessageException e) {
throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(e)
.setUnfinishedMessage(builder.buildPartial());
}
return builder.buildPartial();
}
};
public static com.google.protobuf.Parser<MyMessage> parser() {
return PARSER;
}
@java.lang.Override
public com.google.protobuf.Parser<MyMessage> getParserForType() {
return PARSER;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.MyMessage getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
public interface StudentOrBuilder extends
// @@protoc_insertion_point(interface_extends:Student)
com.google.protobuf.MessageOrBuilder {
/**
* <pre>
*Student类的属性
* </pre>
*
* <code>int32 id = 1;</code>
* @return The id.
*/
int getId();
/**
* <code>string name = 2;</code>
* @return The name.
*/
java.lang.String getName();
/**
* <code>string name = 2;</code>
* @return The bytes for name.
*/
com.google.protobuf.ByteString
getNameBytes();
}
/**
* Protobuf type {@code Student}
*/
public static final class Student extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:Student)
StudentOrBuilder {
private static final long serialVersionUID = 0L;
// Use Student.newBuilder() to construct.
private Student(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private Student() {
name_ = "";
}
@java.lang.Override
@SuppressWarnings({"unused"})
protected java.lang.Object newInstance(
UnusedPrivateParameter unused) {
return new Student();
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.internal_static_Student_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return netty.protobuf2.MyDataInfo.internal_static_Student_fieldAccessorTable
.ensureFieldAccessorsInitialized(
netty.protobuf2.MyDataInfo.Student.class, netty.protobuf2.MyDataInfo.Student.Builder.class);
}
public static final int ID_FIELD_NUMBER = 1;
private int id_ = 0;
/**
* <pre>
*Student类的属性
* </pre>
*
* <code>int32 id = 1;</code>
* @return The id.
*/
@java.lang.Override
public int getId() {
return id_;
}
public static final int NAME_FIELD_NUMBER = 2;
@SuppressWarnings("serial")
private volatile java.lang.Object name_ = "";
/**
* <code>string name = 2;</code>
* @return The name.
*/
@java.lang.Override
public java.lang.String getName() {
java.lang.Object ref = name_;
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();
name_ = s;
return s;
}
}
/**
* <code>string name = 2;</code>
* @return The bytes for name.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = 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;
memoizedIsInitialized = 1;
return true;
}
@java.lang.Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (id_ != 0) {
output.writeInt32(1, id_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 2, name_);
}
getUnknownFields().writeTo(output);
}
@java.lang.Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (id_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, id_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, name_);
}
size += getUnknownFields().getSerializedSize();
memoizedSize = size;
return size;
}
@java.lang.Override
public boolean equals(final java.lang.Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof netty.protobuf2.MyDataInfo.Student)) {
return super.equals(obj);
}
netty.protobuf2.MyDataInfo.Student other = (netty.protobuf2.MyDataInfo.Student) obj;
if (getId()
!= other.getId()) return false;
if (!getName()
.equals(other.getName())) return false;
if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true;
}
@java.lang.Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
hash = (37 * hash) + ID_FIELD_NUMBER;
hash = (53 * hash) + getId();
hash = (37 * hash) + NAME_FIELD_NUMBER;
hash = (53 * hash) + getName().hashCode();
hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash;
return hash;
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.Student parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.Student 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 netty.protobuf2.MyDataInfo.Student parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.Student 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 netty.protobuf2.MyDataInfo.Student parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.Student 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(netty.protobuf2.MyDataInfo.Student 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 Student}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:Student)
netty.protobuf2.MyDataInfo.StudentOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.internal_static_Student_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return netty.protobuf2.MyDataInfo.internal_static_Student_fieldAccessorTable
.ensureFieldAccessorsInitialized(
netty.protobuf2.MyDataInfo.Student.class, netty.protobuf2.MyDataInfo.Student.Builder.class);
}
// Construct using netty.protobuf2.MyDataInfo.Student.newBuilder()
private Builder() {
}
private Builder(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
super(parent);
}
@java.lang.Override
public Builder clear() {
super.clear();
bitField0_ = 0;
id_ = 0;
name_ = "";
return this;
}
@java.lang.Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return netty.protobuf2.MyDataInfo.internal_static_Student_descriptor;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Student getDefaultInstanceForType() {
return netty.protobuf2.MyDataInfo.Student.getDefaultInstance();
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Student build() {
netty.protobuf2.MyDataInfo.Student result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Student buildPartial() {
netty.protobuf2.MyDataInfo.Student result = new netty.protobuf2.MyDataInfo.Student(this);
if (bitField0_ != 0) { buildPartial0(result); }
onBuilt();
return result;
}
private void buildPartial0(netty.protobuf2.MyDataInfo.Student result) {
int from_bitField0_ = bitField0_;
if (((from_bitField0_ & 0x00000001) != 0)) {
result.id_ = id_;
}
if (((from_bitField0_ & 0x00000002) != 0)) {
result.name_ = name_;
}
}
@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 netty.protobuf2.MyDataInfo.Student) {
return mergeFrom((netty.protobuf2.MyDataInfo.Student)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(netty.protobuf2.MyDataInfo.Student other) {
if (other == netty.protobuf2.MyDataInfo.Student.getDefaultInstance()) return this;
if (other.getId() != 0) {
setId(other.getId());
}
if (!other.getName().isEmpty()) {
name_ = other.name_;
bitField0_ |= 0x00000002;
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
onChanged();
return this;
}
@java.lang.Override
public final boolean isInitialized() {
return true;
}
@java.lang.Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
if (extensionRegistry == null) {
throw new java.lang.NullPointerException();
}
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 8: {
id_ = input.readInt32();
bitField0_ |= 0x00000001;
break;
} // case 8
case 18: {
name_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000002;
break;
} // case 18
default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag
}
break;
} // default:
} // switch (tag)
} // while (!done)
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.unwrapIOException();
} finally {
onChanged();
} // finally
return this;
}
private int bitField0_;
private int id_ ;
/**
* <pre>
*Student类的属性
* </pre>
*
* <code>int32 id = 1;</code>
* @return The id.
*/
@java.lang.Override
public int getId() {
return id_;
}
/**
* <pre>
*Student类的属性
* </pre>
*
* <code>int32 id = 1;</code>
* @param value The id to set.
* @return This builder for chaining.
*/
public Builder setId(int value) {
id_ = value;
bitField0_ |= 0x00000001;
onChanged();
return this;
}
/**
* <pre>
*Student类的属性
* </pre>
*
* <code>int32 id = 1;</code>
* @return This builder for chaining.
*/
public Builder clearId() {
bitField0_ = (bitField0_ & ~0x00000001);
id_ = 0;
onChanged();
return this;
}
private java.lang.Object name_ = "";
/**
* <code>string name = 2;</code>
* @return The name.
*/
public java.lang.String getName() {
java.lang.Object ref = name_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
name_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>string name = 2;</code>
* @return The bytes for name.
*/
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>string name = 2;</code>
* @param value The name to set.
* @return This builder for chaining.
*/
public Builder setName(
java.lang.String value) {
if (value == null) { throw new NullPointerException(); }
name_ = value;
bitField0_ |= 0x00000002;
onChanged();
return this;
}
/**
* <code>string name = 2;</code>
* @return This builder for chaining.
*/
public Builder clearName() {
name_ = getDefaultInstance().getName();
bitField0_ = (bitField0_ & ~0x00000002);
onChanged();
return this;
}
/**
* <code>string name = 2;</code>
* @param value The bytes for name to set.
* @return This builder for chaining.
*/
public Builder setNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) { throw new NullPointerException(); }
checkByteStringIsUtf8(value);
name_ = value;
bitField0_ |= 0x00000002;
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:Student)
}
// @@protoc_insertion_point(class_scope:Student)
private static final netty.protobuf2.MyDataInfo.Student DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new netty.protobuf2.MyDataInfo.Student();
}
public static netty.protobuf2.MyDataInfo.Student getDefaultInstance() {
return DEFAULT_INSTANCE;
}
private static final com.google.protobuf.Parser<Student>
PARSER = new com.google.protobuf.AbstractParser<Student>() {
@java.lang.Override
public Student parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
Builder builder = newBuilder();
try {
builder.mergeFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(builder.buildPartial());
} catch (com.google.protobuf.UninitializedMessageException e) {
throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(e)
.setUnfinishedMessage(builder.buildPartial());
}
return builder.buildPartial();
}
};
public static com.google.protobuf.Parser<Student> parser() {
return PARSER;
}
@java.lang.Override
public com.google.protobuf.Parser<Student> getParserForType() {
return PARSER;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Student getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
public interface WorkerOrBuilder extends
// @@protoc_insertion_point(interface_extends:Worker)
com.google.protobuf.MessageOrBuilder {
/**
* <code>string name = 1;</code>
* @return The name.
*/
java.lang.String getName();
/**
* <code>string name = 1;</code>
* @return The bytes for name.
*/
com.google.protobuf.ByteString
getNameBytes();
/**
* <code>int32 age = 2;</code>
* @return The age.
*/
int getAge();
}
/**
* Protobuf type {@code Worker}
*/
public static final class Worker extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:Worker)
WorkerOrBuilder {
private static final long serialVersionUID = 0L;
// Use Worker.newBuilder() to construct.
private Worker(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private Worker() {
name_ = "";
}
@java.lang.Override
@SuppressWarnings({"unused"})
protected java.lang.Object newInstance(
UnusedPrivateParameter unused) {
return new Worker();
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.internal_static_Worker_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return netty.protobuf2.MyDataInfo.internal_static_Worker_fieldAccessorTable
.ensureFieldAccessorsInitialized(
netty.protobuf2.MyDataInfo.Worker.class, netty.protobuf2.MyDataInfo.Worker.Builder.class);
}
public static final int NAME_FIELD_NUMBER = 1;
@SuppressWarnings("serial")
private volatile java.lang.Object name_ = "";
/**
* <code>string name = 1;</code>
* @return The name.
*/
@java.lang.Override
public java.lang.String getName() {
java.lang.Object ref = name_;
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();
name_ = s;
return s;
}
}
/**
* <code>string name = 1;</code>
* @return The bytes for name.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int AGE_FIELD_NUMBER = 2;
private int age_ = 0;
/**
* <code>int32 age = 2;</code>
* @return The age.
*/
@java.lang.Override
public int getAge() {
return age_;
}
private byte memoizedIsInitialized = -1;
@java.lang.Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
@java.lang.Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_);
}
if (age_ != 0) {
output.writeInt32(2, age_);
}
getUnknownFields().writeTo(output);
}
@java.lang.Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, name_);
}
if (age_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, age_);
}
size += getUnknownFields().getSerializedSize();
memoizedSize = size;
return size;
}
@java.lang.Override
public boolean equals(final java.lang.Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof netty.protobuf2.MyDataInfo.Worker)) {
return super.equals(obj);
}
netty.protobuf2.MyDataInfo.Worker other = (netty.protobuf2.MyDataInfo.Worker) obj;
if (!getName()
.equals(other.getName())) return false;
if (getAge()
!= other.getAge()) return false;
if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true;
}
@java.lang.Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
hash = (37 * hash) + NAME_FIELD_NUMBER;
hash = (53 * hash) + getName().hashCode();
hash = (37 * hash) + AGE_FIELD_NUMBER;
hash = (53 * hash) + getAge();
hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash;
return hash;
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static netty.protobuf2.MyDataInfo.Worker parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.Worker 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 netty.protobuf2.MyDataInfo.Worker parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.Worker 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 netty.protobuf2.MyDataInfo.Worker parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static netty.protobuf2.MyDataInfo.Worker 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(netty.protobuf2.MyDataInfo.Worker 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 Worker}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:Worker)
netty.protobuf2.MyDataInfo.WorkerOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return netty.protobuf2.MyDataInfo.internal_static_Worker_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return netty.protobuf2.MyDataInfo.internal_static_Worker_fieldAccessorTable
.ensureFieldAccessorsInitialized(
netty.protobuf2.MyDataInfo.Worker.class, netty.protobuf2.MyDataInfo.Worker.Builder.class);
}
// Construct using netty.protobuf2.MyDataInfo.Worker.newBuilder()
private Builder() {
}
private Builder(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
super(parent);
}
@java.lang.Override
public Builder clear() {
super.clear();
bitField0_ = 0;
name_ = "";
age_ = 0;
return this;
}
@java.lang.Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return netty.protobuf2.MyDataInfo.internal_static_Worker_descriptor;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Worker getDefaultInstanceForType() {
return netty.protobuf2.MyDataInfo.Worker.getDefaultInstance();
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Worker build() {
netty.protobuf2.MyDataInfo.Worker result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Worker buildPartial() {
netty.protobuf2.MyDataInfo.Worker result = new netty.protobuf2.MyDataInfo.Worker(this);
if (bitField0_ != 0) { buildPartial0(result); }
onBuilt();
return result;
}
private void buildPartial0(netty.protobuf2.MyDataInfo.Worker result) {
int from_bitField0_ = bitField0_;
if (((from_bitField0_ & 0x00000001) != 0)) {
result.name_ = name_;
}
if (((from_bitField0_ & 0x00000002) != 0)) {
result.age_ = age_;
}
}
@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 netty.protobuf2.MyDataInfo.Worker) {
return mergeFrom((netty.protobuf2.MyDataInfo.Worker)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(netty.protobuf2.MyDataInfo.Worker other) {
if (other == netty.protobuf2.MyDataInfo.Worker.getDefaultInstance()) return this;
if (!other.getName().isEmpty()) {
name_ = other.name_;
bitField0_ |= 0x00000001;
onChanged();
}
if (other.getAge() != 0) {
setAge(other.getAge());
}
this.mergeUnknownFields(other.getUnknownFields());
onChanged();
return this;
}
@java.lang.Override
public final boolean isInitialized() {
return true;
}
@java.lang.Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
if (extensionRegistry == null) {
throw new java.lang.NullPointerException();
}
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 10: {
name_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000001;
break;
} // case 10
case 16: {
age_ = input.readInt32();
bitField0_ |= 0x00000002;
break;
} // case 16
default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag
}
break;
} // default:
} // switch (tag)
} // while (!done)
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.unwrapIOException();
} finally {
onChanged();
} // finally
return this;
}
private int bitField0_;
private java.lang.Object name_ = "";
/**
* <code>string name = 1;</code>
* @return The name.
*/
public java.lang.String getName() {
java.lang.Object ref = name_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
name_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>string name = 1;</code>
* @return The bytes for name.
*/
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>string name = 1;</code>
* @param value The name to set.
* @return This builder for chaining.
*/
public Builder setName(
java.lang.String value) {
if (value == null) { throw new NullPointerException(); }
name_ = value;
bitField0_ |= 0x00000001;
onChanged();
return this;
}
/**
* <code>string name = 1;</code>
* @return This builder for chaining.
*/
public Builder clearName() {
name_ = getDefaultInstance().getName();
bitField0_ = (bitField0_ & ~0x00000001);
onChanged();
return this;
}
/**
* <code>string name = 1;</code>
* @param value The bytes for name to set.
* @return This builder for chaining.
*/
public Builder setNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) { throw new NullPointerException(); }
checkByteStringIsUtf8(value);
name_ = value;
bitField0_ |= 0x00000001;
onChanged();
return this;
}
private int age_ ;
/**
* <code>int32 age = 2;</code>
* @return The age.
*/
@java.lang.Override
public int getAge() {
return age_;
}
/**
* <code>int32 age = 2;</code>
* @param value The age to set.
* @return This builder for chaining.
*/
public Builder setAge(int value) {
age_ = value;
bitField0_ |= 0x00000002;
onChanged();
return this;
}
/**
* <code>int32 age = 2;</code>
* @return This builder for chaining.
*/
public Builder clearAge() {
bitField0_ = (bitField0_ & ~0x00000002);
age_ = 0;
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:Worker)
}
// @@protoc_insertion_point(class_scope:Worker)
private static final netty.protobuf2.MyDataInfo.Worker DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new netty.protobuf2.MyDataInfo.Worker();
}
public static netty.protobuf2.MyDataInfo.Worker getDefaultInstance() {
return DEFAULT_INSTANCE;
}
private static final com.google.protobuf.Parser<Worker>
PARSER = new com.google.protobuf.AbstractParser<Worker>() {
@java.lang.Override
public Worker parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
Builder builder = newBuilder();
try {
builder.mergeFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(builder.buildPartial());
} catch (com.google.protobuf.UninitializedMessageException e) {
throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(e)
.setUnfinishedMessage(builder.buildPartial());
}
return builder.buildPartial();
}
};
public static com.google.protobuf.Parser<Worker> parser() {
return PARSER;
}
@java.lang.Override
public com.google.protobuf.Parser<Worker> getParserForType() {
return PARSER;
}
@java.lang.Override
public netty.protobuf2.MyDataInfo.Worker getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_MyMessage_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_MyMessage_fieldAccessorTable;
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_Student_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_Student_fieldAccessorTable;
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_Worker_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_Worker_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\rStudent.proto\"\244\001\n\tMyMessage\022&\n\tdata_ty" +
"pe\030\001 \001(\0162\023.MyMessage.DataType\022\033\n\007student" +
"\030\002 \001(\0132\010.StudentH\000\022\031\n\006worker\030\003 \001(\0132\007.Wor" +
"kerH\000\"+\n\010DataType\022\017\n\013StudentType\020\000\022\016\n\nWo" +
"rkerType\020\001B\n\n\010dataBody\"#\n\007Student\022\n\n\002id\030" +
"\001 \001(\005\022\014\n\004name\030\002 \001(\t\"#\n\006Worker\022\014\n\004name\030\001 " +
"\001(\t\022\013\n\003age\030\002 \001(\005B\037\n\017netty.protobuf2B\nMyD" +
"ataInfoH\001b\006proto3"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_MyMessage_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_MyMessage_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_MyMessage_descriptor,
new java.lang.String[] { "DataType", "Student", "Worker", "DataBody", });
internal_static_Student_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_Student_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_Student_descriptor,
new java.lang.String[] { "Id", "Name", });
internal_static_Worker_descriptor =
getDescriptor().getMessageTypes().get(2);
internal_static_Worker_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_Worker_descriptor,
new java.lang.String[] { "Name", "Age", });
}
// @@protoc_insertion_point(outer_class_scope)
}
3、 NettyServer.java;
package netty.protobuf2;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
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;
public class NettyServer {
public static void main(String[] args) throws Exception {
//创建BossGroup和WorkerGroup
//说明
//1. 创建两个线程组bossGroup和workerGroup
//2. bossGroup它只是处理连接请求,真正的与客户端业务处理会交给workerGroup去完成
//3. 两个都是无限循环
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(8);
try {
//创建服务器端的启动对象,配置启动参数
ServerBootstrap bootstrap = new ServerBootstrap();
//使用链式编程来进行设置
bootstrap.group(bossGroup, workerGroup) //设置两个线程组
.channel(NioServerSocketChannel.class) //使用NioServerSocketChannel作为服务器的通道实现
.childHandler(new ChannelInitializer<SocketChannel>() { //创建一个通道初始化对象
//给pipeline设置处理器
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//在pipeline加入ProtobufDecoder
//指定对哪种对象进行解码
pipeline.addLast("decoder", new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()));
//向管道的最后增加一个处理器
pipeline.addLast(new NettyChannelHandler());
};
}); //给我们的workerGroup的EventLoop对应的管道设置处理器
//bossGroup参数
bootstrap.option(ChannelOption.SO_BACKLOG, 1024); //设置线程队列等待连接的个数
//workerGroup参数
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); //设置保持活动连接状态
System.out.println("...服务器 is ready...");
//绑定一个端口并且同步,生成了一个ChannelFuture对象
//启动服务器并绑定端口
ChannelFuture cf = bootstrap.bind(6668).sync();
//对关闭通道进行监听
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Shutdown Netty Server...");
//优雅的关闭
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
System.out.println("Shutdown Netty Server Success!");
}
}
}
4、 NettyChannelHandler.java;
package netty.protobuf2;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* 说明
* 1. 我们自定义一个Handler,需要继承netty规定好的某个HandlerAdapter(规范)
* 2. 这时我们自定义一个Handler,才能称之为Handler
*
*/
public class NettyChannelHandler extends ChannelInboundHandlerAdapter {
//读取数据的事件(这里我们可以读取客户端发送的消息)
/*
* 1. ChannelHandlerContext ctx:上下文对象,含有管道pipeline,通道channel,地址
* 2. Object msg:就是客户端发送的数据,默认是Object
* 3. 通道读写数据,管道处理数据
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
MyDataInfo.MyMessage message = (MyDataInfo.MyMessage)msg;
//根据dataType来显示不同的信息
MyDataInfo.MyMessage.DataType dataType = message.getDataType();
if (dataType == MyDataInfo.MyMessage.DataType.StudentType) {
MyDataInfo.Student student = message.getStudent();
System.out.println("客户端发送的student id=" + student.getId() + " name=" + student.getName());
} else if (dataType == MyDataInfo.MyMessage.DataType.WorkerType) {
MyDataInfo.Worker worker = message.getWorker();
System.out.println("客户端发送的worker age=" + worker.getAge() + " name=" + worker.getName());
} else {
System.out.println("传输的类型不正确!");
}
}
//数据读取完毕
//这个方法会在channelRead读完后触发
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//把数据写到缓冲区,并且刷新缓冲区,是write + flush
//一般来讲,我们对这个发送的数据进行编码
ctx.channel().writeAndFlush(Unpooled.copiedBuffer("hello,客户端~", CharsetUtil.UTF_8));
}
//处理异常,一般是需要关闭通道
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.channel().close();
}
}
5、 NettyClient.java;
package netty.protobuf2;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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.ProtobufEncoder;
public class NettyClient {
public static void main(String[] args) throws Exception {
//客户端需要一个事件循环组
EventLoopGroup group = new NioEventLoopGroup();
try {
//创建客户端的启动对象
//注意客户端使用的是Bootstrap
Bootstrap bootstrap = new Bootstrap();
//设置相关参数
bootstrap.group(group) //设置线程组
.channel(NioSocketChannel.class) //设置客户端通道的实现类
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//在pipeline中加入ProtobufEncoder
pipeline.addLast("encoder", new ProtobufEncoder());
//加入自己的处理器
pipeline.addLast(new NettyClientHandler());
}
});
System.out.println("...客户端 is ready...");
//启动客户端去连接服务器端
//ChannelFuture涉及到netty的异步模型
ChannelFuture cf = bootstrap.connect("127.0.0.1", 6668).sync();
//对关闭通道进行监听
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
//优雅的关闭
group.shutdownGracefully();
}
}
}
6、 NettyClientHandler.java;
package netty.protobuf2;
import java.util.Random;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
//当通道就绪时,就会触发该方法
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//随机的发送Student或者Worker对象
int i = new Random().nextInt(3);
MyDataInfo.MyMessage myMessage = null;
if (0 == i) {
myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.StudentType)
.setStudent(
MyDataInfo.Student.newBuilder().setId(10).setName("张三").build()
).build();
} else {
//发送一个Worker对象
myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.WorkerType)
.setWorker(
MyDataInfo.Worker.newBuilder().setAge(20).setName("李四").build()
).build();
}
ctx.writeAndFlush(myMessage);
}
//当通道有读取事件时会触发
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.out.println("服务器回复的消息:" + buf.toString(CharsetUtil.UTF_8));
System.out.println("服务器的地址:" + ctx.channel().remoteAddress());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
三、执行
1、 服务端日志;
客户端发送的student id=10 name=张三
客户端发送的worker age=20 name=李四
客户端发送的worker age=20 name=李四