Protocol Buffer序列化对比Java序列化.

时间:2021-07-26 21:04:10

初识

Protocol Buff是谷歌推出的一种序列化协议. 而Java序列化协议也是一种协议.

两者的目的是, 将对象序列化成字节数组, 或者说是二进制数据, 那么他们之间有什么差异呢.

proto对象

要使用PB, 我们需要定义一个proto对象, 其支持的数据类型如下:

Protobuf定义了一套基本数据类型。几乎都可以映射到C++\Java等语言的基础数据类型.

protobuf 数据类型

描述

打包

C++语言映射

bool

布尔类型

1字节

bool

double

64位浮点数

N

double

float

32为浮点数

N

float

int32

32位整数、

N

int

uin32

无符号32位整数

N

unsigned int

int64

64位整数

N

__int64

uint64

64为无符号整

N

unsigned __int64

sint32

32位整数,处理负数效率更高

N

int32

sing64

64位整数 处理负数效率更高

N

__int64

fixed32

32位无符号整数

4

unsigned int32

fixed64

64位无符号整数

8

unsigned __int64

sfixed32

32位整数、能以更高的效率处理负数

4

unsigned int32

sfixed64

64为整数

8

unsigned __int64

string

只能处理 ASCII字符

N

std::string

bytes

用于处理多字节的语言字符、如中文

N

std::string

enum

可以包含一个用户自定义的枚举类型uint32

N(uint32)

enum

message

可以包含一个用户自定义的消息类型

N

object of class

proto对象结构类似于java的class, 在同一个module里, 定义了两个类, PBPlayer 和 PBResource, 各自拥有一些成员变量.

 option java_package = "com.proto";
option java_outer_classname = "PlayerModule"; message PBPlayer{
required int64 playerId = 1; required int32 age = 2; required string name = 3; repeated int32 skills = 4;
} message PBResource{
required int64 gold = 1; required int32 energy = 2;
}

