mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-25 10:38:40 +00:00
Merge pull request #3992 from akazakov/master
Add byte array and unintialized array creation to FlatBufferBuilder
This commit is contained in:
@@ -367,6 +367,28 @@ public class FlatBufferBuilder {
|
|||||||
}
|
}
|
||||||
/// @endcond
|
/// @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
|
* Encode the string `s` in the buffer using UTF-8. If {@code s} is
|
||||||
* already a {@link CharBuffer}, this method is allocation free.
|
* already a {@link CharBuffer}, this method is allocation free.
|
||||||
@@ -413,6 +435,20 @@ public class FlatBufferBuilder {
|
|||||||
return endVector();
|
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
|
/// @cond FLATBUFFERS_INTERNAL
|
||||||
/**
|
/**
|
||||||
* Should not be accessing the final buffer before it is finished.
|
* Should not be accessing the final buffer before it is finished.
|
||||||
|
|||||||
@@ -161,6 +161,10 @@ class JavaTest {
|
|||||||
|
|
||||||
TestNestedFlatBuffer();
|
TestNestedFlatBuffer();
|
||||||
|
|
||||||
|
TestCreateByteVector();
|
||||||
|
|
||||||
|
TestCreateUninitializedVector();
|
||||||
|
|
||||||
System.out.println("FlatBuffers test: completed successfully");
|
System.out.println("FlatBuffers test: completed successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,6 +285,44 @@ class JavaTest {
|
|||||||
TestEq(nestedMonsterName, nestedMonster.name());
|
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) {
|
static <T> void TestEq(T a, T b) {
|
||||||
if (!a.equals(b)) {
|
if (!a.equals(b)) {
|
||||||
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());
|
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());
|
||||||
|
|||||||
Reference in New Issue
Block a user