| FlatBuffers An open source project by FPL.

Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
com.google.flatbuffers.FlatBufferBuilder Class Reference
Flatbuffers_java_api

Class that helps you build a FlatBuffer. More...

## Detailed Description

Class that helps you build a FlatBuffer.

See the section "Use in Java/C#" in the main FlatBuffers documentation.

## Classes
class | ByteBufferBackedInputStream
| A class that allows a user to create an InputStream from a ByteBuffer.
class | ByteBufferFactory
| An interface that provides a user of the FlatBufferBuilder class the ability to specify the method in which the internal buffer gets allocated. More...
class | HeapByteBufferFactory
| An implementation of the ByteBufferFactory interface that is used when one is not provided by the user. More...

## Public Member Functions
void | addBoolean (boolean x)
| Add a `boolean` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | addByte (byte x)
| Add a `byte` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | addDouble (double x)
| Add a `double` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | addFloat (float x)
| Add a `float` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | addInt (int x)
| Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | addLong (long x)
| Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | addOffset (int off)
| Adds on offset, relative to where it will be written. More...
void | addShort (short x)
| Add a `short` to the buffer, properly aligned, and grows the buffer (if necessary). More...
void | clear ()
| Reset the FlatBufferBuilder by purging all data that it holds.
int | createByteVector (byte[] arr)
| Create a byte array in the buffer. More...
int | createByteVector (byte[] arr, int offset, int length)
| Create a byte array in the buffer. More...
int | createByteVector (ByteBuffer byteBuffer)
| Create a byte array in the buffer. More...
int | createString (ByteBuffer s)
| Create a string in the buffer from an already encoded UTF-8 string in a ByteBuffer. More...
int | createString (CharSequence s)
| Encode the string `s` in the buffer using UTF-8. More...
ByteBuffer | createUnintializedVector (int elem_size, int num_elems, int alignment)
| Create a new array/vector and return a ByteBuffer to be filled later. More...
int | createVectorOfTables (int[] offsets)
| Create a vector of tables. More...
ByteBuffer | dataBuffer ()
| Get the ByteBuffer representing the FlatBuffer. More...
void | finish (int root_table)
| Finalize a buffer, pointing to the given `root_table`. More...
void | finish (int root_table, String file_identifier)
| Finalize a buffer, pointing to the given `root_table`. More...
void | finishSizePrefixed (int root_table)
| Finalize a buffer, pointing to the given `root_table`, with the size prefixed. More...
void | finishSizePrefixed (int root_table, String file_identifier)
| Finalize a buffer, pointing to the given `root_table`, with the size prefixed. More...
| FlatBufferBuilder ()
| Start with a buffer of 1KiB, then grow as required.
| FlatBufferBuilder (ByteBuffer existing_bb)
| Alternative constructor allowing reuse of `ByteBuffer`s. More...
| FlatBufferBuilder (ByteBuffer existing_bb, ByteBufferFactory bb_factory)
| Alternative constructor allowing reuse of `ByteBuffer`s. More...
| FlatBufferBuilder (int initial_size)
| Start with a buffer of size `initial_size`, then grow as required. More...
| FlatBufferBuilder (int initial_size, ByteBufferFactory bb_factory)
| Start with a buffer of size `initial_size`, then grow as required. More...
| FlatBufferBuilder (int initial_size, ByteBufferFactory bb_factory, ByteBuffer existing_bb, Utf8 utf8)
| Start with a buffer of size `initial_size`, then grow as required. More...
FlatBufferBuilder | forceDefaults (boolean forceDefaults)
| In order to save space, fields that are set to their default value don't get serialized into the buffer. More...
FlatBufferBuilder | init (ByteBuffer existing_bb, ByteBufferFactory bb_factory)
| Alternative initializer that allows reusing this object on an existing `ByteBuffer`. More...
int | offset ()
| Offset relative to the end of the buffer. More...
void | pad (int byte_size)
| Add zero valued bytes to prepare a new entry to be added. More...
void | prep (int size, int additional_bytes)
| Prepare to write an element of `size` after `additional_bytes` have been written, e.g. More...
void | putBoolean (boolean x)
| Add a `boolean` to the buffer, backwards from the current location. More...
void | putByte (byte x)
| Add a `byte` to the buffer, backwards from the current location. More...
void | putDouble (double x)
| Add a `double` to the buffer, backwards from the current location. More...
void | putFloat (float x)
| Add a `float` to the buffer, backwards from the current location. More...
void | putInt (int x)
| Add an `int` to the buffer, backwards from the current location. More...
void | putLong (long x)
| Add a `long` to the buffer, backwards from the current location. More...
void | putShort (short x)
| Add a `short` to the buffer, backwards from the current location. More...
byte[] | sizedByteArray ()
| A utility function to copy and return the ByteBuffer data as a `byte[]`. More...
byte[] | sizedByteArray (int start, int length)
| A utility function to copy and return the ByteBuffer data from `start` to `start` + `length` as a `byte[]`. More...
InputStream | sizedInputStream ()
| A utility function to return an InputStream to the ByteBuffer data. More...

