Skip to content

Commit d8f2881

Browse files
authored
[BAEL-5907] flatbuffers (#15969)
1 parent 344e89b commit d8f2881

9 files changed

Lines changed: 327 additions & 0 deletions

File tree

libraries-data-io-2/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>libraries-data-io-2</artifactId>
13+
<name>libraries-data-io-3</name>
14+
15+
<properties>
16+
<maven.compiler.source>19</maven.compiler.source>
17+
<maven.compiler.target>19</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<google-flatbuffers.version>23.5.26</google-flatbuffers.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.google.flatbuffers</groupId>
24+
<artifactId>flatbuffers-java</artifactId>
25+
<version>${google-flatbuffers.version}</version>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
package MyGame.terrains;
4+
5+
@SuppressWarnings("unused")
6+
public final class Color {
7+
private Color() { }
8+
public static final byte Brown = 0;
9+
public static final byte Red = 1;
10+
public static final byte Green = 2;
11+
public static final byte Blue = 3;
12+
public static final byte White = 4;
13+
14+
public static final String[] names = { "Brown", "Red", "Green", "Blue", "White", };
15+
16+
public static String name(int e) { return names[e]; }
17+
}
18+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
package MyGame.terrains;
4+
5+
import java.nio.ByteBuffer;
6+
import java.nio.ByteOrder;
7+
8+
import com.google.flatbuffers.BaseVector;
9+
import com.google.flatbuffers.Constants;
10+
import com.google.flatbuffers.FlatBufferBuilder;
11+
import com.google.flatbuffers.Table;
12+
13+
@SuppressWarnings("unused")
14+
public final class Effect extends Table {
15+
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
16+
public static Effect getRootAsEffect(ByteBuffer _bb) { return getRootAsEffect(_bb, new Effect()); }
17+
public static Effect getRootAsEffect(ByteBuffer _bb, Effect obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
18+
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
19+
public Effect __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
20+
21+
public String name() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
22+
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(4, 1); }
23+
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 4, 1); }
24+
public short damage() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 0; }
25+
public boolean mutateDamage(short damage) { int o = __offset(6); if (o != 0) { bb.putShort(o + bb_pos, damage); return true; } else { return false; } }
26+
27+
public static int createEffect(FlatBufferBuilder builder,
28+
int nameOffset,
29+
short damage) {
30+
builder.startTable(2);
31+
Effect.addName(builder, nameOffset);
32+
Effect.addDamage(builder, damage);
33+
return Effect.endEffect(builder);
34+
}
35+
36+
public static void startEffect(FlatBufferBuilder builder) { builder.startTable(2); }
37+
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(0, nameOffset, 0); }
38+
public static void addDamage(FlatBufferBuilder builder, short damage) { builder.addShort(1, damage, 0); }
39+
public static int endEffect(FlatBufferBuilder builder) {
40+
int o = builder.endTable();
41+
return o;
42+
}
43+
44+
public static final class Vector extends BaseVector {
45+
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
46+
47+
public Effect get(int j) { return get(new Effect(), j); }
48+
public Effect get(Effect obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
49+
}
50+
}
51+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
package MyGame.terrains;
4+
5+
import java.nio.ByteBuffer;
6+
import java.nio.ByteOrder;
7+
8+
import com.google.flatbuffers.BaseVector;
9+
import com.google.flatbuffers.Constants;
10+
import com.google.flatbuffers.FlatBufferBuilder;
11+
import com.google.flatbuffers.Table;
12+
13+
@SuppressWarnings("unused")
14+
public final class Terrain extends Table {
15+
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
16+
public static Terrain getRootAsTerrain(ByteBuffer _bb) { return getRootAsTerrain(_bb, new Terrain()); }
17+
public static Terrain getRootAsTerrain(ByteBuffer _bb, Terrain obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
18+
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
19+
public Terrain __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
20+
21+
public MyGame.terrains.Vec3 pos() { return pos(new MyGame.terrains.Vec3()); }
22+
public MyGame.terrains.Vec3 pos(MyGame.terrains.Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
23+
public String name() { int o = __offset(6); return o != 0 ? __string(o + bb_pos) : null; }
24+
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(6, 1); }
25+
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 6, 1); }
26+
public byte color() { int o = __offset(8); return o != 0 ? bb.get(o + bb_pos) : 3; }
27+
public boolean mutateColor(byte color) { int o = __offset(8); if (o != 0) { bb.put(o + bb_pos, color); return true; } else { return false; } }
28+
public String navigation() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
29+
public ByteBuffer navigationAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
30+
public ByteBuffer navigationInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); }
31+
public MyGame.terrains.Effect effects(int j) { return effects(new MyGame.terrains.Effect(), j); }
32+
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; }
33+
public int effectsLength() { int o = __offset(12); return o != 0 ? __vector_len(o) : 0; }
34+
public MyGame.terrains.Effect.Vector effectsVector() { return effectsVector(new MyGame.terrains.Effect.Vector()); }
35+
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; }
36+
37+
public static void startTerrain(FlatBufferBuilder builder) { builder.startTable(5); }
38+
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
39+
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(1, nameOffset, 0); }
40+
public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(2, color, 3); }
41+
public static void addNavigation(FlatBufferBuilder builder, int navigationOffset) { builder.addOffset(3, navigationOffset, 0); }
42+
public static void addEffects(FlatBufferBuilder builder, int effectsOffset) { builder.addOffset(4, effectsOffset, 0); }
43+
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(); }
44+
public static void startEffectsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
45+
public static int endTerrain(FlatBufferBuilder builder) {
46+
int o = builder.endTable();
47+
return o;
48+
}
49+
public static void finishTerrainBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset); }
50+
public static void finishSizePrefixedTerrainBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset); }
51+
52+
public static final class Vector extends BaseVector {
53+
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
54+
55+
public Terrain get(int j) { return get(new Terrain(), j); }
56+
public Terrain get(Terrain obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
57+
}
58+
}
59+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
package MyGame.terrains;
4+
5+
import java.nio.ByteBuffer;
6+
7+
import com.google.flatbuffers.BaseVector;
8+
import com.google.flatbuffers.FlatBufferBuilder;
9+
import com.google.flatbuffers.Struct;
10+
11+
@SuppressWarnings("unused")
12+
public final class Vec3 extends Struct {
13+
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
14+
public Vec3 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
15+
16+
public float x() { return bb.getFloat(bb_pos + 0); }
17+
public void mutateX(float x) { bb.putFloat(bb_pos + 0, x); }
18+
public float y() { return bb.getFloat(bb_pos + 4); }
19+
public void mutateY(float y) { bb.putFloat(bb_pos + 4, y); }
20+
public float z() { return bb.getFloat(bb_pos + 8); }
21+
public void mutateZ(float z) { bb.putFloat(bb_pos + 8, z); }
22+
23+
public static int createVec3(FlatBufferBuilder builder, float x, float y, float z) {
24+
builder.prep(4, 12);
25+
builder.putFloat(z);
26+
builder.putFloat(y);
27+
builder.putFloat(x);
28+
return builder.offset();
29+
}
30+
31+
public static final class Vector extends BaseVector {
32+
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
33+
34+
public Vec3 get(int j) { return get(new Vec3(), j); }
35+
public Vec3 get(Vec3 obj, int j) { return obj.__assign(__element(j), bb); }
36+
}
37+
}
38+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.flatbuffers;
2+
3+
import static MyGame.terrains.Terrain.addColor;
4+
import static MyGame.terrains.Terrain.addEffects;
5+
import static MyGame.terrains.Terrain.addNavigation;
6+
import static MyGame.terrains.Terrain.endTerrain;
7+
import static MyGame.terrains.Terrain.startTerrain;
8+
9+
import java.nio.ByteBuffer;
10+
11+
import com.google.flatbuffers.FlatBufferBuilder;
12+
13+
import MyGame.terrains.Effect;
14+
import MyGame.terrains.Terrain;
15+
16+
public class MyGameMain {
17+
18+
public byte[] serialiseDesertTerrainData() {
19+
int INITIAL_BUFFER = 1024;
20+
FlatBufferBuilder builder = new FlatBufferBuilder(INITIAL_BUFFER);
21+
22+
int sandstormOffset = builder.createString("Sandstorm");
23+
short damage = 3;
24+
int sandStorm = MyGame.terrains.Effect.createEffect(builder, sandstormOffset, damage);
25+
26+
int droughtOffset = builder.createString("Drought");
27+
short damageDrought = 1;
28+
int drought = MyGame.terrains.Effect.createEffect(builder, droughtOffset, damageDrought);
29+
int[] effects = new int[2];
30+
effects[0] = sandStorm;
31+
effects[1] = drought;
32+
33+
byte color = MyGame.terrains.Color.Brown;
34+
int terrainName = builder.createString("Desert");
35+
int navigationName = builder.createString("south");
36+
37+
int effectOffset = MyGame.terrains.Terrain.createEffectsVector(builder, effects);
38+
39+
startTerrain(builder);
40+
MyGame.terrains.Terrain.addName(builder, terrainName);
41+
addColor(builder, color);
42+
addNavigation(builder, navigationName);
43+
addEffects(builder, effectOffset);
44+
int desert = endTerrain(builder);
45+
builder.finish(desert);
46+
47+
return builder.sizedByteArray();
48+
}
49+
50+
public MyGame.terrains.Terrain deserialiseDataToTerrain(byte[] buffer) {
51+
ByteBuffer buf = ByteBuffer.wrap(buffer);
52+
return Terrain.getRootAsTerrain(buf);
53+
}
54+
55+
public Effect.Vector deserialiseDataToEffect(byte[] buffer) {
56+
ByteBuffer buf = ByteBuffer.wrap(buffer);
57+
58+
return Terrain.getRootAsTerrain(buf)
59+
.effectsVector();
60+
}
61+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
namespace MyGame.terrains;
3+
4+
enum Color:byte { Brown = 0, Red = 1, Green = 2, Blue = 3, White = 4 }
5+
6+
struct Vec3 {
7+
x:float;
8+
y:float;
9+
z:float;
10+
}
11+
12+
table Terrain {
13+
pos:Vec3; // Struct.
14+
name: string;
15+
color:Color = Blue;
16+
navigation: string;
17+
effects: [Effect];
18+
}
19+
20+
table Effect {
21+
name: string;
22+
damage: short;
23+
}
24+
25+
root_type Terrain;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package flatbuffers;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.baeldung.flatbuffers.MyGameMain;
7+
8+
import MyGame.terrains.Effect;
9+
10+
public class FlatBufferGameUnitTest {
11+
12+
@Test
13+
public void givenTerrainEffect_whenSerialisedWithValues_returnBytes() {
14+
MyGameMain myGame = new MyGameMain();
15+
byte[] bytes = myGame.serialiseDesertTerrainData();
16+
Assert.assertNotNull(bytes);
17+
}
18+
19+
@Test
20+
public void givenSerialisedBytes_whenDeSerialised_returnsTerrain() {
21+
MyGameMain myGame = new MyGameMain();
22+
byte[] bytes = myGame.serialiseDesertTerrainData();
23+
24+
MyGame.terrains.Terrain terrain = myGame.deserialiseDataToTerrain(bytes);
25+
Assert.assertNotNull(terrain);
26+
Assert.assertEquals(terrain.name(), "Desert");
27+
Assert.assertEquals(terrain.navigation(), "south");
28+
Assert.assertEquals(MyGame.terrains.Color.name(terrain.color()), "Brown");
29+
}
30+
31+
@Test
32+
public void givenSerialisedBytes_whenDeSerialised_returnsTerrainEffect() {
33+
MyGameMain myGame = new MyGameMain();
34+
byte[] bytes = myGame.serialiseDesertTerrainData();
35+
36+
Effect.Vector effectsVector = myGame.deserialiseDataToEffect(bytes);
37+
Assert.assertNotNull(effectsVector);
38+
Assert.assertEquals(effectsVector.get(0).name(), "Sandstorm");
39+
Assert.assertEquals(effectsVector.get(1).name(), "Drought");
40+
41+
Assert.assertEquals(effectsVector.get(1).damage(), 1);
42+
}
43+
}

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<artifactId>parent-modules</artifactId>
99
<version>1.0.0-SNAPSHOT</version>
1010
<name>parent-modules</name>
11+
<modules>
12+
<module>libraries-data-io-2</module>
13+
</modules>
1114

1215
<packaging>pom</packaging>
1316

0 commit comments

Comments
 (0)