2014-01-28 00:52:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import MyGame.Example.*;
|
2014-09-16 00:45:15 +00:00
|
|
|
import com.google.flatbuffers.FlatBufferBuilder;
|
2014-01-28 00:52:49 +00:00
|
|
|
|
|
|
|
class JavaTest {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
// First, let's test reading a FlatBuffer generated by C++ code:
|
|
|
|
// This file was generated from monsterdata_test.json
|
|
|
|
|
|
|
|
byte[] data = null;
|
2014-09-19 23:51:36 +00:00
|
|
|
File file = new File("monsterdata_test.mon");
|
2014-01-28 00:52:49 +00:00
|
|
|
RandomAccessFile f = null;
|
|
|
|
try {
|
|
|
|
f = new RandomAccessFile(file, "r");
|
|
|
|
data = new byte[(int)f.length()];
|
|
|
|
f.readFully(data);
|
|
|
|
f.close();
|
|
|
|
} catch(java.io.IOException e) {
|
|
|
|
System.out.println("FlatBuffers test: couldn't read file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test it:
|
|
|
|
|
|
|
|
ByteBuffer bb = ByteBuffer.wrap(data);
|
2014-09-10 23:38:37 +00:00
|
|
|
TestBuffer(bb);
|
2014-01-28 00:52:49 +00:00
|
|
|
|
|
|
|
// Second, let's create a FlatBuffer from scratch in Java, and test it also.
|
2014-07-02 00:56:12 +00:00
|
|
|
// We use an initial size of 1 to exercise the reallocation algorithm,
|
|
|
|
// normally a size larger than the typical FlatBuffer you generate would be
|
|
|
|
// better for performance.
|
|
|
|
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
|
2014-01-28 00:52:49 +00:00
|
|
|
|
2014-07-02 00:56:12 +00:00
|
|
|
// We set up the same values as monsterdata.json:
|
2014-01-28 00:52:49 +00:00
|
|
|
|
|
|
|
int str = fbb.createString("MyMonster");
|
|
|
|
|
2014-09-11 21:36:33 +00:00
|
|
|
int inv = Monster.createInventoryVector(fbb, new byte[] { 0, 1, 2, 3, 4 });
|
2014-01-28 00:52:49 +00:00
|
|
|
|
2014-09-19 23:51:36 +00:00
|
|
|
int fred = fbb.createString("Fred");
|
2014-01-28 00:52:49 +00:00
|
|
|
Monster.startMonster(fbb);
|
2014-09-19 23:51:36 +00:00
|
|
|
Monster.addName(fbb, fred);
|
2014-01-28 00:52:49 +00:00
|
|
|
int mon2 = Monster.endMonster(fbb);
|
|
|
|
|
|
|
|
Monster.startTest4Vector(fbb, 2);
|
|
|
|
Test.createTest(fbb, (short)10, (byte)20);
|
|
|
|
Test.createTest(fbb, (short)30, (byte)40);
|
|
|
|
int test4 = fbb.endVector();
|
|
|
|
|
2014-09-11 21:36:33 +00:00
|
|
|
int testArrayOfString = Monster.createTestarrayofstringVector(fbb, new int[] {
|
|
|
|
fbb.createString("test1"),
|
|
|
|
fbb.createString("test2")
|
|
|
|
});
|
2014-08-09 02:59:28 +00:00
|
|
|
|
2014-01-28 00:52:49 +00:00
|
|
|
Monster.startMonster(fbb);
|
|
|
|
Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
|
2014-09-23 18:55:42 +00:00
|
|
|
Color.Green, (short)5, (byte)6));
|
2014-01-28 00:52:49 +00:00
|
|
|
Monster.addHp(fbb, (short)80);
|
|
|
|
Monster.addName(fbb, str);
|
|
|
|
Monster.addInventory(fbb, inv);
|
2014-09-04 19:13:18 +00:00
|
|
|
Monster.addTestType(fbb, (byte)Any.Monster);
|
2014-01-28 00:52:49 +00:00
|
|
|
Monster.addTest(fbb, mon2);
|
|
|
|
Monster.addTest4(fbb, test4);
|
2014-08-09 02:59:28 +00:00
|
|
|
Monster.addTestarrayofstring(fbb, testArrayOfString);
|
2015-01-17 01:48:51 +00:00
|
|
|
Monster.addTestbool(fbb, false);
|
2014-01-28 00:52:49 +00:00
|
|
|
int mon = Monster.endMonster(fbb);
|
|
|
|
|
2014-09-04 23:31:44 +00:00
|
|
|
Monster.finishMonsterBuffer(fbb, mon);
|
2014-01-28 00:52:49 +00:00
|
|
|
|
|
|
|
// Write the result to a file for debugging purposes:
|
|
|
|
// Note that the binaries are not necessarily identical, since the JSON
|
|
|
|
// parser may serialize in a slightly different order than the above
|
|
|
|
// Java code. They are functionally equivalent though.
|
|
|
|
|
|
|
|
try {
|
2014-09-11 21:36:33 +00:00
|
|
|
DataOutputStream os = new DataOutputStream(new FileOutputStream(
|
2014-09-19 23:51:36 +00:00
|
|
|
"monsterdata_java_wire.mon"));
|
2014-09-11 21:36:33 +00:00
|
|
|
os.write(fbb.dataBuffer().array(), fbb.dataBuffer().position(), fbb.offset());
|
|
|
|
os.close();
|
2014-01-28 00:52:49 +00:00
|
|
|
} catch(java.io.IOException e) {
|
|
|
|
System.out.println("FlatBuffers test: couldn't write file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it:
|
2014-09-10 23:38:37 +00:00
|
|
|
TestBuffer(fbb.dataBuffer());
|
2014-01-28 00:52:49 +00:00
|
|
|
|
2014-09-09 09:30:09 +00:00
|
|
|
// Make sure it also works with read only ByteBuffers. This is slower,
|
|
|
|
// since creating strings incurs an additional copy
|
|
|
|
// (see Table.__string).
|
2014-09-10 23:38:37 +00:00
|
|
|
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer());
|
2014-09-03 01:35:20 +00:00
|
|
|
|
2014-09-18 19:28:10 +00:00
|
|
|
TestEnums();
|
|
|
|
|
2014-01-28 00:52:49 +00:00
|
|
|
System.out.println("FlatBuffers test: completed successfully");
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:28:10 +00:00
|
|
|
static void TestEnums() {
|
|
|
|
TestEq(Color.name(Color.Red), "Red");
|
|
|
|
TestEq(Color.name(Color.Blue), "Blue");
|
|
|
|
TestEq(Any.name(Any.NONE), "NONE");
|
|
|
|
TestEq(Any.name(Any.Monster), "Monster");
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:38:37 +00:00
|
|
|
static void TestBuffer(ByteBuffer bb) {
|
|
|
|
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
|
2014-09-04 23:31:44 +00:00
|
|
|
|
2014-09-10 23:38:37 +00:00
|
|
|
Monster monster = Monster.getRootAsMonster(bb);
|
2014-01-28 00:52:49 +00:00
|
|
|
|
|
|
|
TestEq(monster.hp(), (short)80);
|
|
|
|
TestEq(monster.mana(), (short)150); // default
|
|
|
|
|
|
|
|
TestEq(monster.name(), "MyMonster");
|
|
|
|
// monster.friendly() // can't access, deprecated
|
|
|
|
|
|
|
|
Vec3 pos = monster.pos();
|
|
|
|
TestEq(pos.x(), 1.0f);
|
|
|
|
TestEq(pos.y(), 2.0f);
|
|
|
|
TestEq(pos.z(), 3.0f);
|
|
|
|
TestEq(pos.test1(), 3.0);
|
2014-09-23 18:55:42 +00:00
|
|
|
TestEq(pos.test2(), Color.Green);
|
2014-01-28 00:52:49 +00:00
|
|
|
Test t = pos.test3();
|
|
|
|
TestEq(t.a(), (short)5);
|
|
|
|
TestEq(t.b(), (byte)6);
|
|
|
|
|
|
|
|
TestEq(monster.testType(), (byte)Any.Monster);
|
|
|
|
Monster monster2 = new Monster();
|
|
|
|
TestEq(monster.test(monster2) != null, true);
|
2014-09-19 23:51:36 +00:00
|
|
|
TestEq(monster2.name(), "Fred");
|
2014-01-28 00:52:49 +00:00
|
|
|
|
|
|
|
TestEq(monster.inventoryLength(), 5);
|
|
|
|
int invsum = 0;
|
|
|
|
for (int i = 0; i < monster.inventoryLength(); i++)
|
|
|
|
invsum += monster.inventory(i);
|
|
|
|
TestEq(invsum, 10);
|
|
|
|
|
2014-09-10 23:38:37 +00:00
|
|
|
// Alternative way of accessing a vector:
|
|
|
|
ByteBuffer ibb = monster.inventoryAsByteBuffer();
|
|
|
|
invsum = 0;
|
|
|
|
while (ibb.position() < ibb.limit())
|
|
|
|
invsum += ibb.get();
|
|
|
|
TestEq(invsum, 10);
|
|
|
|
|
2014-01-28 00:52:49 +00:00
|
|
|
Test test_0 = monster.test4(0);
|
|
|
|
Test test_1 = monster.test4(1);
|
|
|
|
TestEq(monster.test4Length(), 2);
|
|
|
|
TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100);
|
2014-08-09 02:59:28 +00:00
|
|
|
|
|
|
|
TestEq(monster.testarrayofstringLength(), 2);
|
|
|
|
TestEq(monster.testarrayofstring(0),"test1");
|
|
|
|
TestEq(monster.testarrayofstring(1),"test2");
|
2015-01-17 01:48:51 +00:00
|
|
|
|
|
|
|
TestEq(monster.testbool(), false);
|
2014-01-28 00:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static <T> void TestEq(T a, T b) {
|
|
|
|
if (!a.equals(b)) {
|
|
|
|
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());
|
|
|
|
System.out.println("FlatBuffers test FAILED: \'" + a + "\' != \'" + b + "\'");
|
|
|
|
assert false;
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|