## Static Public Member Functions
static boolean | isFieldPresent (Table table, int offset)
| Helper function to test if a field is present in the table. More...

## Protected Member Functions
void | finish (int root_table, boolean size_prefix)
| Finalize a buffer, pointing to the given `root_table`. More...
void | finish (int root_table, String file_identifier, boolean size_prefix)
| Finalize a buffer, pointing to the given `root_table`. More...

## Constructor & Destructor Documentation

## ◆ FlatBufferBuilder() [1/5]

com.google.flatbuffers.FlatBufferBuilder.FlatBufferBuilder | ( | int | initial_size,
|  | ByteBufferFactory | bb_factory
| ) |  |
| inline

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

Parameters

  initial_size | The initial size of the internal buffer to use.
  bb_factory | The factory to be used for allocating the internal buffer

## ◆ FlatBufferBuilder() [2/5]

com.google.flatbuffers.FlatBufferBuilder.FlatBufferBuilder | ( | int | initial_size,
|  | ByteBufferFactory | bb_factory,
|  | ByteBuffer | existing_bb,
|  | Utf8 | utf8
| ) |  |
| inline

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

Parameters

  initial_size | The initial size of the internal buffer to use.
  bb_factory | The factory to be used for allocating the internal buffer
  existing_bb | The byte buffer to reuse.
  utf8 | The Utf8 codec

## ◆ FlatBufferBuilder() [3/5]

com.google.flatbuffers.FlatBufferBuilder.FlatBufferBuilder | ( | int | initial_size | ) |
| inline

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() [4/5]

com.google.flatbuffers.FlatBufferBuilder.FlatBufferBuilder | ( | ByteBuffer | existing_bb,
|  | ByteBufferFactory | bb_factory
| ) |  |
| inline

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.
  bb_factory | The factory to be used for allocating a new internal buffer if the existing buffer needs to grow

## ◆ FlatBufferBuilder() [5/5]

com.google.flatbuffers.FlatBufferBuilder.FlatBufferBuilder | ( | ByteBuffer | existing_bb | ) |
| inline

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.

## Member Function Documentation

## ◆ addBoolean()

void com.google.flatbuffers.FlatBufferBuilder.addBoolean | ( | boolean | x | ) |
| inline

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

Parameters

  x | A `boolean` to put into the buffer.

## ◆ addByte()

void com.google.flatbuffers.FlatBufferBuilder.addByte | ( | byte | x | ) |
| inline

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

Parameters

  x | A `byte` to put into the buffer.

## ◆ addDouble()

void com.google.flatbuffers.FlatBufferBuilder.addDouble | ( | double | x | ) |
| inline

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

Parameters

  x | A `double` to put into the buffer.

## ◆ addFloat()

void com.google.flatbuffers.FlatBufferBuilder.addFloat | ( | float | x | ) |
| inline

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

Parameters

  x | A `float` to put into the buffer.

## ◆ addInt()

