- Flat Buffers 教程
- Flat Buffers - 首页
- Flat Buffers - 简介
- Flat Buffers - 模式
- Flat Buffers - 构造
- Flat Buffers - 表格
- Flat Buffers - 字符串
- Flat Buffers - 数字
- Flat Buffers - 布尔值
- Flat Buffers - 枚举
- Flat Buffers - 向量
- Flat Buffers - 结构体
- Flat Buffers - 联合体
- Flat Buffers - 嵌套表格
- Flat Buffers - 默认值
- Flat Buffers - JSON 转二进制
- Flat Buffers - 二进制转 JSON
- Flat Buffers - 可变缓冲区
- Flat Buffers - 向后兼容性
- Flat Buffers - 语言独立性
- Flat Buffers 有用资源
- Flat Buffers - 快速指南
- Flat Buffers - 有用资源
- Flat Buffers - 讨论
Flat Buffers - 枚举
概述
enum 数据类型是 Flat Buffers 的复合数据类型之一。它等价于我们使用的语言中的 enum,例如 Java 等。
继续我们从 Flat Buffers - 字符串 章节开始的 theater 示例,以下是我们需要使用的语法,以指示 FlatBuffers 我们将创建一个 enum:
theater.fbs
namespace com.tutorialspoint.theater;
enum PAYMENT_SYSTEM: byte { CASH = 0, CREDIT_CARD = 1, DEBIT_CARD, APP = 3 }
table Theater {
payment:PAYMENT_SYSTEM;
}
root_type Theater;
现在我们的 表格包含一个 enum 属性。我们为每个枚举常量分配了一个值,除了 DEBIT_CARD,它默认取增量值 2。
我们定义了 enum,并在下面将其用作数据类型以及 "payment" 属性。
从 fbs 文件创建 Java 类
要使用 FlatBuffers,我们现在必须使用 flatc 二进制文件从这个 ".fbs" 文件创建所需的类。让我们看看如何做到这一点:
flatc --java theater.fbs
这将在当前目录中的 com > tutorialspoint > theater 文件夹中创建一个 Theater.java 和 PAYMENT_SYSTEM 类。我们在应用程序中使用此类,类似于在 Flat Buffers - 模式 章节中所做的那样。
使用从 fbs 文件创建的 Java 类
TheaterWriter.java
package com.tutorialspoint.theater;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.flatbuffers.FlatBufferBuilder;
public class TheaterWriter {
public static void main(String[] args) throws FileNotFoundException, IOException {
// create a flat buffer builder
// it will be used to create Theater FlatBuffer
FlatBufferBuilder builder = new FlatBufferBuilder(1024);
// create theater FlatBuffers using startTheater() method
Theater.startTheater(builder);
// add details to the Theater FlatBuffer
Theater.addPayment(builder, PAYMENT_SYSTEM.DEBIT_CARD);
// mark end of data being entered in Greet FlatBuffer
int theater = Theater.endTheater(builder);
// finish the builder
builder.finish(theater);
// get the bytes to be stored
byte[] data = builder.sizedByteArray();
String filename = "theater_flatbuffers_output";
System.out.println("Saving theater to file: " + filename);
// write the builder content to the file named theater_flatbuffers_output
try(FileOutputStream output = new FileOutputStream(filename)){
output.write(data);
}
System.out.println("Saved theater with following data to disk: \n" + theater);
}
}
接下来,我们将有一个 读取器来读取 theater 信息:
TheaterReader.java
package com.tutorialspoint.theater;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
public class TheaterReader {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "theater_flatbuffers_output";
System.out.println("Reading from file " + filename);
try(FileInputStream input = new FileInputStream(filename)) {
// get the serialized data
byte[] data = input.readAllBytes();
ByteBuffer buf = ByteBuffer.wrap(data);
// read the root object in serialized data
Theater theater = Theater.getRootAsTheater(buf);
// print theater values
System.out.println("Payment Method: " + theater.payment());
}
}
}
编译项目
现在我们已经设置了 读取器和 写入器,让我们编译项目。
mvn clean install
序列化 Java 对象
现在,编译后,让我们首先执行 写入器:
> java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater information to file: theater_flatbuffers_output Saved theater information with following data to disk: 8
反序列化序列化对象
现在,让我们执行 读取器以从同一文件读取:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Payment Method: 2
因此,正如我们所看到的,我们能够通过将二进制数据反序列化为 Theater 对象来读取序列化的 enum。在下一章 Flat Buffers - 向量 中,我们将了解向量,一种复合类型。
广告