com.google.flatbuffers

## Class FlatBufferBuilder

- java.lang.Object
- - com.google.flatbuffers.FlatBufferBuilder

    public class FlatBufferBuilder
    extends Object

Class that helps you build a FlatBuffer. See the section "Use in Java/C#" in the main FlatBuffers documentation.

### Constructor Summary

Constructors
Constructor and Description
`FlatBufferBuilder()` Start with a buffer of 1KiB, then grow as required.
`FlatBufferBuilder(ByteBuffer existing_bb)` Alternative constructor allowing reuse of `ByteBuffer`s.
`FlatBufferBuilder(int initial_size)` Start with a buffer of size `initial_size`, then grow as required.

### Method Summary

All Methods Instance Methods Concrete Methods
`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(CharSequence 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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### FlatBufferBuilder

    public FlatBufferBuilder(int initial_size)

Start with a buffer of size `initial_size`, then grow as required.

Parameters:
  `initial_size` - The initial size of the internal buffer to use.

#### FlatBufferBuilder

    public FlatBufferBuilder()

Start with a buffer of 1KiB, then grow as required.

#### FlatBufferBuilder

    public FlatBufferBuilder(ByteBuffer existing_bb)

Alternative constructor allowing reuse of `ByteBuffer`s. The builder can still grow the buffer as necessary. User classes should make sure to call `dataBuffer()` to obtain the resulting encoded message.

Parameters:
  `existing_bb` - The byte buffer to reuse.

### Method Detail

#### init

    public FlatBufferBuilder init(ByteBuffer existing_bb)

Alternative initializer that allows reusing this object on an existing `ByteBuffer`. This method resets the builder's internal state, but keeps objects that have been allocated for temporary storage.

Parameters:
  `existing_bb` - The byte buffer to reuse.
Returns:
  Returns `this`.

#### offset

    public int offset()

Offset relative to the end of the buffer.

Returns:
  Offset relative to the end of the buffer.

#### pad

    public void pad(int byte_size)

Add zero valued bytes to prepare a new entry to be added.

Parameters:
  `byte_size` - Number of bytes to add.

#### prep

    public void prep(int size,
                     int additional_bytes)

Prepare to write an element of `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.

Parameters:
  `size` - This is the of the new element to write.
  `additional_bytes` - The padding size.

#### putBoolean

    public void putBoolean(boolean x)

Add a `boolean` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - A `boolean` to put into the buffer.

#### putByte

    public void putByte(byte x)

Add a `byte` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - A `byte` to put into the buffer.

#### putShort

    public void putShort(short x)

Add a `short` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - A `short` to put into the buffer.

#### putInt

    public void putInt(int x)

Add an `int` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - An `int` to put into the buffer.

#### putLong

    public void putLong(long x)

Add a `long` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - A `long` to put into the buffer.

#### putFloat

    public void putFloat(float x)

Add a `float` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - A `float` to put into the buffer.

#### putDouble

    public void putDouble(double x)

Add a `double` to the buffer, backwards from the current location. Doesn't align nor check for space.

Parameters:
  `x` - A `double` to put into the buffer.

#### addBoolean

    public void addBoolean(boolean x)

Add a `boolean` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - A `boolean` to put into the buffer.

#### addByte

    public void addByte(byte x)

Add a `byte` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - A `byte` to put into the buffer.

#### addShort

    public void addShort(short x)

Add a `short` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - A `short` to put into the buffer.

#### addInt

    public void addInt(int x)

Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - An `int` to put into the buffer.

#### addLong

    public void addLong(long x)

Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - A `long` to put into the buffer.

#### addFloat

    public void addFloat(float x)

Add a `float` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - A `float` to put into the buffer.

#### addDouble

    public void addDouble(double x)

Add a `double` to the buffer, properly aligned, and grows the buffer (if necessary).

Parameters:
  `x` - A `double` to put into the buffer.

#### addOffset

    public void addOffset(int off)

Adds on offset, relative to where it will be written.

Parameters:
  `off` - The offset to add.

#### startVector

    public void startVector(int elem_size,
                            int num_elems,
                            int alignment)

Start a new array/vector of objects. Users usually will not call this directly. The `FlatBuffers` compiler will create a start/end method for vector types in generated code.

The expected sequence of calls is:

1. Start the array using this method.
2. Call `addOffset(int)` `num_elems` number of times to set the offset of each element in the array.
3. Call `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();

Parameters:
  `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.