void com.google.flatbuffers.FlatBufferBuilder.addInt | ( | int | x | ) |
| inline

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

Parameters

  x | An `int` to put into the buffer.

## ◆ addLong()

void com.google.flatbuffers.FlatBufferBuilder.addLong | ( | long | x | ) |
| inline

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

Parameters

  x | A `long` to put into the buffer.

## ◆ addOffset()

void com.google.flatbuffers.FlatBufferBuilder.addOffset | ( | int | off | ) |
| inline

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

Parameters

  off | The offset to add.

## ◆ addShort()

void com.google.flatbuffers.FlatBufferBuilder.addShort | ( | short | x | ) |
| inline

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

Parameters

  x | A `short` to put into the buffer.

## ◆ createByteVector() [1/3]

int com.google.flatbuffers.FlatBufferBuilder.createByteVector | ( | byte[] | arr | ) |
| inline

Create a byte array in the buffer.

Parameters

  arr | A source array with data

Returns
  The offset in the buffer where the encoded array starts.

## ◆ createByteVector() [2/3]

int com.google.flatbuffers.FlatBufferBuilder.createByteVector | ( | byte[] | arr,
|  | int | offset,
|  | int | length
| ) |  |
| inline

Create a byte array in the buffer.

Parameters

  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.

Returns
  The offset in the buffer where the encoded array starts.

## ◆ createByteVector() [3/3]

int com.google.flatbuffers.FlatBufferBuilder.createByteVector | ( | ByteBuffer | byteBuffer | ) |
| inline

Create a byte array in the buffer.

The source `ByteBuffer` position is advanced by `ByteBuffer#remaining()` places after this call.

Parameters

  byteBuffer | A source `ByteBuffer` with data.

Returns
  The offset in the buffer where the encoded array starts.

## ◆ createString() [1/2]

int com.google.flatbuffers.FlatBufferBuilder.createString | ( | ByteBuffer | s | ) |
| inline

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.

## ◆ createString() [2/2]

int com.google.flatbuffers.FlatBufferBuilder.createString | ( | CharSequence | s | ) |
| inline

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.

## ◆ createUnintializedVector()

ByteBuffer com.google.flatbuffers.FlatBufferBuilder.createUnintializedVector | ( | int | elem_size,
|  | int | num_elems,
|  | int | alignment
| ) |  |
| inline

Create a new array/vector and return a ByteBuffer to be filled later.

Call `endVector` after this method to get an offset to the beginning of vector.

Parameters

  elem_size | the size of each element in bytes.
  num_elems | number of elements in the vector.
  alignment | byte alignment.

Returns
  ByteBuffer with position and limit set to the space allocated for the array.

## ◆ createVectorOfTables()

int com.google.flatbuffers.FlatBufferBuilder.createVectorOfTables | ( | int[] | offsets | ) |
| inline

Create a vector of tables.

Parameters

  offsets | Offsets of the tables.

Returns
  Returns offset of the vector.

## ◆ dataBuffer()

ByteBuffer com.google.flatbuffers.FlatBufferBuilder.dataBuffer | ( |  | ) |
| inline

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

## ◆ finish() [1/4]

void com.google.flatbuffers.FlatBufferBuilder.finish | ( | int | root_table | ) |
| inline

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

Parameters

  root_table | An offset to be added to the buffer.

## ◆ finish() [2/4]

void com.google.flatbuffers.FlatBufferBuilder.finish | ( | int | root_table,
|  | boolean | size_prefix
| ) |  |
| inline protected

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

Parameters

  root_table | An offset to be added to the buffer.
  size_prefix | Whether to prefix the size to the buffer.

## ◆ finish() [3/4]

void com.google.flatbuffers.FlatBufferBuilder.finish | ( | int | root_table,
|  | String | file_identifier
| ) |  |
| inline

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

## ◆ finish() [4/4]

