// Alternative constructor allowing reuse of ByteBuffers
public FlatBufferBuilder(ByteBuffer existing_bb) {
+ if (!existing_bb.hasArray())
+ throw new AssertionError("FlatBuffers: ByteBuffer must have backing array.");
bb = existing_bb;
bb.clear();
bb.order(ByteOrder.LITTLE_ENDIAN);
// Offset relative to the end of the buffer.
public int offset() {
- return bb.array().length - space;
+ return bb.capacity() - space;
}
public void pad(int byte_size) {
if (size > minalign) minalign = size;
// Find the amount of alignment needed such that `size` is properly
// aligned after `additional_bytes`
- int align_size = ((~(bb.array().length - space + additional_bytes)) + 1) & (size - 1);
+ int align_size = ((~(bb.capacity() - space + additional_bytes)) + 1) & (size - 1);
// Reallocate the buffer if needed.
while (space < align_size + size + additional_bytes) {
- int old_buf_size = bb.array().length;
+ int old_buf_size = bb.capacity();
bb = growByteBuffer(bb);
- space += bb.array().length - old_buf_size;
+ space += bb.capacity() - old_buf_size;
}
pad(align_size);
}
int existing_vtable = 0;
outer_loop:
for (int i = 0; i < num_vtables; i++) {
- int vt1 = bb.array().length - vtables[i];
+ int vt1 = bb.capacity() - vtables[i];
int vt2 = space;
short len = bb.getShort(vt1);
if (len == bb.getShort(vt2)) {
if (existing_vtable != 0) {
// Found a match:
// Remove the current vtable.
- space = bb.array().length - vtableloc;
+ space = bb.capacity() - vtableloc;
// Point table to existing vtable.
bb.putInt(space, existing_vtable - vtableloc);
} else {
if (num_vtables == vtables.length) vtables = Arrays.copyOf(vtables, num_vtables * 2);
vtables[num_vtables++] = offset();
// Point table to current vtable.
- bb.putInt(bb.array().length - vtableloc, offset() - vtableloc);
+ bb.putInt(bb.capacity() - vtableloc, offset() - vtableloc);
}
vtable = null;
// Utility function for copying a byte array that starts at 0.
public byte[] sizedByteArray() {
- return Arrays.copyOfRange(bb.array(), dataStart(), bb.array().length);
+ return Arrays.copyOfRange(bb.array(), dataStart(), bb.capacity());
}
}