#### endVector

    public int endVector()

Finish off the creation of an array and all its elements. The array must be created with `startVector(int, int, int)`.

Returns:
  The offset at which the newly created array starts.
See Also:
  `startVector(int, int, int)`

#### createString

    public int createString(CharSequence s)

Encode the string `s` in the buffer using UTF-8. If `s` is already a `CharBuffer`, this method is allocation free.

Parameters:
  `s` - The string to encode.
Returns:
  The offset in the buffer where the encoded string starts.

#### createString

    public int createString(ByteBuffer s)

Create a string in the buffer from an already encoded UTF-8 string in a ByteBuffer.

Parameters:
  `s` - An already encoded UTF-8 string as a `ByteBuffer`.
Returns:
  The offset in the buffer where the encoded string starts.

#### finished

    public void finished()

Should not be accessing the final buffer before it is finished.

#### notNested

    public void notNested()

Should not be creating any other object, string or vector while an object is being constructed.

#### Nested

    public void Nested(int obj)

Structures are always stored inline, they need to be created right where they're used. You'll get this assertion failure if you created it elsewhere.

Parameters:
  `obj` - The offset of the created object.

#### startObject

    public void startObject(int numfields)

Start encoding a new object in the buffer. Users will not usually need to call this directly. The `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:

- The call to `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.

Parameters:
  `numfields` - The number of fields found in this object.

#### addBoolean

    public 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`.

Parameters:
  `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`.

#### addByte

    public 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`.

Parameters:
  `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`.

#### addShort

    public 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`.

Parameters:
  `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`.

#### addInt

    public 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`.

Parameters:
  `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`.

#### addLong

    public 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`.

Parameters:
  `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`.

#### addFloat

    public 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`.

Parameters:
  `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`.

#### addDouble

    public 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`.

Parameters:
  `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`.

#### addOffset

    public 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`.

Parameters:
  `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`.

#### addStruct

    public void addStruct(int voffset,
                          int x,
                          int d)

Add a struct to the table. Structs are stored inline, so nothing additional is being added.

Parameters:
  `voffset` - The index into the vtable.
  `x` - The offset of the created struct.
  `d` - The default value is always `0`.

#### slot

    public void slot(int voffset)

Set the current vtable at `voffset` to the current location in the buffer.

Parameters:
  `voffset` - The index into the vtable to store the offset relative to the end of the buffer.

#### endObject

    public int endObject()

Finish off writing the object that is under construction.

Returns:
  The offset to the object inside `dataBuffer()`.
See Also:
  `startObject(int)`

#### required

    public void required(int table,
                         int field)

Checks that a required field has been set in a given table that has just been constructed.

Parameters:
  `table` - The offset to the start of the table from the `ByteBuffer` capacity.
  `field` - The offset to the field in the vtable.

#### finish

    public void finish(int root_table)

Finalize a buffer, pointing to the given `root_table`.

Parameters:
  `root_table` - An offset to be added to the buffer.

#### finish

    public void finish(int root_table,
                       String file_identifier)

Finalize a buffer, pointing to the given `root_table`.

Parameters:
  `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`.

#### forceDefaults

    public FlatBufferBuilder forceDefaults(boolean forceDefaults)

In order to save space, fields that are set to their default value don't get serialized into the buffer. Forcing defaults provides a way to manually disable this optimization.

Parameters:
  `forceDefaults` - When set to `true`, always serializes default values.
Returns:
  Returns `this`.

#### dataBuffer

    public ByteBuffer dataBuffer()

Get the ByteBuffer representing the FlatBuffer. Only call this after you've called `finish()`. The actual data starts at the ByteBuffer's current position, not necessarily at `0`.

Returns:
  The `ByteBuffer` representing the FlatBuffer

#### sizedByteArray

    public byte[] sizedByteArray(int start,
                                 int length)

A utility function to copy and return the ByteBuffer data from `start` to `start` + `length` as a `byte[]`.

Parameters:
  `start` - Start copying at this offset.
  `length` - How many bytes to copy.
Returns:
  A range copy of the `data buffer`.
Throws:
  `IndexOutOfBoundsException` - If the range of bytes is ouf of bound.

#### sizedByteArray

    public byte[] sizedByteArray()

A utility function to copy and return the ByteBuffer data as a `byte[]`.

Returns:
  A full copy of the `data buffer`.