一、protobuf环境搭建

Github 地址: https://github.com/protocolbuffers/protobuf

然后进入下载页 https://github.com/protocolbuffers/protobuf/releases

下载系统对应版本编译器

我这里使用的是window 64位

下好之后解压,然后把bin里面的protoc.exe加入到环境变量,

并且把protoc.exe拷贝到C:\Windows\System32

二、创建Student.proto 文件

syntax = "proto2";

package com.example.protobuf;

option optimize_for = SPEED;
option java_package = "com.example.protobuf";
option java_outer_classname = "DataInfo"; message Student{
required string name = 1;
optional int32 age = 2;
optional string address = 3; }

  

在工程中增加依赖

  <dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.9.1</version>
</dependency>

  

然后使用protoc命令生成类

D:\workspace\study\basic\netty_demo>protoc --java_out=src/main/java  src/protobuf/Student.proto

最终生成的DataInfo文件

DataInfo.java

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: src/protobuf/Student.proto package com.example.protobuf; public final class DataInfo {
private DataInfo() {}
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 StudentOrBuilder extends
// @@protoc_insertion_point(interface_extends:com.example.protobuf.Student)
com.google.protobuf.MessageOrBuilder { /**
* <code>required string name = 1;</code>
*/
boolean hasName();
/**
* <code>required string name = 1;</code>
*/
java.lang.String getName();
/**
* <code>required string name = 1;</code>
*/
com.google.protobuf.ByteString
getNameBytes(); /**
* <code>optional int32 age = 2;</code>
*/
boolean hasAge();
/**
* <code>optional int32 age = 2;</code>
*/
int getAge(); /**
* <code>optional string address = 3;</code>
*/
boolean hasAddress();
/**
* <code>optional string address = 3;</code>
*/
java.lang.String getAddress();
/**
* <code>optional string address = 3;</code>
*/
com.google.protobuf.ByteString
getAddressBytes();
}
/**
* Protobuf type {@code com.example.protobuf.Student}
*/
public static final class Student extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:com.example.protobuf.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_ = "";
address_ = "";
} @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;
}
private Student(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
this();
if (extensionRegistry == null) {
throw new java.lang.NullPointerException();
}
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 10: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000001;
name_ = bs;
break;
}
case 16: {
bitField0_ |= 0x00000002;
age_ = input.readInt32();
break;
}
case 26: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000004;
address_ = 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 com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_descriptor;
} @java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_fieldAccessorTable
.ensureFieldAccessorsInitialized(
com.example.protobuf.DataInfo.Student.class, com.example.protobuf.DataInfo.Student.Builder.class);
} private int bitField0_;
public static final int NAME_FIELD_NUMBER = 1;
private volatile java.lang.Object name_;
/**
* <code>required string name = 1;</code>
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>required string name = 1;</code>
*/
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();
if (bs.isValidUtf8()) {
name_ = s;
}
return s;
}
}
/**
* <code>required string name = 1;</code>
*/
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_;
/**
* <code>optional int32 age = 2;</code>
*/
public boolean hasAge() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>optional int32 age = 2;</code>
*/
public int getAge() {
return age_;
} public static final int ADDRESS_FIELD_NUMBER = 3;
private volatile java.lang.Object address_;
/**
* <code>optional string address = 3;</code>
*/
public boolean hasAddress() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>optional string address = 3;</code>
*/
public java.lang.String getAddress() {
java.lang.Object ref = address_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
address_ = s;
}
return s;
}
}
/**
* <code>optional string address = 3;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes() {
java.lang.Object ref = address_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
address_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} private byte memoizedIsInitialized = -1;
@java.lang.Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false; if (!hasName()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
} @java.lang.Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (((bitField0_ & 0x00000001) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_);
}
if (((bitField0_ & 0x00000002) != 0)) {
output.writeInt32(2, age_);
}
if (((bitField0_ & 0x00000004) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, address_);
}
unknownFields.writeTo(output);
} @java.lang.Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size; size = 0;
if (((bitField0_ & 0x00000001) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, name_);
}
if (((bitField0_ & 0x00000002) != 0)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, age_);
}
if (((bitField0_ & 0x00000004) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, address_);
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
} @java.lang.Override
public boolean equals(final java.lang.Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof com.example.protobuf.DataInfo.Student)) {
return super.equals(obj);
}
com.example.protobuf.DataInfo.Student other = (com.example.protobuf.DataInfo.Student) obj; if (hasName() != other.hasName()) return false;
if (hasName()) {
if (!getName()
.equals(other.getName())) return false;
}
if (hasAge() != other.hasAge()) return false;
if (hasAge()) {
if (getAge()
!= other.getAge()) return false;
}
if (hasAddress() != other.hasAddress()) return false;
if (hasAddress()) {
if (!getAddress()
.equals(other.getAddress())) return false;
}
if (!unknownFields.equals(other.unknownFields)) return false;
return true;
} @java.lang.Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
if (hasName()) {
hash = (37 * hash) + NAME_FIELD_NUMBER;
hash = (53 * hash) + getName().hashCode();
}
if (hasAge()) {
hash = (37 * hash) + AGE_FIELD_NUMBER;
hash = (53 * hash) + getAge();
}
if (hasAddress()) {
hash = (37 * hash) + ADDRESS_FIELD_NUMBER;
hash = (53 * hash) + getAddress().hashCode();
}
hash = (29 * hash) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
} public static com.example.protobuf.DataInfo.Student parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static com.example.protobuf.DataInfo.Student parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static com.example.protobuf.DataInfo.Student parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static com.example.protobuf.DataInfo.Student parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static com.example.protobuf.DataInfo.Student parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static com.example.protobuf.DataInfo.Student parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static com.example.protobuf.DataInfo.Student parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static com.example.protobuf.DataInfo.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 com.example.protobuf.DataInfo.Student parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static com.example.protobuf.DataInfo.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 com.example.protobuf.DataInfo.Student parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static com.example.protobuf.DataInfo.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(com.example.protobuf.DataInfo.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 com.example.protobuf.Student}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:com.example.protobuf.Student)
com.example.protobuf.DataInfo.StudentOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_descriptor;
} @java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_fieldAccessorTable
.ensureFieldAccessorsInitialized(
com.example.protobuf.DataInfo.Student.class, com.example.protobuf.DataInfo.Student.Builder.class);
} // Construct using com.example.protobuf.DataInfo.Student.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
} private Builder(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3
.alwaysUseFieldBuilders) {
}
}
@java.lang.Override
public Builder clear() {
super.clear();
name_ = "";
bitField0_ = (bitField0_ & ~0x00000001);
age_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
address_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
return this;
} @java.lang.Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_descriptor;
} @java.lang.Override
public com.example.protobuf.DataInfo.Student getDefaultInstanceForType() {
return com.example.protobuf.DataInfo.Student.getDefaultInstance();
} @java.lang.Override
public com.example.protobuf.DataInfo.Student build() {
com.example.protobuf.DataInfo.Student result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
} @java.lang.Override
public com.example.protobuf.DataInfo.Student buildPartial() {
com.example.protobuf.DataInfo.Student result = new com.example.protobuf.DataInfo.Student(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) != 0)) {
to_bitField0_ |= 0x00000001;
}
result.name_ = name_;
if (((from_bitField0_ & 0x00000002) != 0)) {
result.age_ = age_;
to_bitField0_ |= 0x00000002;
}
if (((from_bitField0_ & 0x00000004) != 0)) {
to_bitField0_ |= 0x00000004;
}
result.address_ = address_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
} @java.lang.Override
public Builder clone() {
return super.clone();
}
@java.lang.Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
java.lang.Object value) {
return super.setField(field, value);
}
@java.lang.Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return super.clearField(field);
}
@java.lang.Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return super.clearOneof(oneof);
}
@java.lang.Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, java.lang.Object value) {
return super.setRepeatedField(field, index, value);
}
@java.lang.Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
java.lang.Object value) {
return super.addRepeatedField(field, value);
}
@java.lang.Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof com.example.protobuf.DataInfo.Student) {
return mergeFrom((com.example.protobuf.DataInfo.Student)other);
} else {
super.mergeFrom(other);
return this;
}
} public Builder mergeFrom(com.example.protobuf.DataInfo.Student other) {
if (other == com.example.protobuf.DataInfo.Student.getDefaultInstance()) return this;
if (other.hasName()) {
bitField0_ |= 0x00000001;
name_ = other.name_;
onChanged();
}
if (other.hasAge()) {
setAge(other.getAge());
}
if (other.hasAddress()) {
bitField0_ |= 0x00000004;
address_ = other.address_;
onChanged();
}
this.mergeUnknownFields(other.unknownFields);
onChanged();
return this;
} @java.lang.Override
public final boolean isInitialized() {
if (!hasName()) {
return false;
}
return true;
} @java.lang.Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
com.example.protobuf.DataInfo.Student parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (com.example.protobuf.DataInfo.Student) e.getUnfinishedMessage();
throw e.unwrapIOException();
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_; private java.lang.Object name_ = "";
/**
* <code>required string name = 1;</code>
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>required string name = 1;</code>
*/
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();
if (bs.isValidUtf8()) {
name_ = s;
}
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>required string name = 1;</code>
*/
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>required string name = 1;</code>
*/
public Builder setName(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
name_ = value;
onChanged();
return this;
}
/**
* <code>required string name = 1;</code>
*/
public Builder clearName() {
bitField0_ = (bitField0_ & ~0x00000001);
name_ = getDefaultInstance().getName();
onChanged();
return this;
}
/**
* <code>required string name = 1;</code>
*/
public Builder setNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
name_ = value;
onChanged();
return this;
} private int age_ ;
/**
* <code>optional int32 age = 2;</code>
*/
public boolean hasAge() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>optional int32 age = 2;</code>
*/
public int getAge() {
return age_;
}
/**
* <code>optional int32 age = 2;</code>
*/
public Builder setAge(int value) {
bitField0_ |= 0x00000002;
age_ = value;
onChanged();
return this;
}
/**
* <code>optional int32 age = 2;</code>
*/
public Builder clearAge() {
bitField0_ = (bitField0_ & ~0x00000002);
age_ = 0;
onChanged();
return this;
} private java.lang.Object address_ = "";
/**
* <code>optional string address = 3;</code>
*/
public boolean hasAddress() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>optional string address = 3;</code>
*/
public java.lang.String getAddress() {
java.lang.Object ref = address_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
address_ = s;
}
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>optional string address = 3;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes() {
java.lang.Object ref = address_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
address_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string address = 3;</code>
*/
public Builder setAddress(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
address_ = value;
onChanged();
return this;
}
/**
* <code>optional string address = 3;</code>
*/
public Builder clearAddress() {
bitField0_ = (bitField0_ & ~0x00000004);
address_ = getDefaultInstance().getAddress();
onChanged();
return this;
}
/**
* <code>optional string address = 3;</code>
*/
public Builder setAddressBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
address_ = value;
onChanged();
return this;
}
@java.lang.Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFields(unknownFields);
} @java.lang.Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
} // @@protoc_insertion_point(builder_scope:com.example.protobuf.Student)
} // @@protoc_insertion_point(class_scope:com.example.protobuf.Student)
private static final com.example.protobuf.DataInfo.Student DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new com.example.protobuf.DataInfo.Student();
} public static com.example.protobuf.DataInfo.Student getDefaultInstance() {
return DEFAULT_INSTANCE;
} @java.lang.Deprecated public 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 {
return new Student(input, extensionRegistry);
}
}; 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 com.example.protobuf.DataInfo.Student getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
} } private static final com.google.protobuf.Descriptors.Descriptor
internal_static_com_example_protobuf_Student_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_com_example_protobuf_Student_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\032src/protobuf/Student.proto\022\024com.exampl" +
"e.protobuf\"5\n\007Student\022\014\n\004name\030\001 \002(\t\022\013\n\003a" +
"ge\030\002 \001(\005\022\017\n\007address\030\003 \001(\tB\"\n\024com.example" +
".protobufB\010DataInfoH\001"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_com_example_protobuf_Student_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_com_example_protobuf_Student_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_com_example_protobuf_Student_descriptor,
new java.lang.String[] { "Name", "Age", "Address", });
} // @@protoc_insertion_point(outer_class_scope)
}

