Flat Buffers - 可变缓冲区



概述

每当我们创建一个 Flat Buffers 文件时,从那时起它就是只读的。我们可以使用 flatc 提供的具有 const 访问器的类来读取此文件。这有助于在多个读取器之间使用 flat buffer 文件时保持一致性。但有时,我们可能需要在读取后修改一个值,并需要将修改后的值传递给下一个读取器。我们可以通过从头开始创建一个新的 flat buffers 来实现这一点,这对于大型更改来说更好且更高效。对于小的更改,Flat Buffers 为 flatc 编译器提供了一个选项 --gen-mutable

以生成非 const 访问器来修改 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 - 模式 章节中所做的那样。

使用从 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 offset for name and address
      int name = builder.createString("Silver Screener");
      int address = builder.createString("212, Maple Street, LA, California");
      
      // create theater FlatBuffers using startTheater() method
      Theater.startTheater(builder);
      // add details to the Theater FlatBuffer
      Theater.addName(builder, name);   
      Theater.addAddress(builder, address);
      Theater.addMobile(builder, 12233345);

      // 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);
   }
}	

接下来,我们将有一个 读取器来读取 剧院信息 -

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("Name: " + theater.name());
         System.out.println("Address: " + theater.address());
         System.out.println("Mobile: " + theater.mobile());
         
         
         // Update mobile
         theater.mutateMobile(22333341);
         
         // we can write the theater object again to send it further
         // read the updated mobile value
         System.out.println("Updated Mobile: " + theater.mobile());
      }
   }
}

编译项目

现在我们已经设置了 读取器写入器,让我们编译项目。

mvn clean install

序列化 Java 对象

现在,编译后,让我们先执行 写入器 -

java -cp .\target\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 .\target\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
广告

© . All rights reserved.