- Flat Buffers 教程
- Flat Buffers - 首頁
- Flat Buffers - 簡介
- Flat Buffers - 模式
- Flat Buffers - 結構
- Flat Buffers - 表格
- Flat Buffers - 字串
- Flat Buffers - 數字
- Flat Buffers - 布林值
- Flat Buffers - 列舉
- Flat Buffers - 向量
- Flat Buffers - 結構體
- Flat Buffers - 聯合體
- Flat Buffers - 巢狀表格
- Flat Buffers - 預設值
- Flat Buffers - JSON 轉二進位制
- Flat Buffers - 二進位制轉 JSON
- Flat Buffers - 可變緩衝區
- Flat Buffers - 向後相容性
- Flat Buffers - 語言獨立性
- Flat Buffers 有用資源
- Flat Buffers - 快速指南
- Flat Buffers - 有用資源
- Flat Buffers - 討論
Flat Buffers - 結構體
概述
struct 資料型別是 Flat Buffers 的複合資料型別之一。它用於建立不可變的資料結構。結構體佔用更少的記憶體,並且查詢速度很快。結構體通常是標量型別的組合。
繼續我們從 Flat Buffers - 字串 章節中的theater 示例,以下是我們需要使用的語法,以指示 FlatBuffers 我們將建立一個struct:
theater.fbs
namespace com.tutorialspoint.theater;
struct Position {
x: int;
y: int;
z: int;
}
table Theater {
location: Position;
}
root_type Theater;
現在我們的表格包含定義為 Position 型別位置的結構體屬性。Position 是一個結構體,用於定義三個整數的資料結構。
從 fbs 檔案建立 Java 類
要使用 FlatBuffers,我們現在必須使用flatc二進位制檔案從這個“.fbs”檔案建立所需的類。讓我們看看如何操作:
flatc --java theater.fbs
這將在當前目錄中的com > tutorialspoint > theater資料夾中建立Theater和Position類。我們在應用程式中使用此類,類似於在Flat Buffers - 模式章節中所做的那樣。
使用從 fbs 檔案建立的 Java 類
建立和寫入結構體
為了建立一個結構體,我們需要首先準備標量型別陣列的偏移量,然後我們可以將向量新增到扁平緩衝區。
// create offset for location struct int location = Position.createPosition(builder, 100, 110, 120); // add details to the Theater FlatBuffer Theater.addLocation(builder, location);
以下示例程式碼顯示了建立整數結構體的過程。
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 location struct
int location = Position.createPosition(builder, 100, 110, 120);
// create theater FlatBuffers using startTheater() method
Theater.startTheater(builder);
// add details to the Theater FlatBuffer
Theater.addLocation(builder, location);
// 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);
}
}
讀取結構體
為了讀取結構體,我們有方法可以獲取結構體的每個值。
Position position = theater.location();
System.out.println("x: " + position.x());
System.out.println("y: " + position.y());
System.out.println("z: " + position.z());
以下示例程式碼顯示了讀取整數結構體的過程。
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("Location: ");
Position position = theater.location();
System.out.println("x: " + position.x());
System.out.println("y: " + position.y());
System.out.println("z: " + position.z());
}
}
}
編譯專案
現在我們已經設定了讀取器和寫入器,讓我們編譯專案。
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: 16
反序列化序列化物件
現在,讓我們執行讀取器從同一個檔案讀取:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Location: x: 100 y: 110 z: 120
因此,正如我們所看到的,我們能夠透過將二進位制資料反序列化為Theater物件來讀取序列化的結構體。在下一章Flat Buffers - 聯合體中,我們將瞭解聯合體,這是一種複合型別。
廣告