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