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)
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.
|
int |
createString(ByteBuffer s)
Create a string in the buffer from an already encoded UTF-8 string in a ByteBuffer.
|
int |
createString(String s)
Encode the string `s` in the buffer using UTF-8.
|
ByteBuffer |
dataBuffer()
Get the ByteBuffer representing the FlatBuffer.
|
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)
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`.
|
void |
finished()
Should not be accessing the final buffer before it is finished.
|
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)
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[]`.
|
void |
slot(int voffset)
Set the current vtable at `voffset` to the current location in the buffer.
|
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 - The initial size of the internal buffer to use.public 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 message.existing_bb - The byte buffer to reuse.public FlatBufferBuilder init(ByteBuffer existing_bb)
existing_bb - The byte buffer to reuse.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 int createString(String s)
s - The string to encode.public int createString(ByteBuffer s)
s - An already encoded UTF-8 string as a `ByteBuffer`.public void finished()
public void notNested()
public void Nested(int obj)
obj - The offset of the created object.public void startObject(int numfields)
For example, using the "Monster" code found on the
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 endObject()
dataBuffer().startObject(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.public void finish(int root_table)
root_table - An offset to be added 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 FlatBufferBuilder forceDefaults(boolean forceDefaults)
forceDefaults - When set to `true`, always serializes default values.public ByteBuffer dataBuffer()
public 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.Copyright © 2016. All rights reserved.