Protobuf快速入门实例2
编写程序,使用Protobuf完成如下功能
客户端可以随机发送Student PoJo/ Worker PoJo 对象到服务器 (通过 Protobuf 编码)
服务端能接收Student PoJo/ Worker PoJo 对象(需要判断是哪种类型),并显示信息(通过 Protobuf 解码)
syntax = "proto3";
option optimize_for = SPEED; // 加快解析
option java_package="com.atguigu.netty.codec2"; //指定生成到哪个包下
option java_outer_classname="MyDataInfo"; // 外部类名, 文件名
//protobuf 可以使用message 管理其他的message
message MyMessage {
//定义一个枚举类型
enum DataType {
StudentType = 0; //在proto3 要求enum的编号从0开始
WorkerType = 1;
}
//用data_type 来标识传的是哪一个枚举类型
DataType data_type = 1;
//表示每次枚举类型最多只能出现其中的一个, 节省空间
oneof dataBody {
Student student = 2;
Worker worker = 3;
}
}
message Student {
int32 id = 1;//Student类的属性
string name = 2; //
}
message Worker {
string name=1;
int32 age=2;
}
//
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: Student.proto
package com.my.netty.codec2;
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 来标识传的是哪一个枚举类型
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
*/
int getDataTypeValue();
/**
* <pre>
*用data_type 来标识传的是哪一个枚举类型
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
*/
MyMessage.DataType getDataType();
/**
* <code>.Student student = 2;</code>
*/
boolean hasStudent();
/**
* <code>.Student student = 2;</code>
*/
Student getStudent();
/**
* <code>.Student student = 2;</code>
*/
StudentOrBuilder getStudentOrBuilder();
/**
* <code>.Worker worker = 3;</code>
*/
boolean hasWorker();
/**
* <code>.Worker worker = 3;</code>
*/
Worker getWorker();
/**
* <code>.Worker worker = 3;</code>
*/
WorkerOrBuilder getWorkerOrBuilder();
public 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;
}
@Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private MyMessage(
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: {
int rawValue = input.readEnum();
dataType_ = rawValue;
break;
}
case 18: {
Student.Builder subBuilder = null;
if (dataBodyCase_ == 2) {
subBuilder = ((Student) dataBody_).toBuilder();
}
dataBody_ =
input.readMessage(Student.parser(), extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom((Student) dataBody_);
dataBody_ = subBuilder.buildPartial();
}
dataBodyCase_ = 2;
break;
}
case 26: {
Worker.Builder subBuilder = null;
if (dataBodyCase_ == 3) {
subBuilder = ((Worker) dataBody_).toBuilder();
}
dataBody_ =
input.readMessage(Worker.parser(), extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom((Worker) dataBody_);
dataBody_ = subBuilder.buildPartial();
}
dataBodyCase_ = 3;
break;
}
default: {
if (!parseUnknownFieldProto3(
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 MyDataInfo.internal_static_MyMessage_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return MyDataInfo.internal_static_MyMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
MyMessage.class, 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),
/**
* <code>WorkerType = 1;</code>
*/
WorkerType(1),
UNRECOGNIZED(-1),
;
/**
* <pre>
*在proto3 要求enum的编号从0开始
* </pre>
*
* <code>StudentType = 0;</code>
*/
public static final int StudentType_VALUE = 0;
/**
* <code>WorkerType = 1;</code>
*/
public static final int WorkerType_VALUE = 1;
public final int getNumber() {
if (this == UNRECOGNIZED) {
throw new IllegalArgumentException(
"Can't get the number of an unknown enum value.");
}
return value;
}
/**
* @deprecated Use {@link #forNumber(int)} instead.
*/
@Deprecated
public static DataType valueOf(int value) {
return forNumber(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() {
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 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 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 Object dataBody_;
public enum DataBodyCase
implements com.google.protobuf.Internal.EnumLite {
STUDENT(2),
WORKER(3),
DATABODY_NOT_SET(0);
private final int value;
private DataBodyCase(int value) {
this.value = value;
}
/**
* @deprecated Use {@link #forNumber(int)} instead.
*/
@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_;
/**
* <pre>
*用data_type 来标识传的是哪一个枚举类型
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
*/
public int getDataTypeValue() {
return dataType_;
}
/**
* <pre>
*用data_type 来标识传的是哪一个枚举类型
* </pre>
*
* <code>.MyMessage.DataType data_type = 1;</code>
*/
public DataType getDataType() {
@SuppressWarnings("deprecation")
DataType result = DataType.valueOf(dataType_);
return result == null ? DataType.UNRECOGNIZED : result;
}
public static final int STUDENT_FIELD_NUMBER = 2;
/**
* <code>.Student student = 2;</code>
*/
public boolean hasStudent() {
return dataBodyCase_ == 2;
}
/**
* <code>.Student student = 2;</code>
*/
public Student getStudent() {
if (dataBodyCase_ == 2) {
return (Student) dataBody_;
}
return Student.getDefaultInstance();
}
/**
* <code>.Student student = 2;</code>
*/
public StudentOrBuilder getStudentOrBuilder() {
if (dataBodyCase_ == 2) {
return (Student) dataBody_;
}
return Student.getDefaultInstance();
}
public static final int WORKER_FIELD_NUMBER = 3;
/**
* <code>.Worker worker = 3;</code>
*/
public boolean hasWorker() {
return dataBodyCase_ == 3;
}
/**
* <code>.Worker worker = 3;</code>
*/
public Worker getWorker() {
if (dataBodyCase_ == 3) {
return (Worker) dataBody_;
}
return Worker.getDefaultInstance();
}
/**
* <code>.Worker worker = 3;</code>
*/
public WorkerOrBuilder getWorkerOrBuilder() {
if (dataBodyCase_ == 3) {
return (Worker) dataBody_;
}
return Worker.getDefaultInstance();
}
private byte memoizedIsInitialized = -1;
@Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
@Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (dataType_ != DataType.StudentType.getNumber()) {
output.writeEnum(1, dataType_);
}
if (dataBodyCase_ == 2) {
output.writeMessage(2, (Student) dataBody_);
}
if (dataBodyCase_ == 3) {
output.writeMessage(3, (Worker) dataBody_);
}
unknownFields.writeTo(output);
}
@Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (dataType_ != DataType.StudentType.getNumber()) {
size += com.google.protobuf.CodedOutputStream
.computeEnumSize(1, dataType_);
}
if (dataBodyCase_ == 2) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(2, (Student) dataBody_);
}
if (dataBodyCase_ == 3) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(3, (Worker) dataBody_);
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
}
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof MyMessage)) {
return super.equals(obj);
}
MyMessage other = (MyMessage) obj;
boolean result = true;
result = result && dataType_ == other.dataType_;
result = result && getDataBodyCase().equals(
other.getDataBodyCase());
if (!result) return false;
switch (dataBodyCase_) {
case 2:
result = result && getStudent()
.equals(other.getStudent());
break;
case 3:
result = result && getWorker()
.equals(other.getWorker());
break;
case 0:
default:
}
result = result && unknownFields.equals(other.unknownFields);
return result;
}
@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) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
}
public static MyMessage parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static MyMessage parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static MyMessage parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static MyMessage parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static MyMessage parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static MyMessage parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static MyMessage parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static MyMessage parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOExcept