From: Wouter van Oortmerssen Date: Fri, 5 Sep 2014 20:48:02 +0000 (-0700) Subject: Made FlatBufferBuilder.java require ByteBuffers that have an array. X-Git-Tag: v1.0.3~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=354aad4bc541c6a1346e8aac33fd3cdc9d951edb;p=platform%2Fupstream%2Fflatbuffers.git 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. --- diff --git a/java/flatbuffers/FlatBufferBuilder.java b/java/flatbuffers/FlatBufferBuilder.java index a1bc974..d55a242 100755 --- a/java/flatbuffers/FlatBufferBuilder.java +++ b/java/flatbuffers/FlatBufferBuilder.java @@ -45,6 +45,8 @@ public class FlatBufferBuilder extends Constants { // 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); @@ -74,7 +76,7 @@ public class FlatBufferBuilder extends Constants { // 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) { @@ -91,12 +93,12 @@ public class FlatBufferBuilder extends Constants { 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); } @@ -209,7 +211,7 @@ public class FlatBufferBuilder extends Constants { 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)) { @@ -226,7 +228,7 @@ public class FlatBufferBuilder extends Constants { 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 { @@ -235,7 +237,7 @@ public class FlatBufferBuilder extends Constants { 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; @@ -267,6 +269,6 @@ public class FlatBufferBuilder extends Constants { // 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()); } }