Flat Buffers - struct

概述

struct 数据类型是 Flat Buffers 的复合数据类型之一。它用于创建不可变的数据结构。结构占用的内存较少,查找速度很快。结构通常是标量类型的组合。

继续使用 Flat Buffers - String 一章中的 theater 示例,以下是我们需要用来指示 FlatBuffers 我们将创建 struct 的语法 −

theater.fbs

namespace com.tutorialspoint.theater;

struct Position {
   x: int;
   y: int;
   z: int;
}
table Theater {
   location: Position;
}
root_type Theater;

现在我们的 table 包含定义为 Position 类型位置的 struct 属性。Position 是一个定义三个 int 数据结构的结构。

从 fbs 文件创建 Java 类

要使用 FlatBuffers,我们现在必须使用 flatc 二进制文件从此".fbs"文件创建所需的类。让我们看看如何做到这一点 −

flatc --java theater.fbs

这将在当前目录的 com > tutorialspoint > theater 文件夹中创建 TheaterPosition 类。我们在应用程序中使用此类的方式与 Flat Buffers - Schema 一章中的方式类似。

使用从 fbs 文件创建的 Java 类

创建和编写结构体

为了创建结构体,我们首先需要准备标量类型数组的偏移量,然后我们可以将向量添加到Flat Buffers。

// 为位置结构体创建偏移量
int location = Position.createPosition(builder, 100, 110, 120);

// 向 Theater FlatBuffer 添加详细信息
Theater.addLocation(builder, location);

以下示例代码展示了创建 Ints 结构的过程。

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 {
    // 创建一个Flat Buffers构建器
    // 它将用于创建Theater  FlatBuffer
    FlatBufferBuilder builder = new FlatBufferBuilder(1024);
    
    // 为位置结构创建偏移量
    int location = Position.createPosition(builder, 100, 110, 120);
    
    // 使用 startTheater() 方法创建Theater  FlatBuffers
    Theater.startTheater(builder);
    // 向Theater  FlatBuffer 添加详细信息
    Theater.addLocation(builder, location);
    
    // 标记在 Greet FlatBuffer 中输入的数据的结束
    int theater = Theater.endTheater(builder);
    
    // 完成构建器
    builder.finish(theater);
    
    // 获取要存储的字节
    byte[] data = builder.sizedByteArray();
    
    String filename = "theater_flatbuffers_output";
    System.out.println("将Theater 保存到文件:" + filename);
    // 将构建器内容写入名为 theater_flatbuffers_output 的文件
      try(FileOutputStream output = new FileOutputStream(filename)){
         output.write(data);
      }
      System.out.println("Saved theater with following data to disk: 
" + theater);
   }
}	

读取结构体

为了读取结构体,我们有方法来获取结构体的每个值。

Position position = theater.location();
System.out.println("x: " + position.x());
System.out.println("y: " + position.y());
System.out.println("z: " + position.z());

以下示例代码显示了读取 int 结构体的过程。

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)) {
        // 获取序列化数据
        byte[] data = input.readAllBytes();
        ByteBuffer buf = ByteBuffer.wrap(data);
        // 读取序列化数据中的根对象
        Theater theater = Theater.getRootAsTheater(buf);
        // 打印Theater 值
        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());  
      }
   }
}

编译项目

现在我们已经设置了readerwriter,让我们编译项目。

mvn clean install

序列化 Java 对象

现在,编译后,让我们先执行writer

> java -cp .	arget\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

反序列化序列化对象

现在,让我们执行 reader 以从同一文件中读取 −

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

Reading from file theater_flatbuffers_output
Location:
x: 100
y: 110
z: 120

因此,如我们所见,我们能够通过将二进制数据反序列化为 Theater 对象来读取序列化的 struct。在下一章 Flat Buffers - union 中,我们将研究 union,一种复合类型。