public class FlatBufferBuilder extends Object
| Constructor and Description |
|---|
FlatBufferBuilder()
Start with a buffer of 1KiB, then grow as required.
|
FlatBufferBuilder(ByteBuffer existing_bb)
Alternative constructor allowing reuse of
ByteBuffers. |
FlatBufferBuilder(int initial_size)
Start with a buffer of size
initial_size, then grow as required. |
| Modifier and Type | Method and Description |
|---|---|
void |
addBoolean(boolean x) |
void |
addBoolean(int o,
boolean x,
boolean d) |
void |
addByte(byte x) |
void |
addByte(int o,
byte x,
int d) |
void |
addDouble(double x) |
void |
addDouble(int o,
double x,
double d) |
void |
addFloat(float x) |
void |
addFloat(int o,
float x,
double d) |
void |
addInt(int x) |
void |
addInt(int o,
int x,
int d) |
void |
addLong(int o,
long x,
long d) |
void |
addLong(long x) |
void |
addOffset(int off)
Adds on offset, relative to where it will be written.
|
void |
addOffset(int o,
int x,
int d) |
void |
addShort(int o,
short x,
int d) |
void |
addShort(short x) |
void |
addStruct(int voffset,
int x,
int d) |
int |
createString(ByteBuffer s)
Encode the string
s in the buffer using UTF-8. |
int |
createString(String s)
Encode the string
s in the buffer using UTF-8. |
ByteBuffer |
dataBuffer() |
int |
endObject()
Finish off writing the object that is under construction.
|
int |
endVector()
Finish off the creation of an array and all its elements.
|
void |
finish(int root_table) |
void |
finish(int root_table,
String file_identifier) |
FlatBufferBuilder |
forceDefaults(boolean forceDefaults)
In order to save space, fields that are set to their default value
don't get serialized into the buffer.
|
FlatBufferBuilder |
init(ByteBuffer existing_bb)
Alternative initializer that allows reusing this object on an existing
ByteBuffer.
|
void |
Nested(int obj)
Structures are always stored inline, they need to be created right
where they're used.
|
void |
notNested()
Should not be creating any other object, string or vector
while an object is being constructed
|
int |
offset()
Offset relative to the end of the buffer.
|
void |
pad(int byte_size)
Add zero valued bytes to prepare a new entry to be added
|
void |
prep(int size,
int additional_bytes)
Prepare to write an element of
size after additional_bytes
have been written, e.g. |
void |
putBoolean(boolean x) |
void |
putByte(byte x) |
void |
putDouble(double x) |
void |
putFloat(float x) |
void |
putInt(int x) |
void |
putLong(long x) |
void |
putShort(short x) |
void |
required(int table,
int field) |
byte[] |
sizedByteArray()
Utility function for copying a byte array that starts at 0.
|
byte[] |
sizedByteArray(int start,
int length)
Utility function for copying a byte array from
start to
start + length |
void |
slot(int voffset) |
void |
startObject(int numfields)
Start encoding a new object in the buffer.
|
void |
startVector(int elem_size,
int num_elems,
int alignment)
Start a new array/vector of objects.
|
public FlatBufferBuilder(int initial_size)
initial_size, then grow as required.initial_size - The initial size of the internal buffer to usepublic FlatBufferBuilder()
public FlatBufferBuilder(ByteBuffer existing_bb)
ByteBuffers. The builder
can still grow the buffer as necessary. User classes should make sure
to call dataBuffer() to obtain the resulting encoded messageexisting_bb - The byte buffer to reusepublic FlatBufferBuilder init(ByteBuffer existing_bb)
existing_bb - The byte buffer to reusepublic int offset()
public void pad(int byte_size)
byte_size - Number of bytes to add.public void prep(int size,
int additional_bytes)
size after additional_bytes
have been written, e.g. if you write a string, you need to align such
the int length field is aligned to Constants.SIZEOF_INT, and
the string data follows it directly. If all you need to do is alignment, additional_bytes
will be 0.size - This is the of the new element to writeadditional_bytes - The padding sizepublic void putBoolean(boolean x)
public void putByte(byte x)
public void putShort(short x)
public void putInt(int x)
public void putLong(long x)
public void putFloat(float x)
public void putDouble(double x)
public void addBoolean(boolean x)
public void addByte(byte x)
public void addShort(short x)
public void addInt(int x)
public void addLong(long x)
public void addFloat(float x)
public void addDouble(double x)
public void addOffset(int off)
off - The offset to addpublic void startVector(int elem_size,
int num_elems,
int alignment)
FlatBuffers compiler will create a start/end
method for vector types in generated code.
The expected sequence of calls is:
addOffset(int) num_elems number of times to set
the offset of each element in the array.endVector() to retrieve the offset of the array.For example, to create an array of strings, do:
// Need 10 strings
FlatBufferBuilder builder = new FlatBufferBuilder(existingBuffer);
int[] offsets = new int[10];
for (int i = 0; i < 10; i++) {
offsets[i] = fbb.createString(" " + i);
}
// Have the strings in the buffer, but don't have a vector.
// Add a vector that references the newly created strings:
builder.startVector(4, offsets.length, 4);
// Add each string to the newly created vector
// The strings are added in reverse order since the buffer
// is filled in back to front
for (int i = offsets.length - 1; i >= 0; i--) {
builder.addOffset(offsets[i]);
}
// Finish off the vector
int offsetOfTheVector = fbb.endVector();
elem_size - The size of each element in the arraynum_elems - The number of elements in the arrayalignment - The alignment of the arraypublic int endVector()
startVector(int, int, int).startVector(int, int, int)public int createString(String s)
s in the buffer using UTF-8.s - The string to encodepublic int createString(ByteBuffer s)
s in the buffer using UTF-8.s - An already encoded UTF-8 stringpublic void notNested()
public void Nested(int obj)
obj - The offset of the created objectpublic void startObject(int numfields)
FlatBuffers compiler will generate helper methods
that call this method internally.
For example, using the "Monster" code found on the
landing page. An
object of type Monster can be created using the following code:
int testArrayOfString = Monster.createTestarrayofstringVector(fbb, new int[] {
fbb.createString("test1"),
fbb.createString("test2")
});
Monster.startMonster(fbb);
Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
Color.Green, (short)5, (byte)6));
Monster.addHp(fbb, (short)80);
Monster.addName(fbb, str);
Monster.addInventory(fbb, inv);
Monster.addTestType(fbb, (byte)Any.Monster);
Monster.addTest(fbb, mon2);
Monster.addTest4(fbb, test4);
Monster.addTestarrayofstring(fbb, testArrayOfString);
int mon = Monster.endMonster(fbb);
Here:
Monster#startMonster(FlatBufferBuilder) will call this
method with the right number of fields set.Monster#endMonster(FlatBufferBuilder) will ensure endObject() is called.
It's not recommended to call this method directly. If it's called manually, you must ensure
to audit all calls to it whenever fields are added or removed from your schema. This is
automatically done by the code generated by the FlatBuffers compiler.
numfields - The number of fields found in this object.public void addBoolean(int o,
boolean x,
boolean d)
public void addByte(int o,
byte x,
int d)
public void addShort(int o,
short x,
int d)
public void addInt(int o,
int x,
int d)
public void addLong(int o,
long x,
long d)
public void addFloat(int o,
float x,
double d)
public void addDouble(int o,
double x,
double d)
public void addOffset(int o,
int x,
int d)
public void addStruct(int voffset,
int x,
int d)
public void slot(int voffset)
public int endObject()
dataBuffer()startObject(int)public void required(int table,
int field)
public void finish(int root_table)
public void finish(int root_table,
String file_identifier)
public FlatBufferBuilder forceDefaults(boolean forceDefaults)
forceDefaults - true always serializes default valuespublic ByteBuffer dataBuffer()
public byte[] sizedByteArray(int start,
int length)
start to
start + lengthstart - Start copying at this offsetlength - How many bytes to copydata bufferIndexOutOfBoundsException - If the range of bytes is ouf of boundpublic byte[] sizedByteArray()
data bufferCopyright © 2015. All rights reserved.