protobuf 环境配置
1、安装编译器
下载地址
直接解压缩。
2、配置环境变量
环境变量Path
中增加安装目录的路径
3、检查是否配置成功
protoc
Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
-IPATH, --proto_path=PATH Specify the directory in which to search for
imports. May be specified multiple times;
directories will be searched in order. If not
given, the current working directory is used.
If not found in any of the these directories,
the --descriptor_set_in descriptors will be
checked for required proto file.
--version #版本
-h, --help #帮助
--encode=MESSAGE_TYPE Read a text-format message of the given type
from standard input and write it in binary
to standard output. The message type must
be defined in PROTO_FILES or their imports.
--deterministic_output When using --encode, ensure map fields are
deterministically ordered. Note that this order
is not canonical, and changes across builds or
releases of protoc.
--decode=MESSAGE_TYPE Read a binary message of the given type from
standard input and write it in text format
to standard output. The message type must
be defined in PROTO_FILES or their imports.
--decode_raw Read an arbitrary protocol message from
standard input and write the raw tag/value
pairs in text format to standard output. No
PROTO_FILES should be given when using this
flag.
--descriptor_set_in=FILES Specifies a delimited list of FILES
each containing a FileDescriptorSet (a
protocol buffer defined in descriptor.proto).
The FileDescriptor for each of the PROTO_FILES
provided will be loaded from these
FileDescriptorSets. If a FileDescriptor
appears multiple times, the first occurrence
will be used.
-oFILE, Writes a FileDescriptorSet (a protocol buffer,
--descriptor_set_out=FILE defined in descriptor.proto) containing all of
the input files to FILE.
--include_imports When using --descriptor_set_out, also include
all dependencies of the input files in the
set, so that the set is self-contained.
--include_source_info When using --descriptor_set_out, do not strip
SourceCodeInfo from the FileDescriptorProto.
This results in vastly larger descriptors that
include information about the original
location of each decl in the source file as
well as surrounding comments.
--retain_options When using --descriptor_set_out, do not strip
any options from the FileDescriptorProto.
This results in potentially larger descriptors
that include information about options that were
only meant to be useful during compilation.
--dependency_out=FILE Write a dependency output file in the format
expected by make. This writes the transitive
set of input file paths to FILE
--error_format=FORMAT Set the format in which to print errors.
FORMAT may be 'gcc' (the default) or 'msvs'
(Microsoft Visual Studio format).
--fatal_warnings Make warnings be fatal (similar to -Werr in
gcc). This flag will make protoc return
with a non-zero exit code if any warnings
are generated.
--print_free_field_numbers Print the free field numbers of the messages
defined in the given proto files. Extension ranges
are counted as occupied fields numbers.
--enable_codegen_trace Enables tracing which parts of protoc are
responsible for what codegen output. Not supported
by all backends or on all platforms.
--plugin=EXECUTABLE Specifies a plugin executable to use.
Normally, protoc searches the PATH for
plugins, but you may specify additional
executables not in the path using this flag.
Additionally, EXECUTABLE may be of the form
NAME=PATH, in which case the given plugin name
is mapped to the given executable even if
the executable's own name differs.
--cpp_out=OUT_DIR Generate C++ header and source.
--csharp_out=OUT_DIR Generate C# source file.
--java_out=OUT_DIR Generate Java source file.
--kotlin_out=OUT_DIR Generate Kotlin file.
--objc_out=OUT_DIR Generate Objective-C header and source.
--php_out=OUT_DIR Generate PHP source file.
--pyi_out=OUT_DIR Generate python pyi stub.
--python_out=OUT_DIR Generate Python source file.
--ruby_out=OUT_DIR Generate Ruby source file.
--rust_out=OUT_DIR Generate Rust sources.
@<filename> Read options and filenames from file. If a
relative file path is specified, the file
will be searched in the working directory.
The --proto_path option will not affect how
this argument file is searched. Content of
the file will be expanded in the position of
@<filename> as in the argument list. Note
that shell expansion is not applied to the
content of the file (i.e., you cannot use
quotes, wildcards, escapes, commands, etc.).
Each line corresponds to a single argument,
even if it contains spaces.
E:\setups\protoc-25.0-win64\bin\protoc.exe --java_out=E:\projects_study\research\base\target Data2.proto
idea 中使用 protobuf
1、idea 安装 protobuf 相关插件
安装插件:
protobuf generator
:根据.proto
文件来生成proto
对象pojo to proto
:类文件-右键-PojoProto, 将简单Java类型转成proto message拷贝至剪贴板
2、配置全局 protobuf
人工生成Java文件
1、引入包
<!-- protobuf 支持 Java 核心包-->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.11</version>
</dependency>
<!-- proto 与 Json 互转会用到-->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.15.3</version>
</dependency>
2、编写.proto文件
//使用 proto3 语法 ,未指定则使用proto2
syntax = "proto3";
//生成 proto 文件所在包路径
package demon.research.proto;
//生成的 proto 文件所在包路径
option java_package = "demon.research.proto";
//生成的 proto 文件名
option java_outer_classname="DataGen";
//message关键字 定义一个消息体。
message DataOriginal{
int32 id = 1;
string code = 2;
string name = 3;
}
3、生成Java 文件
直接在文件上点击右键,quick gen protobuf here
,则在此目录根据包生成一个java文件,名字为Data.java
.
quick gen protobuf rules
则根据配置的规则,把java文件生成在指定的目录。
然后把生成的Java文件,copy到java类目录下。
4、序列化和反序列化
//初始化数据
DataGen.DataOriginal.Builder builder = DataGen.DataOriginal.newBuilder();
builder.setId(1).setCode("001").setName("张三").build();
//序列化
DataGen.DataOriginal build = builder.build();
//转换成字节数组
byte[] s = build.toByteArray();
System.out.println("protobuf数据bytes[]:" + Arrays.toString(s));
System.out.println("protobuf序列化大小: " + s.length);
DataGen.DataOriginal data = null;
String jsonObject = null;
try {
//反序列化
data = DataGen.DataOriginal.parseFrom(s);
//转 json
jsonObject = JsonFormat.printer().print(data);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Json格式化结果:\n" + jsonObject);
System.out.println("Json格式化数据大小: " + jsonObject.getBytes().length);
输出结果:
protobuf数据bytes[]:[8, 1, 18, 3, 48, 48, 49, 26, 6, -27, -68, -96, -28, -72, -119]
protobuf序列化大小: 15
Json格式化结果:
{
"id": 1,
"code": "001",
"name": "张三"
}
Json格式化数据大小: 50
传输的数据,确实比json格式的要小。
maven自动生成
引入插件
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.11:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:3.21.11:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
默认.proto
文件的扫描目录为: ${basedir}/src/main/proto
默认生成的文件路径为:${project.build.directory}/generated-sources/protobuf/java
.
需要人工把代码放到指定目录。如果需要自动生成到指定目录,则修改指定目录。
注意:默认会把指定的目录数据都给删除掉。,可以设置
clearOutputDirectory
为false。
附录
参考
下载地址
生成代码
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: DataOriginal.proto
// Protobuf Java Version: 3.25.0
// java_package 设置的包名
package demon.research.proto;
//java_outer_classname 指定的类名
public final class DataGen {
//避免修改
private DataGen() {}
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 DataOriginalOrBuilder extends
// @@protoc_insertion_point(interface_extends:demon.research.proto.DataOriginal)
com.google.protobuf.MessageOrBuilder {
/**
* <code>int32 id = 1;</code>
* @return The id.
*/
int getId();
//******************* 会为字符串类型 生成序列化 方法。
/**
* <code>string code = 2;</code>
* @return The code.
*/
String getCode();
/**
* <code>string code = 2;</code>
* @return The bytes for code.
*/
com.google.protobuf.ByteString
getCodeBytes();
/**
* <code>string name = 3;</code>
* @return The name.
*/
String getName();
/**
* <code>string name = 3;</code>
* @return The bytes for name.
*/
com.google.protobuf.ByteString
getNameBytes();
}
/**
* <pre>
*message关键字 定义一个消息体。
* </pre>
*
* Protobuf type {@code demon.research.proto.DataOriginal}
*/
public static final class DataOriginal extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:demon.research.proto.DataOriginal)
DataOriginalOrBuilder {
private static final long serialVersionUID = 0L;
// Use DataOriginal.newBuilder() to construct.
private DataOriginal(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private DataOriginal() {
code_ = "";
name_ = "";
}
@Override
@SuppressWarnings({"unused"})
protected Object newInstance(
UnusedPrivateParameter unused) {
return new DataOriginal();
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return DataGen.internal_static_demon_research_proto_DataOriginal_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return DataGen.internal_static_demon_research_proto_DataOriginal_fieldAccessorTable
.ensureFieldAccessorsInitialized(
DataOriginal.class, Builder.class);
}
public static final int ID_FIELD_NUMBER = 1;
private int id_ = 0;
/**
* <code>int32 id = 1;</code>
* @return The id.
*/
@Override
public int getId() {
return id_;
}
public static final int CODE_FIELD_NUMBER = 2;
@SuppressWarnings("serial")
private volatile Object code_ = "";
/**
* <code>string code = 2;</code>
* @return The code.
*/
@Override
public String getCode() {
Object ref = code_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
code_ = s;
return s;
}
}
/**
* <code>string code = 2;</code>
* @return The bytes for code.
*/
@Override
public com.google.protobuf.ByteString
getCodeBytes() {
Object ref = code_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
code_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int NAME_FIELD_NUMBER = 3;
@SuppressWarnings("serial")
private volatile Object name_ = "";
/**
* <code>string name = 3;</code>
* @return The name.
*/
@Override
public String getName() {
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();
name_ = s;
return s;
}
}
/**
* <code>string name = 3;</code>
* @return The bytes for name.
*/
@Override
public com.google.protobuf.ByteString
getNameBytes() {
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;
}
}
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 (id_ != 0) {
output.writeInt32(1, id_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(code_)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 2, code_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, name_);
}
getUnknownFields().writeTo(output);
}
@Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (id_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, id_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(code_)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, code_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, name_);
}
size += getUnknownFields().getSerializedSize();
memoizedSize = size;
return size;
}
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DataOriginal)) {
return super.equals(obj);
}
DataOriginal other = (DataOriginal) obj;
if (getId()
!= other.getId()) return false;
if (!getCode()
.equals(other.getCode())) return false;
if (!getName()
.equals(other.getName())) return false;
if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true;
}
@Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
hash = (37 * hash) + ID_FIELD_NUMBER;
hash = (53 * hash) + getId();
hash = (37 * hash) + CODE_FIELD_NUMBER;
hash = (53 * hash) + getCode().hashCode();
hash = (37 * hash) + NAME_FIELD_NUMBER;
hash = (53 * hash) + getName().hashCode();
hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash;
return hash;
}
public static DataOriginal parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static DataOriginal parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static DataOriginal parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static DataOriginal parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static DataOriginal parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static DataOriginal parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static DataOriginal parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static DataOriginal 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 DataOriginal parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static DataOriginal 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 DataOriginal parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static DataOriginal parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
}
@Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(DataOriginal prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
@Override
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
}
@Override
protected Builder newBuilderForType(
BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* <pre>
*message关键字 定义一个消息体。
* </pre>
*
* Protobuf type {@code demon.research.proto.DataOriginal}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:demon.research.proto.DataOriginal)
DataOriginalOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return DataGen.internal_static_demon_research_proto_DataOriginal_descriptor;
}
@Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return DataGen.internal_static_demon_research_proto_DataOriginal_fieldAccessorTable
.ensureFieldAccessorsInitialized(
DataOriginal.class, Builder.class);
}
// Construct using demon.research.proto.DataGen.DataOriginal.newBuilder()
private Builder() {
}
private Builder(
BuilderParent parent) {
super(parent);
}
@Override
public Builder clear() {
super.clear();
bitField0_ = 0;
id_ = 0;
code_ = "";
name_ = "";
return this;
}
@Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return DataGen.internal_static_demon_research_proto_DataOriginal_descriptor;
}
@Override
public DataOriginal getDefaultInstanceForType() {
return DataOriginal.getDefaultInstance();
}
@Override
public DataOriginal build() {
DataOriginal result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@Override
public DataOriginal buildPartial() {
DataOriginal result = new DataOriginal(this);
if (bitField0_ != 0) { buildPartial0(result); }
onBuilt();
return result;
}
private void buildPartial0(DataOriginal result) {
int from_bitField0_ = bitField0_;
if (((from_bitField0_ & 0x00000001) != 0)) {
result.id_ = id_;
}
if (((from_bitField0_ & 0x00000002) != 0)) {
result.code_ = code_;
}
if (((from_bitField0_ & 0x00000004) != 0)) {
result.name_ = name_;
}
}
@Override
public Builder clone() {
return super.clone();
}
@Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return super.setField(field, value);
}
@Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return super.clearField(field);
}
@Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return super.clearOneof(oneof);
}
@Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, Object value) {
return super.setRepeatedField(field, index, value);
}
@Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return super.addRepeatedField(field, value);
}
@Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof DataOriginal) {
return mergeFrom((DataOriginal)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(DataOriginal other) {
if (other == DataOriginal.getDefaultInstance()) return this;
if (other.getId() != 0) {
setId(other.getId());
}
if (!other.getCode().isEmpty()) {
code_ = other.code_;
bitField0_ |= 0x00000002;
onChanged();
}
if (!other.getName().isEmpty()) {
name_ = other.name_;
bitField0_ |= 0x00000004;
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
onChanged();
return this;
}
@Override
public final boolean isInitialized() {
return true;
}
@Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
if (extensionRegistry == null) {
throw new NullPointerException();
}
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 8: {
id_ = input.readInt32();
bitField0_ |= 0x00000001;
break;
} // case 8
case 18: {
code_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000002;
break;
} // case 18
case 26: {
name_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000004;
break;
} // case 26
default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag
}
break;
} // default:
} // switch (tag)
} // while (!done)
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.unwrapIOException();
} finally {
onChanged();
} // finally
return this;
}
private int bitField0_;
private int id_ ;
/**
* <code>int32 id = 1;</code>
* @return The id.
*/
@Override
public int getId() {
return id_;
}
/**
* <code>int32 id = 1;</code>
* @param value The id to set.
* @return This builder for chaining.
*/
public Builder setId(int value) {
id_ = value;
bitField0_ |= 0x00000001;
onChanged();
return this;
}
/**
* <code>int32 id = 1;</code>
* @return This builder for chaining.
*/
public Builder clearId() {
bitField0_ = (bitField0_ & ~0x00000001);
id_ = 0;
onChanged();
return this;
}
private Object code_ = "";
/**
* <code>string code = 2;</code>
* @return The code.
*/
public String getCode() {
Object ref = code_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
code_ = s;
return s;
} else {
return (String) ref;
}
}
/**
* <code>string code = 2;</code>
* @return The bytes for code.
*/
public com.google.protobuf.ByteString
getCodeBytes() {
Object ref = code_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
code_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>string code = 2;</code>
* @param value The code to set.
* @return This builder for chaining.
*/
public Builder setCode(
String value) {
if (value == null) { throw new NullPointerException(); }
code_ = value;
bitField0_ |= 0x00000002;
onChanged();
return this;
}
/**
* <code>string code = 2;</code>
* @return This builder for chaining.
*/
public Builder clearCode() {
code_ = getDefaultInstance().getCode();
bitField0_ = (bitField0_ & ~0x00000002);
onChanged();
return this;
}
/**
* <code>string code = 2;</code>
* @param value The bytes for code to set.
* @return This builder for chaining.
*/
public Builder setCodeBytes(
com.google.protobuf.ByteString value) {
if (value == null) { throw new NullPointerException(); }
checkByteStringIsUtf8(value);
code_ = value;
bitField0_ |= 0x00000002;
onChanged();
return this;
}
private Object name_ = "";
/**
* <code>string name = 3;</code>
* @return The name.
*/
public String getName() {
Object ref = name_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
name_ = s;
return s;
} else {
return (String) ref;
}
}
/**
* <code>string name = 3;</code>
* @return The bytes for name.
*/
public com.google.protobuf.ByteString
getNameBytes() {
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;
}
}
/**
* <code>string name = 3;</code>
* @param value The name to set.
* @return This builder for chaining.
*/
public Builder setName(
String value) {
if (value == null) { throw new NullPointerException(); }
name_ = value;
bitField0_ |= 0x00000004;
onChanged();
return this;
}
/**
* <code>string name = 3;</code>
* @return This builder for chaining.
*/
public Builder clearName() {
name_ = getDefaultInstance().getName();
bitField0_ = (bitField0_ & ~0x00000004);
onChanged();
return this;
}
/**
* <code>string name = 3;</code>
* @param value The bytes for name to set.
* @return This builder for chaining.
*/
public Builder setNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) { throw new NullPointerException(); }
checkByteStringIsUtf8(value);
name_ = value;
bitField0_ |= 0x00000004;
onChanged();
return this;
}
@Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFields(unknownFields);
}
@Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
}
// @@protoc_insertion_point(builder_scope:demon.research.proto.DataOriginal)
}
// @@protoc_insertion_point(class_scope:demon.research.proto.DataOriginal)
private static final DataOriginal DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new DataOriginal();
}
public static DataOriginal getDefaultInstance() {
return DEFAULT_INSTANCE;
}
private static final com.google.protobuf.Parser<DataOriginal>
PARSER = new com.google.protobuf.AbstractParser<DataOriginal>() {
@Override
public DataOriginal parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
Builder builder = newBuilder();
try {
builder.mergeFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(builder.buildPartial());
} catch (com.google.protobuf.UninitializedMessageException e) {
throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(e)
.setUnfinishedMessage(builder.buildPartial());
}
return builder.buildPartial();
}
};
public static com.google.protobuf.Parser<DataOriginal> parser() {
return PARSER;
}
@Override
public com.google.protobuf.Parser<DataOriginal> getParserForType() {
return PARSER;
}
@Override
public DataOriginal getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_demon_research_proto_DataOriginal_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_demon_research_proto_DataOriginal_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\022DataOriginal.proto\022\024demon.research.pro" +
"to\"6\n\014DataOriginal\022\n\n\002id\030\001 \001(\005\022\014\n\004code\030\002" +
" \001(\t\022\014\n\004name\030\003 \001(\tB\037\n\024demon.research.pro" +
"toB\007DataGenb\006proto3"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_demon_research_proto_DataOriginal_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_demon_research_proto_DataOriginal_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_demon_research_proto_DataOriginal_descriptor,
new String[] { "Id", "Code", "Name", });
}
// @@protoc_insertion_point(outer_class_scope)
}
1、DataGen
类只提供框架方法。
public final class DataGen {
private DataGen() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_demon_research_proto_DataOriginal_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_demon_research_proto_DataOriginal_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
2、接口 DataOriginalOrBuilder
,只提供了get
方法。
int getId();
String getCode();
com.google.protobuf.ByteString getCodeBytes();
String getName();
com.google.protobuf.ByteString getNameBytes();
3、类DataOriginal
继承类 com.google.protobuf.GeneratedMessageV3
,实现接口 DataOriginalOrBuilder
提供了get
方法,并保存了数据。同时提供了序列化与反序列化能力
public static final int ID_FIELD_NUMBER = 1;
private int id_ = 0;
/**
* <code>int32 id = 1;</code>
* @return The id.
*/
@Override
public int getId() {
return id_;
}
4、类Builder
继承类 com.google.protobuf.GeneratedMessageV3.Builder<Builder>
,实现接口 DataOriginalOrBuilder
提供了get/set
方法,