然后用protoc.exe将proto对象转为java对象, 打开cmd, 运行如下命令:

 protoc ./proto/*.proto --java_out=./src

生成的java对象比较庞大, 我们不需要细究里面代码, 直接调用即可.

 // Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: proto/player.proto package com.proto; public final class PlayerModule {
private PlayerModule() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface PBPlayerOrBuilder
extends com.google.protobuf.MessageOrBuilder { // required int64 playerId = 1;
boolean hasPlayerId();
long getPlayerId(); // required int32 age = 2;
boolean hasAge();
int getAge(); // required string name = 3;
boolean hasName();
String getName(); // repeated int32 skills = 4;
java.util.List<java.lang.Integer> getSkillsList();
int getSkillsCount();
int getSkills(int index);
}
public static final class PBPlayer extends
com.google.protobuf.GeneratedMessage
implements PBPlayerOrBuilder {
// Use PBPlayer.newBuilder() to construct.
private PBPlayer(Builder builder) {
super(builder);
}
private PBPlayer(boolean noInit) {} private static final PBPlayer defaultInstance;
public static PBPlayer getDefaultInstance() {
return defaultInstance;
} public PBPlayer getDefaultInstanceForType() {
return defaultInstance;
} public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.proto.PlayerModule.internal_static_PBPlayer_descriptor;
} protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.proto.PlayerModule.internal_static_PBPlayer_fieldAccessorTable;
} private int bitField0_;
// required int64 playerId = 1;
public static final int PLAYERID_FIELD_NUMBER = 1;
private long playerId_;
public boolean hasPlayerId() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
public long getPlayerId() {
return playerId_;
} // required int32 age = 2;
public static final int AGE_FIELD_NUMBER = 2;
private int age_;
public boolean hasAge() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
public int getAge() {
return age_;
} // required string name = 3;
public static final int NAME_FIELD_NUMBER = 3;
private java.lang.Object name_;
public boolean hasName() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
public String getName() {
java.lang.Object ref = name_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (com.google.protobuf.Internal.isValidUtf8(bs)) {
name_ = s;
}
return s;
}
}
private com.google.protobuf.ByteString getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8((String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} // repeated int32 skills = 4;
public static final int SKILLS_FIELD_NUMBER = 4;
private java.util.List<java.lang.Integer> skills_;
public java.util.List<java.lang.Integer>
getSkillsList() {
return skills_;
}
public int getSkillsCount() {
return skills_.size();
}
public int getSkills(int index) {
return skills_.get(index);
} private void initFields() {
playerId_ = 0L;
age_ = 0;
name_ = "";
skills_ = java.util.Collections.emptyList();;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1; if (!hasPlayerId()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasAge()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasName()) {
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.writeInt64(1, playerId_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
output.writeInt32(2, age_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
output.writeBytes(3, getNameBytes());
}
for (int i = 0; i < skills_.size(); i++) {
output.writeInt32(4, skills_.get(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
.computeInt64Size(1, playerId_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, age_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
size += com.google.protobuf.CodedOutputStream
.computeBytesSize(3, getNameBytes());
}
{
int dataSize = 0;
for (int i = 0; i < skills_.size(); i++) {
dataSize += com.google.protobuf.CodedOutputStream
.computeInt32SizeNoTag(skills_.get(i));
}
size += dataSize;
size += 1 * getSkillsList().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 com.proto.PlayerModule.PBPlayer parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data).buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data, extensionRegistry)
.buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data).buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data, extensionRegistry)
.buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseFrom(java.io.InputStream input)
throws java.io.IOException {
return newBuilder().mergeFrom(input).buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return newBuilder().mergeFrom(input, extensionRegistry)
.buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
Builder builder = newBuilder();
if (builder.mergeDelimitedFrom(input)) {
return builder.buildParsed();
} else {
return null;
}
}
public static com.proto.PlayerModule.PBPlayer parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
Builder builder = newBuilder();
if (builder.mergeDelimitedFrom(input, extensionRegistry)) {
return builder.buildParsed();
} else {
return null;
}
}
public static com.proto.PlayerModule.PBPlayer parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return newBuilder().mergeFrom(input).buildParsed();
}
public static com.proto.PlayerModule.PBPlayer parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return newBuilder().mergeFrom(input, extensionRegistry)
.buildParsed();
} public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(com.proto.PlayerModule.PBPlayer 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;
}
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements com.proto.PlayerModule.PBPlayerOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.proto.PlayerModule.internal_static_PBPlayer_descriptor;
} protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.proto.PlayerModule.internal_static_PBPlayer_fieldAccessorTable;
} // Construct using com.proto.PlayerModule.PBPlayer.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
} private Builder(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();
playerId_ = 0L;
bitField0_ = (bitField0_ & ~0x00000001);
age_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
name_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
skills_ = java.util.Collections.emptyList();;
bitField0_ = (bitField0_ & ~0x00000008);
return this;
} public Builder clone() {
return create().mergeFrom(buildPartial());
} public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return com.proto.PlayerModule.PBPlayer.getDescriptor();
} public com.proto.PlayerModule.PBPlayer getDefaultInstanceForType() {
return com.proto.PlayerModule.PBPlayer.getDefaultInstance();
} public com.proto.PlayerModule.PBPlayer build() {
com.proto.PlayerModule.PBPlayer result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
} private com.proto.PlayerModule.PBPlayer buildParsed()
throws com.google.protobuf.InvalidProtocolBufferException {
com.proto.PlayerModule.PBPlayer result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(
result).asInvalidProtocolBufferException();
}
return result;
} public com.proto.PlayerModule.PBPlayer buildPartial() {
com.proto.PlayerModule.PBPlayer result = new com.proto.PlayerModule.PBPlayer(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.playerId_ = playerId_;
if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
to_bitField0_ |= 0x00000002;
}
result.age_ = age_;
if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
to_bitField0_ |= 0x00000004;
}
result.name_ = name_;
if (((bitField0_ & 0x00000008) == 0x00000008)) {
skills_ = java.util.Collections.unmodifiableList(skills_);
bitField0_ = (bitField0_ & ~0x00000008);
}
result.skills_ = skills_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
} public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof com.proto.PlayerModule.PBPlayer) {
return mergeFrom((com.proto.PlayerModule.PBPlayer)other);
} else {
super.mergeFrom(other);
return this;
}
} public Builder mergeFrom(com.proto.PlayerModule.PBPlayer other) {
if (other == com.proto.PlayerModule.PBPlayer.getDefaultInstance()) return this;
if (other.hasPlayerId()) {
setPlayerId(other.getPlayerId());
}
if (other.hasAge()) {
setAge(other.getAge());
}
if (other.hasName()) {
setName(other.getName());
}
if (!other.skills_.isEmpty()) {
if (skills_.isEmpty()) {
skills_ = other.skills_;
bitField0_ = (bitField0_ & ~0x00000008);
} else {
ensureSkillsIsMutable();
skills_.addAll(other.skills_);
}
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
} public final boolean isInitialized() {
if (!hasPlayerId()) { return false;
}
if (!hasAge()) { return false;
}
if (!hasName()) { return false;
}
return true;
} public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder(
this.getUnknownFields());
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
this.setUnknownFields(unknownFields.build());
onChanged();
return this;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
this.setUnknownFields(unknownFields.build());
onChanged();
return this;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
playerId_ = input.readInt64();
break;
}
case 16: {
bitField0_ |= 0x00000002;
age_ = input.readInt32();
break;
}
case 26: {
bitField0_ |= 0x00000004;
name_ = input.readBytes();
break;
}
case 32: {
ensureSkillsIsMutable();
skills_.add(input.readInt32());
break;
}
case 34: {
int length = input.readRawVarint32();
int limit = input.pushLimit(length);
while (input.getBytesUntilLimit() > 0) {
addSkills(input.readInt32());
}
input.popLimit(limit);
break;
}
}
}
} private int bitField0_; // required int64 playerId = 1;
private long playerId_ ;
public boolean hasPlayerId() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
public long getPlayerId() {
return playerId_;
}
public Builder setPlayerId(long value) {
bitField0_ |= 0x00000001;
playerId_ = value;
onChanged();
return this;
}
public Builder clearPlayerId() {
bitField0_ = (bitField0_ & ~0x00000001);
playerId_ = 0L;
onChanged();
return this;
} // required int32 age = 2;
private int age_ ;
public boolean hasAge() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
public int getAge() {
return age_;
}
public Builder setAge(int value) {
bitField0_ |= 0x00000002;
age_ = value;
onChanged();
return this;
}
public Builder clearAge() {
bitField0_ = (bitField0_ & ~0x00000002);
age_ = 0;
onChanged();
return this;
} // required string name = 3;
private java.lang.Object name_ = "";
public boolean hasName() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
public String getName() {
java.lang.Object ref = name_;
if (!(ref instanceof String)) {
String s = ((com.google.protobuf.ByteString) ref).toStringUtf8();
name_ = s;
return s;
} else {
return (String) ref;
}
}
public Builder setName(String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
name_ = value;
onChanged();
return this;
}
public Builder clearName() {
bitField0_ = (bitField0_ & ~0x00000004);
name_ = getDefaultInstance().getName();
onChanged();
return this;
}
void setName(com.google.protobuf.ByteString value) {
bitField0_ |= 0x00000004;
name_ = value;
onChanged();
} // repeated int32 skills = 4;
private java.util.List<java.lang.Integer> skills_ = java.util.Collections.emptyList();;
private void ensureSkillsIsMutable() {
if (!((bitField0_ & 0x00000008) == 0x00000008)) {
skills_ = new java.util.ArrayList<java.lang.Integer>(skills_);
bitField0_ |= 0x00000008;
}
}
public java.util.List<java.lang.Integer>
getSkillsList() {
return java.util.Collections.unmodifiableList(skills_);
}
public int getSkillsCount() {
return skills_.size();
}
public int getSkills(int index) {
return skills_.get(index);
}
public Builder setSkills(
int index, int value) {
ensureSkillsIsMutable();
skills_.set(index, value);
onChanged();
return this;
}
public Builder addSkills(int value) {
ensureSkillsIsMutable();
skills_.add(value);
onChanged();
return this;
}
public Builder addAllSkills(
java.lang.Iterable<? extends java.lang.Integer> values) {
ensureSkillsIsMutable();
super.addAll(values, skills_);
onChanged();
return this;
}
public Builder clearSkills() {
skills_ = java.util.Collections.emptyList();;
bitField0_ = (bitField0_ & ~0x00000008);
onChanged();
return this;
} // @@protoc_insertion_point(builder_scope:PBPlayer)
} static {
defaultInstance = new PBPlayer(true);
defaultInstance.initFields();
} // @@protoc_insertion_point(class_scope:PBPlayer)
} public interface PBResourceOrBuilder
extends com.google.protobuf.MessageOrBuilder { // required int64 gold = 1;
boolean hasGold();
long getGold(); // required int32 energy = 2;
boolean hasEnergy();
int getEnergy();
}
public static final class PBResource extends
com.google.protobuf.GeneratedMessage
implements PBResourceOrBuilder {
// Use PBResource.newBuilder() to construct.
private PBResource(Builder builder) {
super(builder);
}
private PBResource(boolean noInit) {} private static final PBResource defaultInstance;
public static PBResource getDefaultInstance() {
return defaultInstance;
} public PBResource getDefaultInstanceForType() {
return defaultInstance;
} public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.proto.PlayerModule.internal_static_PBResource_descriptor;
} protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.proto.PlayerModule.internal_static_PBResource_fieldAccessorTable;
} private int bitField0_;
// required int64 gold = 1;
public static final int GOLD_FIELD_NUMBER = 1;
private long gold_;
public boolean hasGold() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
public long getGold() {
return gold_;
} // required int32 energy = 2;
public static final int ENERGY_FIELD_NUMBER = 2;
private int energy_;
public boolean hasEnergy() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
public int getEnergy() {
return energy_;
} private void initFields() {
gold_ = 0L;
energy_ = 0;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1; if (!hasGold()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasEnergy()) {
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.writeInt64(1, gold_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
output.writeInt32(2, energy_);
}
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
.computeInt64Size(1, gold_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, energy_);
}
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 com.proto.PlayerModule.PBResource parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data).buildParsed();
}
public static com.proto.PlayerModule.PBResource parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data, extensionRegistry)
.buildParsed();
}
public static com.proto.PlayerModule.PBResource parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data).buildParsed();
}
public static com.proto.PlayerModule.PBResource parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return newBuilder().mergeFrom(data, extensionRegistry)
.buildParsed();
}
public static com.proto.PlayerModule.PBResource parseFrom(java.io.InputStream input)
throws java.io.IOException {
return newBuilder().mergeFrom(input).buildParsed();
}
public static com.proto.PlayerModule.PBResource parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return newBuilder().mergeFrom(input, extensionRegistry)
.buildParsed();
}
public static com.proto.PlayerModule.PBResource parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
Builder builder = newBuilder();
if (builder.mergeDelimitedFrom(input)) {
return builder.buildParsed();
} else {
return null;
}
}
public static com.proto.PlayerModule.PBResource parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
Builder builder = newBuilder();
if (builder.mergeDelimitedFrom(input, extensionRegistry)) {
return builder.buildParsed();
} else {
return null;
}
}
public static com.proto.PlayerModule.PBResource parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return newBuilder().mergeFrom(input).buildParsed();
}
public static com.proto.PlayerModule.PBResource parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return newBuilder().mergeFrom(input, extensionRegistry)
.buildParsed();
} public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(com.proto.PlayerModule.PBResource 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;
}
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements com.proto.PlayerModule.PBResourceOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.proto.PlayerModule.internal_static_PBResource_descriptor;
} protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.proto.PlayerModule.internal_static_PBResource_fieldAccessorTable;
} // Construct using com.proto.PlayerModule.PBResource.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
} private Builder(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();
gold_ = 0L;
bitField0_ = (bitField0_ & ~0x00000001);
energy_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
return this;
} public Builder clone() {
return create().mergeFrom(buildPartial());
} public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return com.proto.PlayerModule.PBResource.getDescriptor();
} public com.proto.PlayerModule.PBResource getDefaultInstanceForType() {
return com.proto.PlayerModule.PBResource.getDefaultInstance();
} public com.proto.PlayerModule.PBResource build() {
com.proto.PlayerModule.PBResource result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
} private com.proto.PlayerModule.PBResource buildParsed()
throws com.google.protobuf.InvalidProtocolBufferException {
com.proto.PlayerModule.PBResource result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(
result).asInvalidProtocolBufferException();
}
return result;
} public com.proto.PlayerModule.PBResource buildPartial() {
com.proto.PlayerModule.PBResource result = new com.proto.PlayerModule.PBResource(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.gold_ = gold_;
if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
to_bitField0_ |= 0x00000002;
}
result.energy_ = energy_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
} public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof com.proto.PlayerModule.PBResource) {
return mergeFrom((com.proto.PlayerModule.PBResource)other);
} else {
super.mergeFrom(other);
return this;
}
} public Builder mergeFrom(com.proto.PlayerModule.PBResource other) {
if (other == com.proto.PlayerModule.PBResource.getDefaultInstance()) return this;
if (other.hasGold()) {
setGold(other.getGold());
}
if (other.hasEnergy()) {
setEnergy(other.getEnergy());
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
} public final boolean isInitialized() {
if (!hasGold()) { return false;
}
if (!hasEnergy()) { return false;
}
return true;
} public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder(
this.getUnknownFields());
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
this.setUnknownFields(unknownFields.build());
onChanged();
return this;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
this.setUnknownFields(unknownFields.build());
onChanged();
return this;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
gold_ = input.readInt64();
break;
}
case 16: {
bitField0_ |= 0x00000002;
energy_ = input.readInt32();
break;
}
}
}
} private int bitField0_; // required int64 gold = 1;
private long gold_ ;
public boolean hasGold() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
public long getGold() {
return gold_;
}
public Builder setGold(long value) {
bitField0_ |= 0x00000001;
gold_ = value;
onChanged();
return this;
}
public Builder clearGold() {
bitField0_ = (bitField0_ & ~0x00000001);
gold_ = 0L;
onChanged();
return this;
} // required int32 energy = 2;
private int energy_ ;
public boolean hasEnergy() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
public int getEnergy() {
return energy_;
}
public Builder setEnergy(int value) {
bitField0_ |= 0x00000002;
energy_ = value;
onChanged();
return this;
}
public Builder clearEnergy() {
bitField0_ = (bitField0_ & ~0x00000002);
energy_ = 0;
onChanged();
return this;
} // @@protoc_insertion_point(builder_scope:PBResource)
} static {
defaultInstance = new PBResource(true);
defaultInstance.initFields();
} // @@protoc_insertion_point(class_scope:PBResource)
} private static com.google.protobuf.Descriptors.Descriptor
internal_static_PBPlayer_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_PBPlayer_fieldAccessorTable;
private static com.google.protobuf.Descriptors.Descriptor
internal_static_PBResource_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_PBResource_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\022proto/player.proto\"G\n\010PBPlayer\022\020\n\010play" +
"erId\030\001 \002(\003\022\013\n\003age\030\002 \002(\005\022\014\n\004name\030\003 \002(\t\022\016\n" +
"\006skills\030\004 \003(\005\"*\n\nPBResource\022\014\n\004gold\030\001 \002(" +
"\003\022\016\n\006energy\030\002 \002(\005B\031\n\tcom.protoB\014PlayerMo" +
"dule"
};
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_PBPlayer_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_PBPlayer_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_PBPlayer_descriptor,
new java.lang.String[] { "PlayerId", "Age", "Name", "Skills", },
com.proto.PlayerModule.PBPlayer.class,
com.proto.PlayerModule.PBPlayer.Builder.class);
internal_static_PBResource_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_PBResource_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_PBResource_descriptor,
new java.lang.String[] { "Gold", "Energy", },
com.proto.PlayerModule.PBResource.class,
com.proto.PlayerModule.PBResource.Builder.class);
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
} // @@protoc_insertion_point(outer_class_scope)
}

PB序列化

         //获取一个PBPlayer的构造器
Builder builder = PlayerModule.PBPlayer.newBuilder();
//设置数据
builder.setPlayerId(101).setAge(20).setName("neil").addSkills(1001);
//构造出对象
PBPlayer player = builder.build();
//序列化成字节数组
byte[] byteArray = player.toByteArray();
System.out.println(Arrays.toString(byteArray));
return byteArray;

运行结果, 一串非常短的byte数组, 远远比json, xml, java序列化要短.

[8, 101, 16, 20, 26, 4, 110, 101, 105, 108, 32, -23, 7]

PB反序列化

     public static void toPlayer(byte[] bs) throws Exception{
PBPlayer player = PlayerModule.PBPlayer.parseFrom(bs);
System.out.println("playerId:" + player.getPlayerId());
System.out.println("age:" + player.getAge());
System.out.println("name:" + player.getName());
System.out.println("skills:" + (Arrays.toString(player.getSkillsList().toArray())));
}

运行结果

playerId:101
age:20
name:neil
skills:[1001]

Java序列化

定义player pojo.

     private long playerId;
private int age;
private String name;
private List<Integer> skills = new ArrayList<>();
         Player player = new Player(101, 20, "neil");
player.getSkills().add(1001); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); //写入对象
objectOutputStream.writeObject(player); //获取 字节数组
byte[] byteArray = byteArrayOutputStream.toByteArray();
System.out.println(Arrays.toString(byteArray));
return byteArray;

运行结果, 非常长的一个byte数组

[-84, -19, 0, 5, 115, 114, 0, 15, 99, 111, 109, 46, 106, 97, 118, 97, 46, 80, 108, 97, 121, 101, 114, -73, 43, 28, 39, -119, -86, -125, -3, 2, 0, 4, 73, 0, 3, 97, 103, 101, 74, 0, 8, 112, 108, 97, 121, 101, 114, 73, 100, 76, 0, 4, 110, 97, 109, 101, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 76, 0, 6, 115, 107, 105, 108, 108, 115, 116, 0, 16, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 76, 105, 115, 116, 59, 120, 112, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 101, 116, 0, 4, 110, 101, 105, 108, 115, 114, 0, 19, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114, 97, 121, 76, 105, 115, 116, 120, -127, -46, 29, -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105, 122, 101, 120, 112, 0, 0, 0, 1, 119, 4, 0, 0, 0, 1, 115, 114, 0, 17, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 73, 110, 116, 101, 103, 101, 114, 18, -30, -96, -92, -9, -127, -121, 56, 2, 0, 1, 73, 0, 5, 118, 97, 108, 117, 101, 120, 114, 0, 16, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 78, 117, 109, 98, 101, 114, -122, -84, -107, 29, 11, -108, -32, -117, 2, 0, 0, 120, 112, 0, 0, 3, -23, 120]

Java反序列化

         ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(bs));
Player player = (Player)inputStream.readObject(); //打印
System.out.println("playerId:" + player.getPlayerId());
System.out.println("age:" + player.getAge());
System.out.println("name:" + player.getName());
System.out.println("skills:" + (Arrays.toString(player.getSkills().toArray())));

运行结果

playerId:101
age:20
name:neil
skills:[1001]

对比

经过上面运行结果的对比可以看出来, PB是一个非常高效的序列化协议.

相对于Java这种数据类型固定长度的序列化(int 4字节, long 8字节), PB提供了可伸缩性的数据类型(int 1-5字节).

当数据比较小的时候int只占用1个字节, 而大部分场景下, 数据都是很小的, 不然jdk本身也不会缓存 -127~128了.