void com.google.flatbuffers.FlatBufferBuilder.finish | ( | int | root_table,
|  | String | file_identifier,
|  | boolean | size_prefix
| ) |  |
| inline protected

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`.
  size_prefix | Whether to prefix the size to the buffer.

## ◆ finishSizePrefixed() [1/2]

void com.google.flatbuffers.FlatBufferBuilder.finishSizePrefixed | ( | int | root_table | ) |
| inline

Finalize a buffer, pointing to the given `root_table`, with the size prefixed.

Parameters

  root_table | An offset to be added to the buffer.

## ◆ finishSizePrefixed() [2/2]

void com.google.flatbuffers.FlatBufferBuilder.finishSizePrefixed | ( | int | root_table,
|  | String | file_identifier
| ) |  |
| inline

Finalize a buffer, pointing to the given `root_table`, with the size prefixed.

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()

FlatBufferBuilder com.google.flatbuffers.FlatBufferBuilder.forceDefaults | ( | boolean | forceDefaults | ) |
| inline

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

## ◆ init()

FlatBufferBuilder com.google.flatbuffers.FlatBufferBuilder.init | ( | ByteBuffer | existing_bb,
|  | ByteBufferFactory | bb_factory
| ) |  |
| inline

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.
  bb_factory | The factory to be used for allocating a new internal buffer if the existing buffer needs to grow

Returns
  Returns `this`.

## ◆ isFieldPresent()

static boolean com.google.flatbuffers.FlatBufferBuilder.isFieldPresent | ( | Table | table,
|  | int | offset
| ) |  |
| inline static

Helper function to test if a field is present in the table.

Parameters

  table | Flatbuffer table
  offset | virtual table offset

Returns
  true if the filed is present

## ◆ offset()

int com.google.flatbuffers.FlatBufferBuilder.offset | ( |  | ) |
| inline

Offset relative to the end of the buffer.

Returns
  Offset relative to the end of the buffer.

## ◆ pad()

void com.google.flatbuffers.FlatBufferBuilder.pad | ( | int | byte_size | ) |
| inline

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

Parameters

  byte_size | Number of bytes to add.

## ◆ prep()

void com.google.flatbuffers.FlatBufferBuilder.prep | ( | int | size,
|  | int | additional_bytes
| ) |  |
| inline

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 `com.google.flatbuffers.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()

void com.google.flatbuffers.FlatBufferBuilder.putBoolean | ( | boolean | x | ) |
| inline

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()

void com.google.flatbuffers.FlatBufferBuilder.putByte | ( | byte | x | ) |
| inline

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.

## ◆ putDouble()

void com.google.flatbuffers.FlatBufferBuilder.putDouble | ( | double | x | ) |
| inline

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.

## ◆ putFloat()

void com.google.flatbuffers.FlatBufferBuilder.putFloat | ( | float | x | ) |
| inline

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.

## ◆ putInt()

void com.google.flatbuffers.FlatBufferBuilder.putInt | ( | int | x | ) |
| inline

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()

void com.google.flatbuffers.FlatBufferBuilder.putLong | ( | long | x | ) |
| inline

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.

## ◆ putShort()

void com.google.flatbuffers.FlatBufferBuilder.putShort | ( | short | x | ) |
| inline

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.

## ◆ sizedByteArray() [1/2]

byte[] com.google.flatbuffers.FlatBufferBuilder.sizedByteArray | ( |  | ) |
| inline

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

Returns
  A full copy of the `data buffer`.

## ◆ sizedByteArray() [2/2]

byte[] com.google.flatbuffers.FlatBufferBuilder.sizedByteArray | ( | int | start,
|  | int | length
| ) |  |
| inline

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

Exceptions

  IndexOutOfBoundsException | If the range of bytes is ouf of bound.

## ◆ sizedInputStream()

InputStream com.google.flatbuffers.FlatBufferBuilder.sizedInputStream | ( |  | ) |
| inline

A utility function to return an InputStream to the ByteBuffer data.

Returns
  An InputStream that starts at the beginning of the ByteBuffer data and can read to the end of it.

The documentation for this class was generated from the following file:

- FlatBufferBuilder.java