• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Run this file with the `java_sample.sh` script.
18 
19 import MyGame.Sample.Color;
20 import MyGame.Sample.Equipment;
21 import MyGame.Sample.Monster;
22 import MyGame.Sample.Vec3;
23 import MyGame.Sample.Weapon;
24 
25 import com.google.flatbuffers.FlatBufferBuilder;
26 
27 import java.nio.ByteBuffer;
28 
29 class SampleBinary {
30   // Example how to use FlatBuffers to create and read binary buffers.
main(String[] args)31   public static void main(String[] args) {
32     FlatBufferBuilder builder = new FlatBufferBuilder(0);
33 
34     // Create some weapons for our Monster ('Sword' and 'Axe').
35     int weaponOneName = builder.createString("Sword");
36     short weaponOneDamage = 3;
37     int weaponTwoName = builder.createString("Axe");
38     short weaponTwoDamage = 5;
39 
40     // Use the `createWeapon()` helper function to create the weapons, since we set every field.
41     int[] weaps = new int[2];
42     weaps[0] = Weapon.createWeapon(builder, weaponOneName, weaponOneDamage);
43     weaps[1] = Weapon.createWeapon(builder, weaponTwoName, weaponTwoDamage);
44 
45     // Serialize the FlatBuffer data.
46     int name = builder.createString("Orc");
47     byte[] treasure = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
48     int inv = Monster.createInventoryVector(builder, treasure);
49     int weapons = Monster.createWeaponsVector(builder, weaps);
50     int pos = Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f);
51 
52     Monster.startMonster(builder);
53     Monster.addPos(builder, pos);
54     Monster.addName(builder, name);
55     Monster.addColor(builder, Color.Red);
56     Monster.addHp(builder, (short)300);
57     Monster.addInventory(builder, inv);
58     Monster.addWeapons(builder, weapons);
59     Monster.addEquippedType(builder, Equipment.Weapon);
60     Monster.addEquipped(builder, weaps[1]);
61     int orc = Monster.endMonster(builder);
62 
63     builder.finish(orc); // You could also call `Monster.finishMonsterBuffer(builder, orc);`.
64 
65     // We now have a FlatBuffer that can be stored on disk or sent over a network.
66 
67     // ...Code to store to disk or send over a network goes here...
68 
69     // Instead, we are going to access it right away, as if we just received it.
70 
71     ByteBuffer buf = builder.dataBuffer();
72 
73     // Get access to the root:
74     Monster monster = Monster.getRootAsMonster(buf);
75 
76     // Note: We did not set the `mana` field explicitly, so we get back the default value.
77     assert monster.mana() == (short)150;
78     assert monster.hp() == (short)300;
79     assert monster.name().equals("Orc");
80     assert monster.color() == Color.Red;
81     assert monster.pos().x() == 1.0f;
82     assert monster.pos().y() == 2.0f;
83     assert monster.pos().z() == 3.0f;
84 
85     // Get and test the `inventory` FlatBuffer `vector`.
86     for (int i = 0; i < monster.inventoryLength(); i++) {
87       assert monster.inventory(i) == (byte)i;
88     }
89 
90     // Get and test the `weapons` FlatBuffer `vector` of `table`s.
91     String[] expectedWeaponNames = {"Sword", "Axe"};
92     int[] expectedWeaponDamages = {3, 5};
93     for (int i = 0; i < monster.weaponsLength(); i++) {
94       assert monster.weapons(i).name().equals(expectedWeaponNames[i]);
95       assert monster.weapons(i).damage() == expectedWeaponDamages[i];
96     }
97 
98     // Get and test the `equipped` FlatBuffer `union`.
99     assert monster.equippedType() == Equipment.Weapon;
100     Weapon equipped = (Weapon)monster.equipped(new Weapon());
101     assert equipped.name().equals("Axe");
102     assert equipped.damage() == 5;
103 
104     System.out.println("The FlatBuffer was successfully created and verified!");
105   }
106 }
107