typora-copy-images-to:アセット
typora-root-url:アセット
プロトコルバッファ
プロトコルバッファの概要
- Protocal Buffers(略してprotobuf)は、構造化データのシリアル化と逆シリアル化に使用されるGoogleテクノロジーであり、RPCシステムや連続データストレージシステムでよく使用されます。
- これはXMLの生成と解析に似ていますが、protobufはXMLよりも効率的ですが、protobufはバイトコードを生成します。バイトコードは、json、JavaのSerializableなどと同様にXMLよりも読みにくくなっています。
- データストレージまたはRPCデータ交換フォーマットに非常に適しています。これは、通信プロトコル、データストレージ、およびその他の分野で、言語に依存せず、プラットフォームに依存せず、拡張可能なシリアル化された構造化データ形式に使用できます。
- 参照:https://zhuanlan.zhihu.com/p/53339153
アイデアインストールprotobufプラグイン
プラグインprotobufサポートをインストールしてから再起動します
- データパッケージでprotobuf-jetbrains-plugin-0.13.0.zipを見つけ、IDEAにプラグインをインストールします
ProtoBufを使用してデータをシリアル化する
Mavenの依存関係とプラグインを構成する
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<!-- Protobuf插件 src/main/java/proto -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protoSourceRoot>${project.basedir}/src/main/java/proto</protoSourceRoot>
<protocArtifact>
com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
</protocArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
プロトファイルを書く
-
protobuf3の文法は、配布物の「protobuf3文法」を指します。
-
メインフォルダにprotoディレクトリを作成し、protoファイルを書き込みます
syntax = "proto3";
option java_package = "com.wangjie.protobuf";
option java_outer_classname = "Msg";
message User {
int32 id = 1;
string name = 2;
string sex = 3;
}
message Stats{
int32 hp =1;
int32 mp = 2;
int32 exp =3;
int32 atk =4;
int32 def =5;
}
注:クラス名をメッセージ名と同じにすることはできません
protobufとjavaタイプの比較表
.protoタイプ | Javaタイプ | 備考 |
---|---|---|
ダブル | ダブル | |
浮く | 浮く | |
int32 | int | 可変長エンコーディングを使用します。負のエンコーディングは非効率的です。フィールドに負の値がある可能性がある場合は、代わりにsint32を使用してください。 |
int64 | 長いです | 可変長エンコーディングを使用します。負のエンコーディングは非効率的です。フィールドに負の値がある可能性がある場合は、代わりにsint64を使用してください。 |
uint32 | int | 可変長エンコーディングを使用します。 |
uint64 | 長いです | 可変長エンコーディングを使用します。 |
sint32 | int | 可変長エンコーディングを使用します。符号付き整数値。通常のint32と比較して、負の数をより効率的にエンコードします。 |
sint64 | 長いです | 可変長エンコーディングを使用します。符号付き整数値。通常のint64と比較して、負の数をより効率的にエンコードします。 |
fixed32 | int | 常に4バイトです。通常、値が2 ^ 28より大きい場合は、uint32よりも効率的です。 |
fixed64 | 長いです | 常に8バイトです。通常、値が2 ^ 56より大きい場合は、uint64よりも効率的です。 |
sfixed32 | int | 常に4バイトです。 |
sfixed64 | 長いです | 常に8バイトです。 |
ブール | ブール値 | |
ストリング | ストリング | 文字列には、常にUTF-8エンコーディングまたは7ビットASCIIテキストが含まれている必要があります。 |
バイト | ByteString | 任意のバイトシーケンスを含めることができます。 |
protobuf:compileコンパイルコマンドを実行します
- protoファイルをJavaコードにコンパイルします
ProtoBufのシリアル化と逆シリアル化を使用してコードを記述します
package com.wangjie.protobuf;
import com.google.protobuf.InvalidProtocolBufferException;
public class ProtoBufMsg {
public static void main(String[] args) {
Msg.User.Builder builder = Msg.User.newBuilder();
builder.setId(1);
builder.setName("saber");
builder.setSex("female");
byte[] bytes = builder.build().toByteArray();
System.out.println("--protobuf---");
for (byte b : bytes) {
System.out.print(b);
}
System.out.println();
System.out.println("---");
try {
Msg.User user = Msg.User.parseFrom(bytes);
System.out.println(user);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
Msg.Stats.Builder statsBuild = Msg.Stats.newBuilder();
statsBuild.setHp(10);
statsBuild.setMp(10);
statsBuild.setExp(10);
statsBuild.setDef(10);
statsBuild.setAtk(10);
byte[] bytes1 = statsBuild.build().toByteArray();
System.out.println("--protobuf---");
for (byte b : bytes1) {
System.out.print(b);
}
System.out.println();
System.out.println("---");
Msg.Stats stats = null;
try {
stats = Msg.Stats.parseFrom(bytes1);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
System.out.println(stats);
}
}