mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-17 01:26:45 +00:00
Added accessor in Java to get vectors as ByteBuffers.
Also cleaned up ByteBuffer usage in general: ByteBuffer.position now universally indicates the start of a ByteBuffer. Change-Id: Ic4bfb98f9df9501b8fc82de2c45db7d7311135ac Tested: on Linux.
This commit is contained in:
@@ -41,7 +41,7 @@ class JavaTest {
|
||||
// Now test it:
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
TestBuffer(bb, 0);
|
||||
TestBuffer(bb);
|
||||
|
||||
// Second, let's create a FlatBuffer from scratch in Java, and test it also.
|
||||
// We use an initial size of 1 to exercise the reallocation algorithm,
|
||||
@@ -95,7 +95,7 @@ class JavaTest {
|
||||
try {
|
||||
DataOutputStream os = new DataOutputStream(new FileOutputStream(
|
||||
"monsterdata_java_wire.bin"));
|
||||
os.write(fbb.dataBuffer().array(), fbb.dataStart(), fbb.offset());
|
||||
os.write(fbb.dataBuffer().array(), fbb.dataBuffer().position(), fbb.offset());
|
||||
os.close();
|
||||
} catch(java.io.IOException e) {
|
||||
System.out.println("FlatBuffers test: couldn't write file");
|
||||
@@ -103,20 +103,20 @@ class JavaTest {
|
||||
}
|
||||
|
||||
// Test it:
|
||||
TestBuffer(fbb.dataBuffer(), fbb.dataStart());
|
||||
TestBuffer(fbb.dataBuffer());
|
||||
|
||||
// Make sure it also works with read only ByteBuffers. This is slower,
|
||||
// since creating strings incurs an additional copy
|
||||
// (see Table.__string).
|
||||
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer(), fbb.dataStart());
|
||||
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer());
|
||||
|
||||
System.out.println("FlatBuffers test: completed successfully");
|
||||
}
|
||||
|
||||
static void TestBuffer(ByteBuffer bb, int start) {
|
||||
TestEq(Monster.MonsterBufferHasIdentifier(bb, start), true);
|
||||
static void TestBuffer(ByteBuffer bb) {
|
||||
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
|
||||
|
||||
Monster monster = Monster.getRootAsMonster(bb, start);
|
||||
Monster monster = Monster.getRootAsMonster(bb);
|
||||
|
||||
TestEq(monster.hp(), (short)80);
|
||||
TestEq(monster.mana(), (short)150); // default
|
||||
@@ -145,6 +145,13 @@ class JavaTest {
|
||||
invsum += monster.inventory(i);
|
||||
TestEq(invsum, 10);
|
||||
|
||||
// Alternative way of accessing a vector:
|
||||
ByteBuffer ibb = monster.inventoryAsByteBuffer();
|
||||
invsum = 0;
|
||||
while (ibb.position() < ibb.limit())
|
||||
invsum += ibb.get();
|
||||
TestEq(invsum, 10);
|
||||
|
||||
Test test_0 = monster.test4(0);
|
||||
Test test_1 = monster.test4(1);
|
||||
TestEq(monster.test4Length(), 2);
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.util.*;
|
||||
import flatbuffers.*;
|
||||
|
||||
public class Monster extends Table {
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb, int offset) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Monster()).__init(_bb.getInt(offset) + offset, _bb); }
|
||||
public static boolean MonsterBufferHasIdentifier(ByteBuffer _bb, int offset) { return __has_identifier(_bb, offset, "MONS"); }
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Monster()).__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); }
|
||||
public static boolean MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); }
|
||||
public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
|
||||
public Vec3 pos() { return pos(new Vec3()); }
|
||||
@@ -17,24 +17,30 @@ public class Monster extends Table {
|
||||
public short mana() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 150; }
|
||||
public short hp() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) : 100; }
|
||||
public String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
|
||||
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
|
||||
public byte inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer inventoryAsByteBuffer() { return __vector_as_bytebuffer(14, 1); }
|
||||
public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 8; }
|
||||
public byte testType() { int o = __offset(18); return o != 0 ? bb.get(o + bb_pos) : 0; }
|
||||
public Table test(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o) : null; }
|
||||
public Test test4(int j) { return test4(new Test(), j); }
|
||||
public Test test4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; }
|
||||
public int test4Length() { int o = __offset(22); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer test4AsByteBuffer() { return __vector_as_bytebuffer(22, 4); }
|
||||
public String testarrayofstring(int j) { int o = __offset(24); return o != 0 ? __string(__vector(o) + j * 4) : null; }
|
||||
public int testarrayofstringLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testarrayofstringAsByteBuffer() { return __vector_as_bytebuffer(24, 4); }
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
public Monster testarrayoftables(int j) { return testarrayoftables(new Monster(), j); }
|
||||
public Monster testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__init(__indirect(__vector(o) + j * 4), bb) : null; }
|
||||
public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testarrayoftablesAsByteBuffer() { return __vector_as_bytebuffer(26, 4); }
|
||||
public Monster enemy() { return enemy(new Monster()); }
|
||||
public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public byte testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); }
|
||||
public Monster testempty() { return testempty(new Monster()); }
|
||||
public Monster testempty(Monster obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user