From e14bc1d9ac8091426235f9db9d7acb645f043d48 Mon Sep 17 00:00:00 2001 From: bml13 Date: Tue, 9 Sep 2014 10:30:09 +0100 Subject: [PATCH] Removed the use of b.array() to support DirectBuffers. Also removed Table extend Constants. Change-Id: I1770b613c58094fa572a3b26a31f01bd5fb8fdbf --- java/flatbuffers/FlatBufferBuilder.java | 39 ++++++++++++++----------- java/flatbuffers/Table.java | 4 +-- tests/JavaTest.java | 5 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/java/flatbuffers/FlatBufferBuilder.java b/java/flatbuffers/FlatBufferBuilder.java index d55a24218..4623661b5 100755 --- a/java/flatbuffers/FlatBufferBuilder.java +++ b/java/flatbuffers/FlatBufferBuilder.java @@ -16,7 +16,7 @@ package flatbuffers; -import java.lang.String; +import static flatbuffers.Constants.*; import java.util.Arrays; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -25,7 +25,7 @@ import java.nio.charset.Charset; // Class that helps you build a FlatBuffer. // See the section "Use in Java" in the main FlatBuffers documentation. -public class FlatBufferBuilder extends Constants { +public class FlatBufferBuilder { ByteBuffer bb; // Where we construct the FlatBuffer. int space; // Remaining space in the ByteBuffer. static final Charset utf8charset = Charset.forName("UTF-8"); @@ -40,37 +40,34 @@ public class FlatBufferBuilder extends Constants { public FlatBufferBuilder(int initial_size) { if (initial_size <= 0) initial_size = 1; space = initial_size; - bb = newByteBuffer(new byte[initial_size]); + bb = newByteBuffer(initial_size); } // 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); space = bb.capacity(); } - ByteBuffer newByteBuffer(byte[] buf) { - ByteBuffer newbb = ByteBuffer.wrap(buf); + static ByteBuffer newByteBuffer(int capacity) { + ByteBuffer newbb = ByteBuffer.allocate(capacity); newbb.order(ByteOrder.LITTLE_ENDIAN); return newbb; } // Doubles the size of the ByteBuffer, and copies the old data towards the // end of the new buffer (since we build the buffer backwards). - ByteBuffer growByteBuffer(ByteBuffer bb) { - byte[] old_buf = bb.array(); - int old_buf_size = old_buf.length; + static ByteBuffer growByteBuffer(ByteBuffer bb) { + int old_buf_size = bb.capacity(); if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int. throw new AssertionError("FlatBuffers: cannot grow buffer beyond 2 gigabytes."); int new_buf_size = old_buf_size << 1; - byte[] new_buf = new byte[new_buf_size]; - System.arraycopy(old_buf, 0, new_buf, new_buf_size - old_buf_size, old_buf_size); - ByteBuffer nbb = newByteBuffer(new_buf); - nbb.position(bb.position()); + bb.position(0); + ByteBuffer nbb = newByteBuffer(new_buf_size); + nbb.position(new_buf_size - old_buf_size); + nbb.put(bb); return nbb; } @@ -145,7 +142,8 @@ public class FlatBufferBuilder extends Constants { byte[] utf8 = s.getBytes(utf8charset); addByte((byte)0); startVector(1, utf8.length, 1); - System.arraycopy(utf8, 0, bb.array(), space -= utf8.length, utf8.length); + bb.position(space -= utf8.length); + bb.put(utf8, 0, utf8.length); return endVector(); } @@ -267,8 +265,15 @@ public class FlatBufferBuilder extends Constants { return space; } + public byte[] sizedByteArray(int start, int length){ + byte[] array = new byte[length]; + bb.position(start); + bb.get(array); + return array; + } + // Utility function for copying a byte array that starts at 0. - public byte[] sizedByteArray() { - return Arrays.copyOfRange(bb.array(), dataStart(), bb.capacity()); + public byte[] sizedByteArray(){ + return sizedByteArray(space, bb.capacity() - space); } } diff --git a/java/flatbuffers/Table.java b/java/flatbuffers/Table.java index 7740e49fd..4daafff70 100755 --- a/java/flatbuffers/Table.java +++ b/java/flatbuffers/Table.java @@ -16,12 +16,12 @@ package flatbuffers; -import java.lang.String; +import static flatbuffers.Constants.*; import java.nio.ByteBuffer; import java.nio.charset.Charset; // All tables in the generated code derive from this class, and add their own accessors. -public class Table extends Constants { +public class Table { protected int bb_pos; protected ByteBuffer bb; diff --git a/tests/JavaTest.java b/tests/JavaTest.java index 4747c44f3..04646c587 100755 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -105,8 +105,9 @@ class JavaTest { // Test it: TestBuffer(fbb.dataBuffer(), fbb.dataStart()); - // Make sure it also works with read only ByteBuffers. This is slower, since - // creating strings incurs an additional copy (see Table.__string). + // Make sure it also works with read only ByteBuffers. This is slower, + // since creating strings incurs an additional copy + // (see Table.__string). TestBuffer(fbb.dataBuffer().asReadOnlyBuffer(), fbb.dataStart()); System.out.println("FlatBuffers test: completed successfully");