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.