FlatBuffers - 布林值



概述

bool 資料型別是 FlatBuffers 的基本構建塊之一。它在使用的語言中翻譯為布林值,例如JavaPython 等。

繼續我們從FlatBuffers - 字串章節的劇院示例,以下是我們需要使用的語法,以指示 FlatBuffers 我們將建立bool值:

theater.fbs

namespace com.tutorialspoint.theater;

table Theater {
   drive_in:bool;
}
root_type Theater;

現在我們的表格包含一個bool屬性。預設值為false。

從 FBS 檔案建立 Java 類

要使用 FlatBuffers,我們現在必須使用flatc二進位制檔案從這個“.fbs”檔案建立所需的類。讓我們看看如何操作:

flatc  --java theater.fbs

這將在當前目錄的com > tutorialspoint > theater資料夾中建立一個Theater.java類。我們在應用程式中使用此類,類似於FlatBuffers - Schema章節中所做的那樣。

使用從 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);

      boolean driveIn = true; 

      // create theater FlatBuffers using startTheater() method
      Theater.startTheater(builder);
      // add details to the Theater FlatBuffer
      Theater.addDriveIn(builder, driveIn);

      // 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("Drive In: " + theater.driveIn());
      }
   }
}

編譯專案

現在我們已經設定了讀取器寫入器,讓我們編譯專案。

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:
8

反序列化已序列化的物件

現在,讓我們執行讀取器以從同一檔案讀取:

java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader

Reading from file theater_flatbuffers_output
Drive In: true

因此,正如我們所看到的,我們能夠透過將二進位制資料反序列化為劇院物件來讀取已序列化的字串。讓我們在下一章FlatBuffers - 列舉中檢視數字

廣告
© . All rights reserved.