public class FlatBufferBuilder extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
FlatBufferBuilder.ByteBufferFactory
An interface that provides a user of the FlatBufferBuilder class the ability to specify
the method in which the internal buffer gets allocated.
|
static class |
FlatBufferBuilder.HeapByteBufferFactory
An implementation of the ByteBufferFactory interface that is used when
one is not provided by the user.
|
| 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(ByteBuffer existing_bb,
FlatBufferBuilder.ByteBufferFactory bb_factory)
Alternative constructor allowing reuse of
ByteBuffers. |
FlatBufferBuilder(int initial_size)
Start with a buffer of size `initial_size`, then grow as required.
|
FlatBufferBuilder(int initial_size,
FlatBufferBuilder.ByteBufferFactory bb_factory)
Start with a buffer of size `initial_size`, then grow as required.
|
FlatBufferBuilder(int initial_size,
FlatBufferBuilder.ByteBufferFactory bb_factory,
ByteBuffer existing_bb,
Utf8 utf8)
Start with a buffer of size `initial_size`, then grow as required.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addBoolean(boolean x)
Add a `boolean` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addBoolean(int o,
boolean x,
boolean d)
Add a `boolean` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addByte(byte x)
Add a `byte` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addByte(int o,
byte x,
int d)
Add a `byte` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addDouble(double x)
Add a `double` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addDouble(int o,
double x,
double d)
Add a `double` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addFloat(float x)
Add a `float` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addFloat(int o,
float x,
double d)
Add a `float` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addInt(int x)
Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addInt(int o,
int x,
int d)
Add an `int` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addLong(int o,
long x,
long d)
Add a `long` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addLong(long x)
Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addOffset(int off)
Adds on offset, relative to where it will be written.
|
void |
addOffset(int o,
int x,
int d)
Add an `offset` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addShort(int o,
short x,
int d)
Add a `short` to a table at `o` into its vtable, with value `x` and default `d`.
|
void |
addShort(short x)
Add a `short` to the buffer, properly aligned, and grows the buffer (if necessary).
|
void |
addStruct(int voffset,
int x,
int d)
Add a struct to the table.
|
void |
clear()
Reset the FlatBufferBuilder by purging all data that it holds.
|
int |
createByteVector(byte[] arr)
Create a byte array in the buffer.
|
int |
createByteVector(byte[] arr,
int offset,
int length)
Create a byte array in the buffer.
|
int |
createByteVector(ByteBuffer byteBuffer)
Create a byte array in the buffer.
|
<T extends Table> |
createSortedVectorOfTables(T obj,
int[] offsets)
Create a vector of sorted by the key tables.
|
int |
createString(ByteBuffer s)
Create a string in the buffer from an already encoded UTF-8 string in a ByteBuffer.
|
int |
createString(CharSequence s)
Encode the string `s` in the buffer using UTF-8.
|
ByteBuffer |
createUnintializedVector(int elem_size,
int num_elems,
int alignment)
Create a new array/vector and return a ByteBuffer to be filled later.
|
int |
createVectorOfTables(int[] offsets)
Create a vector of tables.
|
ByteBuffer |
dataBuffer()
Get the ByteBuffer representing the FlatBuffer.
|
int |
endTable()
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)
Finalize a buffer, pointing to the given `root_table`.
|
protected void |
finish(int root_table,
boolean size_prefix)
Finalize a buffer, pointing to the given `root_table`.
|
void |
finish(int root_table,
String file_identifier)
Finalize a buffer, pointing to the given `root_table`.
|
protected void |
finish(int root_table,
String file_identifier,
boolean size_prefix)
Finalize a buffer, pointing to the given `root_table`.
|
void |
finished()
Should not be accessing the final buffer before it is finished.
|
void |
finishSizePrefixed(int root_table)
Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
|
void |
finishSizePrefixed(int root_table,
String file_identifier)
Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
|
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,
FlatBufferBuilder.ByteBufferFactory bb_factory)
Alternative initializer that allows reusing this object on an existing
`ByteBuffer`.
|
static boolean |
isFieldPresent(Table table,
int offset)
Helper function to test if a field is present in the table
|
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)
Add a `boolean` to the buffer, backwards from the current location.
|
void |
putByte(byte x)
Add a `byte` to the buffer, backwards from the current location.
|
void |
putDouble(double x)
Add a `double` to the buffer, backwards from the current location.
|
void |
putFloat(float x)
Add a `float` to the buffer, backwards from the current location.
|
void |
putInt(int x)
Add an `int` to the buffer, backwards from the current location.
|
void |
putLong(long x)
Add a `long` to the buffer, backwards from the current location.
|
void |
putShort(short x)
Add a `short` to the buffer, backwards from the current location.
|
void |
required(int table,
int field)
Checks that a required field has been set in a given table that has
just been constructed.
|
byte[] |
sizedByteArray()
A utility function to copy and return the ByteBuffer data as a `byte[]`.
|
byte[] |
sizedByteArray(int start,
int length)
A utility function to copy and return the ByteBuffer data from `start` to
`start` + `length` as a `byte[]`.
|
InputStream |
sizedInputStream()
A utility function to return an InputStream to the ByteBuffer data
|
void |
slot(int voffset)
Set the current vtable at `voffset` to the current location in the buffer.
|
void |
startTable(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,
FlatBufferBuilder.ByteBufferFactory bb_factory)
initial_size - The initial size of the internal buffer to use.bb_factory - The factory to be used for allocating the internal bufferpublic FlatBufferBuilder(int initial_size,
FlatBufferBuilder.ByteBufferFactory bb_factory,
ByteBuffer existing_bb,
Utf8 utf8)
initial_size - The initial size of the internal buffer to use.bb_factory - The factory to be used for allocating the internal bufferexisting_bb - The byte buffer to reuse.utf8 - The Utf8 codecpublic FlatBufferBuilder(int initial_size)
initial_size - The initial size of the internal buffer to use.public FlatBufferBuilder()
public FlatBufferBuilder(ByteBuffer existing_bb, FlatBufferBuilder.ByteBufferFactory bb_factory)
ByteBuffers. The builder
can still grow the buffer as necessary. User classes should make sure
to call dataBuffer() to obtain the resulting encoded message.existing_bb - The byte buffer to reuse.bb_factory - The factory to be used for allocating a new internal buffer if
the existing buffer needs to growpublic 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 message.existing_bb - The byte buffer to reuse.public FlatBufferBuilder init(ByteBuffer existing_bb, FlatBufferBuilder.ByteBufferFactory bb_factory)
existing_bb - The byte buffer to reuse.bb_factory - The factory to be used for allocating a new internal buffer if
the existing buffer needs to growpublic static boolean isFieldPresent(Table table, int offset)
table - Flatbuffer tableoffset - virtual table offsetpublic void clear()
public int offset()
public void pad(int byte_size)
byte_size - Number of bytes to add.public void prep(int size,
int additional_bytes)
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 write.additional_bytes - The padding size.public void putBoolean(boolean x)
x - A `boolean` to put into the buffer.public void putByte(byte x)
x - A `byte` to put into the buffer.public void putShort(short x)
x - A `short` to put into the buffer.public void putInt(int x)
x - An `int` to put into the buffer.public void putLong(long x)
x - A `long` to put into the buffer.public void putFloat(float x)
x - A `float` to put into the buffer.public void putDouble(double x)
x - A `double` to put into the buffer.public void addBoolean(boolean x)
x - A `boolean` to put into the buffer.public void addByte(byte x)
x - A `byte` to put into the buffer.public void addShort(short x)
x - A `short` to put into the buffer.public void addInt(int x)
x - An `int` to put into the buffer.public void addLong(long x)
x - A `long` to put into the buffer.public void addFloat(float x)
x - A `float` to put into the buffer.public void addDouble(double x)
x - A `double` to put into the buffer.public void addOffset(int off)
off - The offset to add.public void startVector(int elem_size,
int num_elems,
int alignment)
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 array.num_elems - The number of elements in the array.alignment - The alignment of the array.public int endVector()
startVector(int, int, int).startVector(int, int, int)public ByteBuffer createUnintializedVector(int elem_size, int num_elems, int alignment)
endVector() after this method to get an offset to the beginning
of vector.elem_size - the size of each element in bytes.num_elems - number of elements in the vector.alignment - byte alignment.public int createVectorOfTables(int[] offsets)
offsets - Offsets of the tables.public <T extends Table> int createSortedVectorOfTables(T obj, int[] offsets)
obj - Instance of the table subclass.offsets - Offsets of the tables.public int createString(CharSequence s)
s is
already a CharBuffer, this method is allocation free.s - The string to encode.public int createString(ByteBuffer s)
s - An already encoded UTF-8 string as a `ByteBuffer`.public int createByteVector(byte[] arr)
arr - A source array with datapublic int createByteVector(byte[] arr,
int offset,
int length)
arr - a source array with data.offset - the offset in the source array to start copying from.length - the number of bytes to copy from the source array.public int createByteVector(ByteBuffer byteBuffer)
ByteBuffer position is advanced by Buffer.remaining() places
after this call.byteBuffer - A source ByteBuffer with data.public void finished()
public void notNested()
public void Nested(int obj)
obj - The offset of the created object.public void startTable(int numfields)
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:
#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)
o - The index into the vtable.x - A `boolean` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - A `boolean` default value to compare against when `force_defaults` is `false`.public void addByte(int o,
byte x,
int d)
o - The index into the vtable.x - A `byte` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - A `byte` default value to compare against when `force_defaults` is `false`.public void addShort(int o,
short x,
int d)
o - The index into the vtable.x - A `short` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - A `short` default value to compare against when `force_defaults` is `false`.public void addInt(int o,
int x,
int d)
o - The index into the vtable.x - An `int` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - An `int` default value to compare against when `force_defaults` is `false`.public void addLong(int o,
long x,
long d)
o - The index into the vtable.x - A `long` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - A `long` default value to compare against when `force_defaults` is `false`.public void addFloat(int o,
float x,
double d)
o - The index into the vtable.x - A `float` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - A `float` default value to compare against when `force_defaults` is `false`.public void addDouble(int o,
double x,
double d)
o - The index into the vtable.x - A `double` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - A `double` default value to compare against when `force_defaults` is `false`.public void addOffset(int o,
int x,
int d)
o - The index into the vtable.x - An `offset` to put into the buffer, depending on how defaults are handled. If
`force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the
default value, it can be skipped.d - An `offset` default value to compare against when `force_defaults` is `false`.public void addStruct(int voffset,
int x,
int d)
voffset - The index into the vtable.x - The offset of the created struct.d - The default value is always `0`.public void slot(int voffset)
voffset - The index into the vtable to store the offset relative to the end of the
buffer.public int endTable()
dataBuffer().startTable(int)public void required(int table,
int field)
table - The offset to the start of the table from the `ByteBuffer` capacity.field - The offset to the field in the vtable.protected void finish(int root_table,
boolean size_prefix)
root_table - An offset to be added to the buffer.size_prefix - Whether to prefix the size to the buffer.public void finish(int root_table)
root_table - An offset to be added to the buffer.public void finishSizePrefixed(int root_table)
root_table - An offset to be added to the buffer.protected void finish(int root_table,
String file_identifier,
boolean size_prefix)
root_table - An offset to be added to the buffer.file_identifier - A FlatBuffer file identifier to be added to the buffer before
`root_table`.size_prefix - Whether to prefix the size to the buffer.public void finish(int root_table,
String file_identifier)
root_table - An offset to be added to the buffer.file_identifier - A FlatBuffer file identifier to be added to the buffer before
`root_table`.public void finishSizePrefixed(int root_table,
String file_identifier)
root_table - An offset to be added to the buffer.file_identifier - A FlatBuffer file identifier to be added to the buffer before
`root_table`.public FlatBufferBuilder forceDefaults(boolean forceDefaults)
forceDefaults - When set to `true`, always serializes default values.public ByteBuffer dataBuffer()
ByteBuffer representing the FlatBufferpublic byte[] sizedByteArray(int start,
int length)
start - Start copying at this offset.length - How many bytes to copy.data buffer.IndexOutOfBoundsException - If the range of bytes is ouf of bound.public byte[] sizedByteArray()
data buffer.public InputStream sizedInputStream()
Copyright © 2020. All rights reserved.