Flat Buffers - struct



Overview

The struct data type is one of the composite datatypes of Flat Buffers. It is used to create a immutable data structure. A struct takes less memory is quite fast in lookup. A struct is generally a combination of scalar types.

Continuing with our theater example from Flat Buffers - String chapter, following is the syntax that we need to have to instruct FlatBuffers that we will be creating a struct

theater.fbs

namespace com.tutorialspoint.theater;

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

Now our table contains struct attribute defined as location of type Position. Position is a struct to define a data structure of three ints.

Creating Java Classes from fbs File

To use FlatBuffers, we will now have to use flatc binary to create the required classes from this ".fbs" file. Let us see how to do that −

flatc  --java theater.fbs

This will create Theater and Position classes in com > tutorialspoint > theater folder in current directory. We're using this class in our application similar to as done in Flat Buffers - Schema chapter.

Using Java Classes created from fbs File

Creating and Writing Struct

In order to create a Struct, we need to first prepare the offset of scalar type array and then we can add the vector to the flat buffer.

// create offset for location struct
int location = Position.createPosition(builder, 100, 110, 120);

// add details to the Theater FlatBuffer
Theater.addLocation(builder, location); 

Following example code is showing the process of creating a Struct of 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 {
      // 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);
   }
}	

Reading Struct

In order to read a Struct, we have methods to get each value of struct.

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

Following example code is showing the process of reading a Struct of 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)) {
         // 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());        
      }
   }
}

Compile the project

Now that we have set up the reader and the writer, let us compile the project.

mvn clean install

Serialize the Java Object

Now, post compilation, let us execute the writer first −

> 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

Deserialize the Serialized Object

Now, let us execute the reader to read from the same file −

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

So, as we see, we are able to read the serialized struct by deserializing the binary data to Theater object. In the next chapter Flat Buffers - union, we will look at the union, a composite type.

Advertisements