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 - 向量 中,我們將瞭解向量,一種複合型別。

廣告

© . All rights reserved.