google protobuf 简单实例
1.定义proto文件:
User.proto
package netty;
option java_package="myprotobuf";
option java_outer_classname="UserProto"; message User{
required int32 ID=1;
required string userName=2;
required string Password=3;
repeated string address=4;
}
2.用 protoc.exe 生成User.proto的协议文件:
D:\JAVA\protoc-2.5.0-win32>protoc.exe --java_out=. User.proto
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: User.proto package myprotobuf; public final class UserProto {
private UserProto() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface UserOrBuilder
extends com.google.protobuf.MessageOrBuilder { // required int32 ID = 1;
/**
* <code>required int32 ID = 1;</code>
*/
boolean hasID();
/**
* <code>required int32 ID = 1;</code>
*/
int getID(); // required string userName = 2;
/**
* <code>required string userName = 2;</code>
*/
boolean hasUserName();
/**
* <code>required string userName = 2;</code>
*/
java.lang.String getUserName();
/**
* <code>required string userName = 2;</code>
*/
com.google.protobuf.ByteString
getUserNameBytes(); // required string Password = 3;
/**
* <code>required string Password = 3;</code>
*/
boolean hasPassword();
/**
* <code>required string Password = 3;</code>
*/
java.lang.String getPassword();
/**
* <code>required string Password = 3;</code>
*/
com.google.protobuf.ByteString
getPasswordBytes(); // repeated string address = 4;
/**
* <code>repeated string address = 4;</code>
*/
java.util.List<java.lang.String>
getAddressList();
/**
* <code>repeated string address = 4;</code>
*/
int getAddressCount();
/**
* <code>repeated string address = 4;</code>
*/
java.lang.String getAddress(int index);
/**
* <code>repeated string address = 4;</code>
*/
com.google.protobuf.ByteString
getAddressBytes(int index);
}
/**
* Protobuf type {@code netty.User}
*/
public static final class User extends
com.google.protobuf.GeneratedMessage
implements UserOrBuilder {
// Use User.newBuilder() to construct.
private User(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private User(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } private static final User defaultInstance;
public static User getDefaultInstance() {
return defaultInstance;
} public User getDefaultInstanceForType() {
return defaultInstance;
} private final com.google.protobuf.UnknownFieldSet unknownFields;
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private User(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
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;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
iD_ = input.readInt32();
break;
}
case 18: {
bitField0_ |= 0x00000002;
userName_ = input.readBytes();
break;
}
case 26: {
bitField0_ |= 0x00000004;
password_ = input.readBytes();
break;
}
case 34: {
if (!((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
address_ = new com.google.protobuf.LazyStringArrayList();
mutable_bitField0_ |= 0x00000008;
}
address_.add(input.readBytes());
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
if (((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
address_ = new com.google.protobuf.UnmodifiableLazyStringList(address_);
}
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return myprotobuf.UserProto.internal_static_netty_User_descriptor;
} protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return myprotobuf.UserProto.internal_static_netty_User_fieldAccessorTable
.ensureFieldAccessorsInitialized(
myprotobuf.UserProto.User.class, myprotobuf.UserProto.User.Builder.class);
} public static com.google.protobuf.Parser<User> PARSER =
new com.google.protobuf.AbstractParser<User>() {
public User parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new User(input, extensionRegistry);
}
}; @java.lang.Override
public com.google.protobuf.Parser<User> getParserForType() {
return PARSER;
} private int bitField0_;
// required int32 ID = 1;
public static final int ID_FIELD_NUMBER = 1;
private int iD_;
/**
* <code>required int32 ID = 1;</code>
*/
public boolean hasID() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 ID = 1;</code>
*/
public int getID() {
return iD_;
} // required string userName = 2;
public static final int USERNAME_FIELD_NUMBER = 2;
private java.lang.Object userName_;
/**
* <code>required string userName = 2;</code>
*/
public boolean hasUserName() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>required string userName = 2;</code>
*/
public java.lang.String getUserName() {
java.lang.Object ref = userName_;
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()) {
userName_ = s;
}
return s;
}
}
/**
* <code>required string userName = 2;</code>
*/
public com.google.protobuf.ByteString
getUserNameBytes() {
java.lang.Object ref = userName_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
userName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} // required string Password = 3;
public static final int PASSWORD_FIELD_NUMBER = 3;
private java.lang.Object password_;
/**
* <code>required string Password = 3;</code>
*/
public boolean hasPassword() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>required string Password = 3;</code>
*/
public java.lang.String getPassword() {
java.lang.Object ref = password_;
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()) {
password_ = s;
}
return s;
}
}
/**
* <code>required string Password = 3;</code>
*/
public com.google.protobuf.ByteString
getPasswordBytes() {
java.lang.Object ref = password_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
password_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} // repeated string address = 4;
public static final int ADDRESS_FIELD_NUMBER = 4;
private com.google.protobuf.LazyStringList address_;
/**
* <code>repeated string address = 4;</code>
*/
public java.util.List<java.lang.String>
getAddressList() {
return address_;
}
/**
* <code>repeated string address = 4;</code>
*/
public int getAddressCount() {
return address_.size();
}
/**
* <code>repeated string address = 4;</code>
*/
public java.lang.String getAddress(int index) {
return address_.get(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes(int index) {
return address_.getByteString(index);
} private void initFields() {
iD_ = 0;
userName_ = "";
password_ = "";
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1; if (!hasID()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasUserName()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasPassword()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
} public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeInt32(1, iD_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
output.writeBytes(2, getUserNameBytes());
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
output.writeBytes(3, getPasswordBytes());
}
for (int i = 0; i < address_.size(); i++) {
output.writeBytes(4, address_.getByteString(i));
}
getUnknownFields().writeTo(output);
} private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size; size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, iD_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
size += com.google.protobuf.CodedOutputStream
.computeBytesSize(2, getUserNameBytes());
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
size += com.google.protobuf.CodedOutputStream
.computeBytesSize(3, getPasswordBytes());
}
{
int dataSize = 0;
for (int i = 0; i < address_.size(); i++) {
dataSize += com.google.protobuf.CodedOutputStream
.computeBytesSizeNoTag(address_.getByteString(i));
}
size += dataSize;
size += 1 * getAddressList().size();
}
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
} private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
} public static myprotobuf.UserProto.User parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static myprotobuf.UserProto.User parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static myprotobuf.UserProto.User parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static myprotobuf.UserProto.User parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static myprotobuf.UserProto.User parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static myprotobuf.UserProto.User parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static myprotobuf.UserProto.User parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static myprotobuf.UserProto.User parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static myprotobuf.UserProto.User parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static myprotobuf.UserProto.User parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
} public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(myprotobuf.UserProto.User prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); } @java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code netty.User}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements myprotobuf.UserProto.UserOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return myprotobuf.UserProto.internal_static_netty_User_descriptor;
} protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return myprotobuf.UserProto.internal_static_netty_User_fieldAccessorTable
.ensureFieldAccessorsInitialized(
myprotobuf.UserProto.User.class, myprotobuf.UserProto.User.Builder.class);
} // Construct using myprotobuf.UserProto.User.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
} private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
private static Builder create() {
return new Builder();
} public Builder clear() {
super.clear();
iD_ = 0;
bitField0_ = (bitField0_ & ~0x00000001);
userName_ = "";
bitField0_ = (bitField0_ & ~0x00000002);
password_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000008);
return this;
} public Builder clone() {
return create().mergeFrom(buildPartial());
} public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return myprotobuf.UserProto.internal_static_netty_User_descriptor;
} public myprotobuf.UserProto.User getDefaultInstanceForType() {
return myprotobuf.UserProto.User.getDefaultInstance();
} public myprotobuf.UserProto.User build() {
myprotobuf.UserProto.User result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
} public myprotobuf.UserProto.User buildPartial() {
myprotobuf.UserProto.User result = new myprotobuf.UserProto.User(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.iD_ = iD_;
if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
to_bitField0_ |= 0x00000002;
}
result.userName_ = userName_;
if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
to_bitField0_ |= 0x00000004;
}
result.password_ = password_;
if (((bitField0_ & 0x00000008) == 0x00000008)) {
address_ = new com.google.protobuf.UnmodifiableLazyStringList(
address_);
bitField0_ = (bitField0_ & ~0x00000008);
}
result.address_ = address_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
} public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof myprotobuf.UserProto.User) {
return mergeFrom((myprotobuf.UserProto.User)other);
} else {
super.mergeFrom(other);
return this;
}
} public Builder mergeFrom(myprotobuf.UserProto.User other) {
if (other == myprotobuf.UserProto.User.getDefaultInstance()) return this;
if (other.hasID()) {
setID(other.getID());
}
if (other.hasUserName()) {
bitField0_ |= 0x00000002;
userName_ = other.userName_;
onChanged();
}
if (other.hasPassword()) {
bitField0_ |= 0x00000004;
password_ = other.password_;
onChanged();
}
if (!other.address_.isEmpty()) {
if (address_.isEmpty()) {
address_ = other.address_;
bitField0_ = (bitField0_ & ~0x00000008);
} else {
ensureAddressIsMutable();
address_.addAll(other.address_);
}
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
} public final boolean isInitialized() {
if (!hasID()) { return false;
}
if (!hasUserName()) { return false;
}
if (!hasPassword()) { return false;
}
return true;
} public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
myprotobuf.UserProto.User parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (myprotobuf.UserProto.User) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_; // required int32 ID = 1;
private int iD_ ;
/**
* <code>required int32 ID = 1;</code>
*/
public boolean hasID() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 ID = 1;</code>
*/
public int getID() {
return iD_;
}
/**
* <code>required int32 ID = 1;</code>
*/
public Builder setID(int value) {
bitField0_ |= 0x00000001;
iD_ = value;
onChanged();
return this;
}
/**
* <code>required int32 ID = 1;</code>
*/
public Builder clearID() {
bitField0_ = (bitField0_ & ~0x00000001);
iD_ = 0;
onChanged();
return this;
} // required string userName = 2;
private java.lang.Object userName_ = "";
/**
* <code>required string userName = 2;</code>
*/
public boolean hasUserName() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>required string userName = 2;</code>
*/
public java.lang.String getUserName() {
java.lang.Object ref = userName_;
if (!(ref instanceof java.lang.String)) {
java.lang.String s = ((com.google.protobuf.ByteString) ref)
.toStringUtf8();
userName_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>required string userName = 2;</code>
*/
public com.google.protobuf.ByteString
getUserNameBytes() {
java.lang.Object ref = userName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
userName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string userName = 2;</code>
*/
public Builder setUserName(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000002;
userName_ = value;
onChanged();
return this;
}
/**
* <code>required string userName = 2;</code>
*/
public Builder clearUserName() {
bitField0_ = (bitField0_ & ~0x00000002);
userName_ = getDefaultInstance().getUserName();
onChanged();
return this;
}
/**
* <code>required string userName = 2;</code>
*/
public Builder setUserNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000002;
userName_ = value;
onChanged();
return this;
} // required string Password = 3;
private java.lang.Object password_ = "";
/**
* <code>required string Password = 3;</code>
*/
public boolean hasPassword() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>required string Password = 3;</code>
*/
public java.lang.String getPassword() {
java.lang.Object ref = password_;
if (!(ref instanceof java.lang.String)) {
java.lang.String s = ((com.google.protobuf.ByteString) ref)
.toStringUtf8();
password_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>required string Password = 3;</code>
*/
public com.google.protobuf.ByteString
getPasswordBytes() {
java.lang.Object ref = password_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
password_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string Password = 3;</code>
*/
public Builder setPassword(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
password_ = value;
onChanged();
return this;
}
/**
* <code>required string Password = 3;</code>
*/
public Builder clearPassword() {
bitField0_ = (bitField0_ & ~0x00000004);
password_ = getDefaultInstance().getPassword();
onChanged();
return this;
}
/**
* <code>required string Password = 3;</code>
*/
public Builder setPasswordBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
password_ = value;
onChanged();
return this;
} // repeated string address = 4;
private com.google.protobuf.LazyStringList address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
private void ensureAddressIsMutable() {
if (!((bitField0_ & 0x00000008) == 0x00000008)) {
address_ = new com.google.protobuf.LazyStringArrayList(address_);
bitField0_ |= 0x00000008;
}
}
/**
* <code>repeated string address = 4;</code>
*/
public java.util.List<java.lang.String>
getAddressList() {
return java.util.Collections.unmodifiableList(address_);
}
/**
* <code>repeated string address = 4;</code>
*/
public int getAddressCount() {
return address_.size();
}
/**
* <code>repeated string address = 4;</code>
*/
public java.lang.String getAddress(int index) {
return address_.get(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes(int index) {
return address_.getByteString(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder setAddress(
int index, java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.set(index, value);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAddress(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.add(value);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAllAddress(
java.lang.Iterable<java.lang.String> values) {
ensureAddressIsMutable();
super.addAll(values, address_);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder clearAddress() {
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000008);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAddressBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.add(value);
onChanged();
return this;
} // @@protoc_insertion_point(builder_scope:netty.User)
} static {
defaultInstance = new User(true);
defaultInstance.initFields();
} // @@protoc_insertion_point(class_scope:netty.User)
} private static com.google.protobuf.Descriptors.Descriptor
internal_static_netty_User_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_netty_User_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\nUser.proto\022\005netty\"G\n\004User\022\n\n\002ID\030\001 \002(\005\022" +
"\020\n\010userName\030\002 \002(\t\022\020\n\010Password\030\003 \002(\t\022\017\n\007a" +
"ddress\030\004 \003(\tB\027\n\nmyprotobufB\tUserProto"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
internal_static_netty_User_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_netty_User_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_netty_User_descriptor,
new java.lang.String[] { "ID", "UserName", "Password", "Address", });
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
} // @@protoc_insertion_point(outer_class_scope)
}
3.测试类:
package myprotobuf; import java.util.ArrayList;
import java.util.List;
import com.google.protobuf.InvalidProtocolBufferException; public class Test { private static byte[] encode(UserProto.User user) {
return user.toByteArray();
} private static UserProto.User decode(byte[] body)
throws InvalidProtocolBufferException {
return UserProto.User.parseFrom(body);
} public static void main(String[] args)
throws InvalidProtocolBufferException { UserProto.User.Builder builder = UserProto.User.newBuilder();
builder.setID(1);
builder.setUserName("Lilinfeng");
builder.setPassword("Netty Book");
List<String> address = new ArrayList<String>();
address.add("NanJing YuHuaTai");
address.add("BeiJing LiuLiChang");
address.add("ShenZhen HongShuLin");
builder.addAllAddress(address);
UserProto.User user = builder.build(); System.out.println("Before encode : " + user.toString());
UserProto.User user2 = decode(encode(user));
System.out.println("After decode : " + user2.toString());
System.out.println("Assert equal : --> " + user2.equals(user)); } }
4.测试结果,说明编码和解密后结果一致:
Before encode : ID: 1
userName: "Lilinfeng"
Password: "Netty Book"
address: "NanJing YuHuaTai"
address: "BeiJing LiuLiChang"
address: "ShenZhen HongShuLin" After decode : ID: 1
userName: "Lilinfeng"
Password: "Netty Book"
address: "NanJing YuHuaTai"
address: "BeiJing LiuLiChang"
address: "ShenZhen HongShuLin" Assert equal : --> true
google protobuf 简单实例的更多相关文章
- google的protobuf简单介绍
google的protobuf是一种轻便高效的结构化数据存储格式,在通信协议和数据存储等领域中使用比较多.protobuf对于结构中的每个成员,会提供set系列函数和get系列函数. 但是,对于使用来 ...
- 《Dotnet9》系列-Google ProtoBuf在C#中的简单应用
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- GOOGLE PROTOBUF开发者指南
原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1 概览 1.1 什么是pro ...
- (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-2
8.1.2 Protobuf编解码开发 Protobuf的类库使用比较简单,下面我们就通过对SubscrjbeReqProto进行编解码来介绍Protobuf的使用. 8-1 Protob ...
- 在Android Studio配置google protobuf
1.在project的build.gradle中配置 buildscript { repositories { jcenter() mavenCentral() } dependencies { cl ...
- google protobuf初体验
最近在读别人代码的时候发现一个的东西,名字叫protobuf, 感觉挺好用的,写在这里,留个记录.那么什么是protobuf 呢?假如您在网上搜索,应该会得到类似这样的文字介绍: Google Pro ...
- Google protobuf proto文件编写规则
转载自: http://blog.csdn.net/yi_ya/article/details/40404231 1. 简单介绍 protobuf文件:就是定义你要的消息(类似java中的类)和消息中 ...
- Google protobuf
个人理解: 定义.proto文件就是指明消息里包含的成员和类型,protoc会compile成相应的java文件包含interface和implementation class,然后在构建messag ...
- google protobuf使用
下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...
随机推荐
- 浅谈 Active Learning
1. Active Query Driven by Uncertainty and Diversity for Incremental Multi-Label Learning The key tas ...
- Nginx-server_name匹配规则
server_name可为IP/domain/localhost/null等任何字符串(字符串server_name也可以用来匹配),注意各个 server 块的顺序. 1.如果只有一个server, ...
- ID3算法 决策树的生成(1)
# coding:utf-8 import matplotlib.pyplot as plt import numpy as np import pylab def createDataSet(): ...
- ctf之加密
from:http://drops.wooyun.org/tips/10002 0x01 Base64 Base64:ZXZhbCgkX1BPU1RbcDRuOV96MV96aDNuOV9qMXVfU ...
- C#4.0新特性:可选参数,命名参数,Dynamic
1.可选参数 可以为方法的参数设置一个默认值,如下: class Program { static void Main(string[] args) { Show(); Show("cary ...
- 不透明度(兼容IE8,chrome,firefox)
background-color: rgba(0, 0, 0, 0.2); background-color: black; opacity: 0.2; filter: Alpha(opacity=2 ...
- 用javacsv API 来操作csv文件
javacsv是国外开发的一个比较好的操作csv文件的API,这里简单讲一下用法. 先下载javacsv2.0.zip的文件,解压后,把javacsv.jar 添加到项目中. 本站下载地址: htt ...
- IOS开发-手势简单使用及手势不响应处理办法
1.点击 2.长按 3.拖拽 4.轻扫.捏合.旋转 5.使用手势需要注意的地方 1.注意处理轻扫和拖拽的冲突 //那个时间短的话 就让那个先执行 //处理 拖拽和轻扫 两个手势的冲突 //需要轻扫手势 ...
- js禁止从浏览器缓存读取消息
$.ajaxSetup ({ cache: false //设置成false将不会从浏览器缓存读取信息 });
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744943 记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其 ...