diff --git a/libraries-data-io-2/pom.xml b/libraries-data-io-2/pom.xml
new file mode 100644
index 0000000000..2e611ef4a0
--- /dev/null
+++ b/libraries-data-io-2/pom.xml
@@ -0,0 +1,29 @@
+
+
+ 4.0.0
+
+ com.baeldung
+ parent-modules
+ 1.0.0-SNAPSHOT
+
+
+ libraries-data-io-2
+ libraries-data-io-3
+
+
+ 19
+ 19
+ UTF-8
+ 23.5.26
+
+
+
+ com.google.flatbuffers
+ flatbuffers-java
+ ${google-flatbuffers.version}
+
+
+
+
\ No newline at end of file
diff --git a/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Color.java b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Color.java
new file mode 100644
index 0000000000..335093c7f9
--- /dev/null
+++ b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Color.java
@@ -0,0 +1,18 @@
+// automatically generated by the FlatBuffers compiler, do not modify
+
+package MyGame.terrains;
+
+@SuppressWarnings("unused")
+public final class Color {
+ private Color() { }
+ public static final byte Brown = 0;
+ public static final byte Red = 1;
+ public static final byte Green = 2;
+ public static final byte Blue = 3;
+ public static final byte White = 4;
+
+ public static final String[] names = { "Brown", "Red", "Green", "Blue", "White", };
+
+ public static String name(int e) { return names[e]; }
+}
+
diff --git a/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Effect.java b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Effect.java
new file mode 100644
index 0000000000..137c74a777
--- /dev/null
+++ b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Effect.java
@@ -0,0 +1,51 @@
+// automatically generated by the FlatBuffers compiler, do not modify
+
+package MyGame.terrains;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import com.google.flatbuffers.BaseVector;
+import com.google.flatbuffers.Constants;
+import com.google.flatbuffers.FlatBufferBuilder;
+import com.google.flatbuffers.Table;
+
+@SuppressWarnings("unused")
+public final class Effect extends Table {
+ public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
+ public static Effect getRootAsEffect(ByteBuffer _bb) { return getRootAsEffect(_bb, new Effect()); }
+ public static Effect getRootAsEffect(ByteBuffer _bb, Effect obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
+ public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
+ public Effect __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
+
+ public String name() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
+ public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(4, 1); }
+ public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 4, 1); }
+ public short damage() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 0; }
+ public boolean mutateDamage(short damage) { int o = __offset(6); if (o != 0) { bb.putShort(o + bb_pos, damage); return true; } else { return false; } }
+
+ public static int createEffect(FlatBufferBuilder builder,
+ int nameOffset,
+ short damage) {
+ builder.startTable(2);
+ Effect.addName(builder, nameOffset);
+ Effect.addDamage(builder, damage);
+ return Effect.endEffect(builder);
+ }
+
+ public static void startEffect(FlatBufferBuilder builder) { builder.startTable(2); }
+ public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(0, nameOffset, 0); }
+ public static void addDamage(FlatBufferBuilder builder, short damage) { builder.addShort(1, damage, 0); }
+ public static int endEffect(FlatBufferBuilder builder) {
+ int o = builder.endTable();
+ return o;
+ }
+
+ public static final class Vector extends BaseVector {
+ public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
+
+ public Effect get(int j) { return get(new Effect(), j); }
+ public Effect get(Effect obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
+ }
+}
+
diff --git a/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Terrain.java b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Terrain.java
new file mode 100644
index 0000000000..20fa9f356b
--- /dev/null
+++ b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Terrain.java
@@ -0,0 +1,59 @@
+// automatically generated by the FlatBuffers compiler, do not modify
+
+package MyGame.terrains;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import com.google.flatbuffers.BaseVector;
+import com.google.flatbuffers.Constants;
+import com.google.flatbuffers.FlatBufferBuilder;
+import com.google.flatbuffers.Table;
+
+@SuppressWarnings("unused")
+public final class Terrain extends Table {
+ public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
+ public static Terrain getRootAsTerrain(ByteBuffer _bb) { return getRootAsTerrain(_bb, new Terrain()); }
+ public static Terrain getRootAsTerrain(ByteBuffer _bb, Terrain obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
+ public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
+ public Terrain __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
+
+ public MyGame.terrains.Vec3 pos() { return pos(new MyGame.terrains.Vec3()); }
+ public MyGame.terrains.Vec3 pos(MyGame.terrains.Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
+ public String name() { int o = __offset(6); return o != 0 ? __string(o + bb_pos) : null; }
+ public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(6, 1); }
+ public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 6, 1); }
+ public byte color() { int o = __offset(8); return o != 0 ? bb.get(o + bb_pos) : 3; }
+ public boolean mutateColor(byte color) { int o = __offset(8); if (o != 0) { bb.put(o + bb_pos, color); return true; } else { return false; } }
+ public String navigation() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
+ public ByteBuffer navigationAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
+ public ByteBuffer navigationInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); }
+ public MyGame.terrains.Effect effects(int j) { return effects(new MyGame.terrains.Effect(), j); }
+ public MyGame.terrains.Effect effects(MyGame.terrains.Effect obj, int j) { int o = __offset(12); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
+ public int effectsLength() { int o = __offset(12); return o != 0 ? __vector_len(o) : 0; }
+ public MyGame.terrains.Effect.Vector effectsVector() { return effectsVector(new MyGame.terrains.Effect.Vector()); }
+ public MyGame.terrains.Effect.Vector effectsVector(MyGame.terrains.Effect.Vector obj) { int o = __offset(12); return o != 0 ? obj.__assign(__vector(o), 4, bb) : null; }
+
+ public static void startTerrain(FlatBufferBuilder builder) { builder.startTable(5); }
+ public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
+ public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(1, nameOffset, 0); }
+ public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(2, color, 3); }
+ public static void addNavigation(FlatBufferBuilder builder, int navigationOffset) { builder.addOffset(3, navigationOffset, 0); }
+ public static void addEffects(FlatBufferBuilder builder, int effectsOffset) { builder.addOffset(4, effectsOffset, 0); }
+ public static int createEffectsVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
+ public static void startEffectsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
+ public static int endTerrain(FlatBufferBuilder builder) {
+ int o = builder.endTable();
+ return o;
+ }
+ public static void finishTerrainBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset); }
+ public static void finishSizePrefixedTerrainBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset); }
+
+ public static final class Vector extends BaseVector {
+ public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
+
+ public Terrain get(int j) { return get(new Terrain(), j); }
+ public Terrain get(Terrain obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
+ }
+}
+
diff --git a/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Vec3.java b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Vec3.java
new file mode 100644
index 0000000000..3c8c45a39e
--- /dev/null
+++ b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Vec3.java
@@ -0,0 +1,38 @@
+// automatically generated by the FlatBuffers compiler, do not modify
+
+package MyGame.terrains;
+
+import java.nio.ByteBuffer;
+
+import com.google.flatbuffers.BaseVector;
+import com.google.flatbuffers.FlatBufferBuilder;
+import com.google.flatbuffers.Struct;
+
+@SuppressWarnings("unused")
+public final class Vec3 extends Struct {
+ public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
+ public Vec3 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
+
+ public float x() { return bb.getFloat(bb_pos + 0); }
+ public void mutateX(float x) { bb.putFloat(bb_pos + 0, x); }
+ public float y() { return bb.getFloat(bb_pos + 4); }
+ public void mutateY(float y) { bb.putFloat(bb_pos + 4, y); }
+ public float z() { return bb.getFloat(bb_pos + 8); }
+ public void mutateZ(float z) { bb.putFloat(bb_pos + 8, z); }
+
+ public static int createVec3(FlatBufferBuilder builder, float x, float y, float z) {
+ builder.prep(4, 12);
+ builder.putFloat(z);
+ builder.putFloat(y);
+ builder.putFloat(x);
+ return builder.offset();
+ }
+
+ public static final class Vector extends BaseVector {
+ public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
+
+ public Vec3 get(int j) { return get(new Vec3(), j); }
+ public Vec3 get(Vec3 obj, int j) { return obj.__assign(__element(j), bb); }
+ }
+}
+
diff --git a/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGameMain.java b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGameMain.java
new file mode 100644
index 0000000000..b427bcca90
--- /dev/null
+++ b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGameMain.java
@@ -0,0 +1,61 @@
+package com.baeldung.flatbuffers;
+
+import static MyGame.terrains.Terrain.addColor;
+import static MyGame.terrains.Terrain.addEffects;
+import static MyGame.terrains.Terrain.addNavigation;
+import static MyGame.terrains.Terrain.endTerrain;
+import static MyGame.terrains.Terrain.startTerrain;
+
+import java.nio.ByteBuffer;
+
+import com.google.flatbuffers.FlatBufferBuilder;
+
+import MyGame.terrains.Effect;
+import MyGame.terrains.Terrain;
+
+public class MyGameMain {
+
+ public byte[] serialiseDesertTerrainData() {
+ int INITIAL_BUFFER = 1024;
+ FlatBufferBuilder builder = new FlatBufferBuilder(INITIAL_BUFFER);
+
+ int sandstormOffset = builder.createString("Sandstorm");
+ short damage = 3;
+ int sandStorm = MyGame.terrains.Effect.createEffect(builder, sandstormOffset, damage);
+
+ int droughtOffset = builder.createString("Drought");
+ short damageDrought = 1;
+ int drought = MyGame.terrains.Effect.createEffect(builder, droughtOffset, damageDrought);
+ int[] effects = new int[2];
+ effects[0] = sandStorm;
+ effects[1] = drought;
+
+ byte color = MyGame.terrains.Color.Brown;
+ int terrainName = builder.createString("Desert");
+ int navigationName = builder.createString("south");
+
+ int effectOffset = MyGame.terrains.Terrain.createEffectsVector(builder, effects);
+
+ startTerrain(builder);
+ MyGame.terrains.Terrain.addName(builder, terrainName);
+ addColor(builder, color);
+ addNavigation(builder, navigationName);
+ addEffects(builder, effectOffset);
+ int desert = endTerrain(builder);
+ builder.finish(desert);
+
+ return builder.sizedByteArray();
+ }
+
+ public MyGame.terrains.Terrain deserialiseDataToTerrain(byte[] buffer) {
+ ByteBuffer buf = ByteBuffer.wrap(buffer);
+ return Terrain.getRootAsTerrain(buf);
+ }
+
+ public Effect.Vector deserialiseDataToEffect(byte[] buffer) {
+ ByteBuffer buf = ByteBuffer.wrap(buffer);
+
+ return Terrain.getRootAsTerrain(buf)
+ .effectsVector();
+ }
+}
diff --git a/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/terrain.fbs b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/terrain.fbs
new file mode 100644
index 0000000000..5309af2717
--- /dev/null
+++ b/libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/terrain.fbs
@@ -0,0 +1,25 @@
+
+namespace MyGame.terrains;
+
+enum Color:byte { Brown = 0, Red = 1, Green = 2, Blue = 3, White = 4 }
+
+struct Vec3 {
+ x:float;
+ y:float;
+ z:float;
+}
+
+table Terrain {
+ pos:Vec3; // Struct.
+ name: string;
+ color:Color = Blue;
+ navigation: string;
+ effects: [Effect];
+}
+
+table Effect {
+ name: string;
+ damage: short;
+}
+
+root_type Terrain;
\ No newline at end of file
diff --git a/libraries-data-io-2/src/test/java/flatbuffers/FlatBufferGameUnitTest.java b/libraries-data-io-2/src/test/java/flatbuffers/FlatBufferGameUnitTest.java
new file mode 100644
index 0000000000..c2f7926fe8
--- /dev/null
+++ b/libraries-data-io-2/src/test/java/flatbuffers/FlatBufferGameUnitTest.java
@@ -0,0 +1,43 @@
+package flatbuffers;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.baeldung.flatbuffers.MyGameMain;
+
+import MyGame.terrains.Effect;
+
+public class FlatBufferGameUnitTest {
+
+ @Test
+ public void givenTerrainEffect_whenSerialisedWithValues_returnBytes() {
+ MyGameMain myGame = new MyGameMain();
+ byte[] bytes = myGame.serialiseDesertTerrainData();
+ Assert.assertNotNull(bytes);
+ }
+
+ @Test
+ public void givenSerialisedBytes_whenDeSerialised_returnsTerrain() {
+ MyGameMain myGame = new MyGameMain();
+ byte[] bytes = myGame.serialiseDesertTerrainData();
+
+ MyGame.terrains.Terrain terrain = myGame.deserialiseDataToTerrain(bytes);
+ Assert.assertNotNull(terrain);
+ Assert.assertEquals(terrain.name(), "Desert");
+ Assert.assertEquals(terrain.navigation(), "south");
+ Assert.assertEquals(MyGame.terrains.Color.name(terrain.color()), "Brown");
+ }
+
+ @Test
+ public void givenSerialisedBytes_whenDeSerialised_returnsTerrainEffect() {
+ MyGameMain myGame = new MyGameMain();
+ byte[] bytes = myGame.serialiseDesertTerrainData();
+
+ Effect.Vector effectsVector = myGame.deserialiseDataToEffect(bytes);
+ Assert.assertNotNull(effectsVector);
+ Assert.assertEquals(effectsVector.get(0).name(), "Sandstorm");
+ Assert.assertEquals(effectsVector.get(1).name(), "Drought");
+
+ Assert.assertEquals(effectsVector.get(1).damage(), 1);
+ }
+}
diff --git a/pom.xml b/pom.xml
index 548e4c6278..df3ea605aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,6 +8,9 @@
parent-modules
1.0.0-SNAPSHOT
parent-modules
+
+ libraries-data-io-2
+
pom