
- Flat Buffers - Home
- Flat Buffers - Introduction
- Flat Buffers - Environment Setup
- Flat Buffers - Schema
- Flat Buffers - Constructs
- Flat Buffers - table
- Flat Buffers - string
- Flat Buffers - Numbers
- Flat Buffers - Boolean
- Flat Buffers - Enum
- Flat Buffers - Vector
- Flat Buffers - Struct
- Flat Buffers - Union
- Flat Buffers - Nested Table
- Flat Buffers - Default Values
- Flat Buffers - JSON to Binary
- Flat Buffers - Binary to JSON
- Flat Buffers - Mutatable Buffers
- Flat Buffers - Backward Compatability
- Flat Buffers - Language Independence
- Flat Buffers Useful Resources
- Flat Buffers - Quick Guide
- Flat Buffers - Useful Resources
- Flat Buffers - Discussion
Flat Buffers - Backward Compatability
Overview
FlatBuffers schema is backward compatible. It means, if we change add or remove attributes to a flatbuffers schema later, still the existing code can work. This is very useful while maintaining the legacy codebase. Consider a scenario where Theater Schema contains only name and address as shown below:
theater.fbs
namespace com.tutorialspoint.theater; table Theater { name:string; address:string; } root_type Theater;
If we generate code for this schema, it will support storing name and address in flatbuffer bin file.
Now with time, we need to add a mobile number to the schema, then we need to generate the updated code again. And as a consequence, we need to updated writer and reader code as well. But in production, generally changing code directly is not easy and making such a change may break the entire system. Flat buffers here ensure that old reader code will still working fine with the new schema based generated flatbuffers bin file with no change.
Creating Java Classes from fbs File
To use Flat Buffers, we will now have to use flatc binary to create the required class from this ".fbs" file. Let us see how to do that −
flatc --java theater.fbs
This will create a Theater.java class 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
First let's create a writer to write the theater information −
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); int name = builder.createString("Silver Screener"); int address = builder.createString("212, Maple Street, LA, California"); // create theater FlatBuffers using startTheater() method Theater.startTheater(builder); // add the name and address to the Theater FlatBuffer Theater.addName(builder, name); Theater.addAddress(builder, address); // 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); } }
Next, we will have a reader to read the theater information −
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("Name: " + theater.name()); System.out.println("Address: " + theater.address()); } } }
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 to file: theater_flatbuffers_output Saved theater with following data to disk: 72
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 Name: Silver Screener Address: 212, Maple Street, LA, California
Backward Compatability Test
Now let's add a mobile number to the schema, update the writer and run the reader without updating it to check the backward compatability.
theater.fbs
namespace com.tutorialspoint.theater; table Theater { name:string; address:string; mobile:int; } root_type Theater;
Creating Java Classes from fbs File
Use flatc binary to create the required class from this ".fbs" file.
flatc --java theater.fbs
Using Java Classes created from fbs File
First let's create a writer to write the theater information −
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); int name = builder.createString("Silver Screener"); int address = builder.createString("212, Maple Street, LA, California"); // create theater FlatBuffers using startTheater() method Theater.startTheater(builder); // add the name, address and mobile to the Theater FlatBuffer Theater.addName(builder, name); Theater.addAddress(builder, address); Theater.addMobile(builder, 12233345); // 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); } }
Compile the project
Now that we have set up 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 to file: theater_flatbuffers_output Saved theater with following data to disk: 76
Deserialize the Serialized Object Using old reader
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 Name: Silver Screener Address: 212, Maple Street, LA, California
Update Reader and Deserialize again
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("Name: " + theater.name()); System.out.println("Address: " + theater.address()); System.out.println("Mobile: " + theater.mobile()); } } }
Compile the project
Now that we have set up the reader and the writer, let us compile the project.
mvn clean install
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 Name: Silver Screener Address: 212, Maple Street, LA, California Mobile: 12233345