diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index c2186fa7b7422be0f1882940ab927279998d4ef4..f69fbcf96b8251db121b13f4c229f9d1eb7c06e6 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -367,6 +367,28 @@ public class FlatBufferBuilder { } /// @endcond + /** + * Create a new array/vector and return a ByteBuffer to be filled later. + * Call {@link #endVector} after this method to get an offset to the beginning + * of vector. + * + * @param elem_size the size of each element in bytes. + * @param num_elems number of elements in the vector. + * @param alignment byte alignment. + * @return ByteBuffer with position and limit set to the space allocated for the array. + */ + public ByteBuffer createUnintializedVector(int elem_size, int num_elems, int alignment) { + int length = elem_size * num_elems; + startVector(elem_size, num_elems, alignment); + + bb.position(space -= length); + + // Slice and limit the copy vector to point to the 'array' + ByteBuffer copy = bb.slice().order(ByteOrder.LITTLE_ENDIAN); + copy.limit(length); + return copy; + } + /** * Encode the string `s` in the buffer using UTF-8. If {@code s} is * already a {@link CharBuffer}, this method is allocation free. @@ -413,6 +435,20 @@ public class FlatBufferBuilder { return endVector(); } + /** + * Create a byte array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createByteVector(byte[] arr) { + int length = arr.length; + startVector(1, length, 1); + bb.position(space -= length); + bb.put(arr); + return endVector(); + } + /// @cond FLATBUFFERS_INTERNAL /** * Should not be accessing the final buffer before it is finished. diff --git a/tests/JavaTest.java b/tests/JavaTest.java index 154fdec67b4a46525ad9ae77a7189ce40187b9a9..c0d7d03fb14932ceb2a5d0ba7272cff4f2b01d08 100755 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -161,6 +161,10 @@ class JavaTest { TestNestedFlatBuffer(); + TestCreateByteVector(); + + TestCreateUninitializedVector(); + System.out.println("FlatBuffers test: completed successfully"); } @@ -281,6 +285,44 @@ class JavaTest { TestEq(nestedMonsterName, nestedMonster.name()); } + static void TestCreateByteVector() { + FlatBufferBuilder fbb = new FlatBufferBuilder(16); + int str = fbb.createString("MyMonster"); + byte[] inventory = new byte[] { 0, 1, 2, 3, 4 }; + int vec = fbb.createByteVector(inventory); + Monster.startMonster(fbb); + Monster.addInventory(fbb, vec); + Monster.addName(fbb, str); + int monster1 = Monster.endMonster(fbb); + Monster.finishMonsterBuffer(fbb, monster1); + Monster monsterObject = Monster.getRootAsMonster(fbb.dataBuffer()); + + TestEq(monsterObject.inventory(1), (int)inventory[1]); + TestEq(monsterObject.inventoryLength(), inventory.length); + TestEq(ByteBuffer.wrap(inventory), monsterObject.inventoryAsByteBuffer()); + } + + static void TestCreateUninitializedVector() { + FlatBufferBuilder fbb = new FlatBufferBuilder(16); + int str = fbb.createString("MyMonster"); + byte[] inventory = new byte[] { 0, 1, 2, 3, 4 }; + ByteBuffer bb = fbb.createUnintializedVector(1, inventory.length, 1); + for (byte i:inventory) { + bb.put(i); + } + int vec = fbb.endVector(); + Monster.startMonster(fbb); + Monster.addInventory(fbb, vec); + Monster.addName(fbb, str); + int monster1 = Monster.endMonster(fbb); + Monster.finishMonsterBuffer(fbb, monster1); + Monster monsterObject = Monster.getRootAsMonster(fbb.dataBuffer()); + + TestEq(monsterObject.inventory(1), (int)inventory[1]); + TestEq(monsterObject.inventoryLength(), inventory.length); + TestEq(ByteBuffer.wrap(inventory), monsterObject.inventoryAsByteBuffer()); + } + static <T> void TestEq(T a, T b) { if (!a.equals(b)) { System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());