三、测试

public class ProtoBufTest {
public static void main(String[] args) throws Exception {
DataInfo.Student student = DataInfo.Student.newBuilder().
setName("张三").setAge(30).setAddress("ZheJiang").build();
//将对象转byte数组
byte[] student2ByteArray = student.toByteArray(); //将byte数组转对象
DataInfo.Student student2 = DataInfo.Student.parseFrom(student2ByteArray); System.out.println(student2.getName());
System.out.println(student2.getAddress());
System.out.println(student2.getAge());
}
}

  

输出结果:

protobuf使用的更多相关文章

  1. python通过protobuf实现rpc

    由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...

  2. Protobuf使用规范分享

    一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...

  3. java netty socket库和自定义C#socket库利用protobuf进行通信完整实例

    之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...

  4. 在Wcf中应用ProtoBuf替代默认的序列化器

    Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...

  5. protobuf的编译安装

    github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...

  6. 编译protobuf的jar文件

    1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...

  7. protobuf学习(2)-相关学习资料

    protobuf官方git地址 protobuf官方英文文档   (你懂的需要FQ) protobuf中文翻译文档 protobuf概述          (官方翻译 推荐阅读) protobuf入门 ...

  8. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  9. c# (ENUM)枚举组合类型的谷歌序列化Protobuf

    c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...

  10. dubbox 增加google-gprc/protobuf支持

    好久没写东西了,今年实在太忙,基本都在搞业务开发,晚上来补一篇,作为今年的收官博客.google-rpc 正式发布以来,受到了不少人的关注,这么知名的rpc框架,不集成到dubbox中有点说不过去. ...

