mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 01:38:06 +00:00
Made FlatBufferBuilder.java require ByteBuffers that have an array.
Readonly ByteBuffers and Direct ByteBuffers don't have a backing array, and thus can't be used for writing FlatBuffers (though they are fine for reading). Change-Id: I4d7b9cc222b96161d0f8e92f34588bd3e0e38034 Tested: on Linux.
This commit is contained in:
@@ -45,6 +45,8 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
|
|
||||||
// Alternative constructor allowing reuse of ByteBuffers
|
// Alternative constructor allowing reuse of ByteBuffers
|
||||||
public FlatBufferBuilder(ByteBuffer existing_bb) {
|
public FlatBufferBuilder(ByteBuffer existing_bb) {
|
||||||
|
if (!existing_bb.hasArray())
|
||||||
|
throw new AssertionError("FlatBuffers: ByteBuffer must have backing array.");
|
||||||
bb = existing_bb;
|
bb = existing_bb;
|
||||||
bb.clear();
|
bb.clear();
|
||||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
@@ -74,7 +76,7 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
|
|
||||||
// Offset relative to the end of the buffer.
|
// Offset relative to the end of the buffer.
|
||||||
public int offset() {
|
public int offset() {
|
||||||
return bb.array().length - space;
|
return bb.capacity() - space;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pad(int byte_size) {
|
public void pad(int byte_size) {
|
||||||
@@ -91,12 +93,12 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
if (size > minalign) minalign = size;
|
if (size > minalign) minalign = size;
|
||||||
// Find the amount of alignment needed such that `size` is properly
|
// Find the amount of alignment needed such that `size` is properly
|
||||||
// aligned after `additional_bytes`
|
// 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.
|
// Reallocate the buffer if needed.
|
||||||
while (space < align_size + size + additional_bytes) {
|
while (space < align_size + size + additional_bytes) {
|
||||||
int old_buf_size = bb.array().length;
|
int old_buf_size = bb.capacity();
|
||||||
bb = growByteBuffer(bb);
|
bb = growByteBuffer(bb);
|
||||||
space += bb.array().length - old_buf_size;
|
space += bb.capacity() - old_buf_size;
|
||||||
}
|
}
|
||||||
pad(align_size);
|
pad(align_size);
|
||||||
}
|
}
|
||||||
@@ -209,7 +211,7 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
int existing_vtable = 0;
|
int existing_vtable = 0;
|
||||||
outer_loop:
|
outer_loop:
|
||||||
for (int i = 0; i < num_vtables; i++) {
|
for (int i = 0; i < num_vtables; i++) {
|
||||||
int vt1 = bb.array().length - vtables[i];
|
int vt1 = bb.capacity() - vtables[i];
|
||||||
int vt2 = space;
|
int vt2 = space;
|
||||||
short len = bb.getShort(vt1);
|
short len = bb.getShort(vt1);
|
||||||
if (len == bb.getShort(vt2)) {
|
if (len == bb.getShort(vt2)) {
|
||||||
@@ -226,7 +228,7 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
if (existing_vtable != 0) {
|
if (existing_vtable != 0) {
|
||||||
// Found a match:
|
// Found a match:
|
||||||
// Remove the current vtable.
|
// Remove the current vtable.
|
||||||
space = bb.array().length - vtableloc;
|
space = bb.capacity() - vtableloc;
|
||||||
// Point table to existing vtable.
|
// Point table to existing vtable.
|
||||||
bb.putInt(space, existing_vtable - vtableloc);
|
bb.putInt(space, existing_vtable - vtableloc);
|
||||||
} else {
|
} else {
|
||||||
@@ -235,7 +237,7 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
if (num_vtables == vtables.length) vtables = Arrays.copyOf(vtables, num_vtables * 2);
|
if (num_vtables == vtables.length) vtables = Arrays.copyOf(vtables, num_vtables * 2);
|
||||||
vtables[num_vtables++] = offset();
|
vtables[num_vtables++] = offset();
|
||||||
// Point table to current vtable.
|
// Point table to current vtable.
|
||||||
bb.putInt(bb.array().length - vtableloc, offset() - vtableloc);
|
bb.putInt(bb.capacity() - vtableloc, offset() - vtableloc);
|
||||||
}
|
}
|
||||||
|
|
||||||
vtable = null;
|
vtable = null;
|
||||||
@@ -267,6 +269,6 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
|
|
||||||
// Utility function for copying a byte array that starts at 0.
|
// Utility function for copying a byte array that starts at 0.
|
||||||
public byte[] sizedByteArray() {
|
public byte[] sizedByteArray() {
|
||||||
return Arrays.copyOfRange(bb.array(), dataStart(), bb.array().length);
|
return Arrays.copyOfRange(bb.array(), dataStart(), bb.capacity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user