Flat Buffers - 可变缓冲区
概述
每当我们创建一个Flat Buffers文件时,它都是只读的。我们可以使用 flatc 提供的具有 const 访问器的类来读取此文件。这有助于在多个读取器中使用Flat Buffers文件。但有时,我们可能需要在读取后修改某个值,并需要将修改后的值传递给下一个读取器。我们可以通过从头开始创建一个新的Flat Buffers来实现这一点,这对于大的变化来说更好、更高效。对于小改动,Flat Buffers 为 flatc complier 提供了一个选项 --gen-mutable,用于生成非常量访问器来修改 flatbuffers 文件,如下所示:
flatc --java --gen-mutable theater.fbs
示例
请考虑以下模式。
theater.fbs
namespace com.tutorialspoint.theater; table Theater { name:string; address:string; int mobile; } root_type Theater;
从 fbs 文件创建 Java 类
要使用 Flat Buffers,我们现在将使用可变模式下的 flatc 编译器从此".fbs"文件创建所需的类。让我们看看如何做到这一点 −
flatc --java --gen-mutable theater.fbs
这将在当前目录中的 com > tutorialspoint > theater 文件夹中创建一个 Theater.java 类。我们在应用程序中使用此类的方式与Flat Buffers - Schema一章中的方式类似。
使用从 fbs 文件创建的 Java 类
首先,让我们创建一个writer来写入theater信息−
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 { // 创建一个Flat Buffers构建器 // 它将用于创建Theater FlatBuffer FlatBufferBuilder builder = new FlatBufferBuilder(1024); // 为名字和地址创建偏移量 int name = builder.createString("Silver Screener"); int address = builder.createString("212, Maple Street, LA, California"); // 使用 startTheater() 方法创建Theater FlatBuffers Theater.startTheater(builder); // 向Theater FlatBuffer 添加详细信息 Theater.addName(builder, name); Theater.addAddress(builder, address); Theater.addMobile(builder, 12233345); // 标记在 Gret FlatBuffer 中输入数据的结束 int theater = Theater.endTheater(builder); // 完成构建器 builder.finish(theater); // 获取要存储的字节 byte[] data = builder.sizedByteArray(); String filename = "theater_flatbuffers_output"; System.out.println("Saving theater to file: " + filename); // 将构建器内容写入名为 theater_flatbuffers_output 的文件 try(FileOutputStream output = new FileOutputStream(filename)){ output.write(data); } System.out.println("Saved theater with following data to disk: " + 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)) { // 获取序列化数据 byte[] data = input.readAllBytes(); ByteBuffer buf = ByteBuffer.wrap(data); // 读取序列化数据中的根对象 Theater theater = Theater.getRootAsTheater(buf); // 打印 theater 剧场值 System.out.println("Name: " + theater.name()); System.out.println("Address: " + theater.address()); System.out.println("Mobile: " + theater.mobile()); // 更新 mobile theater.mutateMobile(22333341); // 我们可以再次编写 theater 对象以将其进一步发送 // 读取更新后的 mobile 值 System.out.println("Updated Mobile: " + theater.mobile()); } } }
编译项目
现在我们已经设置了reader和writer,让我们编译项目。
mvn clean install
序列化 Java 对象
现在,编译后,让我们先执行writer −
java -cp . arget\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater to file: theater_flatbuffers_output Saved theater with following data to disk: 76
反序列化序列化对象
现在,让我们执行读取器来读取同一个文件−
java -cp . arget\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Name: Silver Screener Address: 212, Maple Street, LA, California Mobile: 12233345 Updated Mobile: 22333341