随机推荐

  1. c语言学习目标

    运行c语言环境codeblocks. codeblocks的安装: 安装地址:http://www.codeblocks.org/downloads/26 codeblocks-16.01mingw- ...

  2. day 07 预科

    目录 异常处理 字符串内置方法 1.索引取值 2.切片 3.成员运算 4.for循环 5.len() 6.strip(): 默认去掉两端空格 7.lsteip()/rstrip(): 去左端/右端 空 ...

  3. day 08 预科

    目录 可变和不可变 不可变类型 可变类型 可变: 列表/字典 ---->值变id不变 不可变: 数字/字符串 ---->值变id也变 列表的内置方法 字典的内置方法 可变和不可变 可变和不 ...

  4. appium自动化webview时遇到的chromedriver问题

    安卓app里面的网页,基本上都是使用手机系统上的webview 去显示的. 安卓 webview 可以看成是 手机上的 chrome 浏览器精简版. appium desktop 里面内置了 用于 w ...

  5. springboot通过idea打jar包

    springboot打jar包 一.      检查pom文件 <packaging>jar</packaging> 二.      切换到maven窗口 三.      先c ...

  6. (二)分布式数据库tidb-事务

    tidb既然是分布式数据库,所以它的事务应该可其它数据库事务有着不同的区别.我们来了解下tidb的数据库事务. (一)事物 1.几种数据库的默认隔离级别: tidb是乐观锁 (二)事务语句 TiDB ...

  7. nodejs 删除空文件

    var fs = require("fs") var path = require("path") var listRealPath = path.resolv ...

  8. HTML&CSS基础-文档声明

    HTML&CSS基础-文档声明 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML的发展 1993年6月: HTML第一个版本 1995年11月: HTML2.0 ...

  9. python学习之多窗口切换

    多窗口切换: from selenium import webdriver d = webdriver.Firefox() d.window_handles #显示所有的窗口 d.current_wi ...

  10. (转)python自动化测试之异常及日志

    为了保持自动化测试用例的健壮性,异常的捕获及处理,日志的记录对掌握自动化测试执行情况尤为重要,这里便详细的介绍下在自动化测试中使用到的异常及日志,并介绍其详细的用法. 一.日志 打印日志是很多程序的重 ...