mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 20:40:02 +00:00
Removed the use of b.array() to support DirectBuffers.
Also removed Table extend Constants. Change-Id: I1770b613c58094fa572a3b26a31f01bd5fb8fdbf
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
9a1f7be6fd
commit
e14bc1d9ac
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package flatbuffers;
|
package flatbuffers;
|
||||||
|
|
||||||
import java.lang.String;
|
import static flatbuffers.Constants.*;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
@@ -25,7 +25,7 @@ import java.nio.charset.Charset;
|
|||||||
// Class that helps you build a FlatBuffer.
|
// Class that helps you build a FlatBuffer.
|
||||||
// See the section "Use in Java" in the main FlatBuffers documentation.
|
// 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.
|
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||||
int space; // Remaining space in the ByteBuffer.
|
int space; // Remaining space in the ByteBuffer.
|
||||||
static final Charset utf8charset = Charset.forName("UTF-8");
|
static final Charset utf8charset = Charset.forName("UTF-8");
|
||||||
@@ -40,37 +40,34 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
public FlatBufferBuilder(int initial_size) {
|
public FlatBufferBuilder(int initial_size) {
|
||||||
if (initial_size <= 0) initial_size = 1;
|
if (initial_size <= 0) initial_size = 1;
|
||||||
space = initial_size;
|
space = initial_size;
|
||||||
bb = newByteBuffer(new byte[initial_size]);
|
bb = newByteBuffer(initial_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
space = bb.capacity();
|
space = bb.capacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer newByteBuffer(byte[] buf) {
|
static ByteBuffer newByteBuffer(int capacity) {
|
||||||
ByteBuffer newbb = ByteBuffer.wrap(buf);
|
ByteBuffer newbb = ByteBuffer.allocate(capacity);
|
||||||
newbb.order(ByteOrder.LITTLE_ENDIAN);
|
newbb.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
return newbb;
|
return newbb;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Doubles the size of the ByteBuffer, and copies the old data towards the
|
// Doubles the size of the ByteBuffer, and copies the old data towards the
|
||||||
// end of the new buffer (since we build the buffer backwards).
|
// end of the new buffer (since we build the buffer backwards).
|
||||||
ByteBuffer growByteBuffer(ByteBuffer bb) {
|
static ByteBuffer growByteBuffer(ByteBuffer bb) {
|
||||||
byte[] old_buf = bb.array();
|
int old_buf_size = bb.capacity();
|
||||||
int old_buf_size = old_buf.length;
|
|
||||||
if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int.
|
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.");
|
throw new AssertionError("FlatBuffers: cannot grow buffer beyond 2 gigabytes.");
|
||||||
int new_buf_size = old_buf_size << 1;
|
int new_buf_size = old_buf_size << 1;
|
||||||
byte[] new_buf = new byte[new_buf_size];
|
bb.position(0);
|
||||||
System.arraycopy(old_buf, 0, new_buf, new_buf_size - old_buf_size, old_buf_size);
|
ByteBuffer nbb = newByteBuffer(new_buf_size);
|
||||||
ByteBuffer nbb = newByteBuffer(new_buf);
|
nbb.position(new_buf_size - old_buf_size);
|
||||||
nbb.position(bb.position());
|
nbb.put(bb);
|
||||||
return nbb;
|
return nbb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +142,8 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
byte[] utf8 = s.getBytes(utf8charset);
|
byte[] utf8 = s.getBytes(utf8charset);
|
||||||
addByte((byte)0);
|
addByte((byte)0);
|
||||||
startVector(1, utf8.length, 1);
|
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();
|
return endVector();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,8 +265,15 @@ public class FlatBufferBuilder extends Constants {
|
|||||||
return space;
|
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.
|
// Utility function for copying a byte array that starts at 0.
|
||||||
public byte[] sizedByteArray() {
|
public byte[] sizedByteArray(){
|
||||||
return Arrays.copyOfRange(bb.array(), dataStart(), bb.capacity());
|
return sizedByteArray(space, bb.capacity() - space);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,12 +16,12 @@
|
|||||||
|
|
||||||
package flatbuffers;
|
package flatbuffers;
|
||||||
|
|
||||||
import java.lang.String;
|
import static flatbuffers.Constants.*;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
// All tables in the generated code derive from this class, and add their own accessors.
|
// 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 int bb_pos;
|
||||||
protected ByteBuffer bb;
|
protected ByteBuffer bb;
|
||||||
|
|
||||||
|
|||||||
@@ -105,8 +105,9 @@ class JavaTest {
|
|||||||
// Test it:
|
// Test it:
|
||||||
TestBuffer(fbb.dataBuffer(), fbb.dataStart());
|
TestBuffer(fbb.dataBuffer(), fbb.dataStart());
|
||||||
|
|
||||||
// Make sure it also works with read only ByteBuffers. This is slower, since
|
// Make sure it also works with read only ByteBuffers. This is slower,
|
||||||
// creating strings incurs an additional copy (see Table.__string).
|
// since creating strings incurs an additional copy
|
||||||
|
// (see Table.__string).
|
||||||
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer(), fbb.dataStart());
|
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer(), fbb.dataStart());
|
||||||
|
|
||||||
System.out.println("FlatBuffers test: completed successfully");
|
System.out.println("FlatBuffers test: completed successfully");
|
||||||
|
|||||||
Reference in New Issue
Block a user