Class FlatBufferBuilder


  • public class FlatBufferBuilder
    extends java.lang.Object
    Class that helps you build a FlatBuffer. See the section "Use in Java/C#" in the main FlatBuffers documentation.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class 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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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​(java.nio.ByteBuffer byteBuffer)
      Create a byte array in the buffer.
      int createSharedString​(java.lang.String s)
      Encode the String `s` in the buffer using UTF-8.
      <T extends Table>
      int
      createSortedVectorOfTables​(T obj, int[] offsets)
      Create a vector of sorted by the key tables.
      int createString​(java.lang.CharSequence s)
      Encode the string `s` in the buffer using UTF-8.
      int createString​(java.nio.ByteBuffer s)
      Create a string in the buffer from an already encoded UTF-8 string in a ByteBuffer.
      java.nio.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.
      java.nio.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, java.lang.String file_identifier)
      Finalize a buffer, pointing to the given `root_table`.
      protected void finish​(int root_table, java.lang.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, java.lang.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​(java.nio.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[]`.
      java.io.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.
      • 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,
                                 FlatBufferBuilder.ByteBufferFactory bb_factory)
        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

        public FlatBufferBuilder​(int initial_size,
                                 FlatBufferBuilder.ByteBufferFactory bb_factory,
                                 java.nio.ByteBuffer existing_bb,
                                 Utf8 utf8)
        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

        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​(java.nio.ByteBuffer existing_bb,
                                 FlatBufferBuilder.ByteBufferFactory bb_factory)
        Alternative constructor allowing reuse of ByteBuffers. 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

        public FlatBufferBuilder​(java.nio.ByteBuffer existing_bb)
        Alternative constructor allowing reuse of ByteBuffers. 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​(java.nio.ByteBuffer existing_bb,
                                      FlatBufferBuilder.ByteBufferFactory bb_factory)
        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

        public static boolean isFieldPresent​(Table table,
                                             int offset)
        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
      • clear

        public void clear()
        Reset the FlatBufferBuilder by purging all data that it holds.
      • 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.
      • createUnintializedVector

        public java.nio.ByteBuffer createUnintializedVector​(int elem_size,
                                                            int num_elems,
                                                            int alignment)
        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

        public int createVectorOfTables​(int[] offsets)
        Create a vector of tables.
        Parameters:
        offsets - Offsets of the tables.
        Returns:
        Returns offset of the vector.
      • createSortedVectorOfTables

        public <T extends Table> int createSortedVectorOfTables​(T obj,
                                                                int[] offsets)
        Create a vector of sorted by the key tables.
        Parameters:
        obj - Instance of the table subclass.
        offsets - Offsets of the tables.
        Returns:
        Returns offset of the sorted vector.
      • createSharedString

        public int createSharedString​(java.lang.String s)
        Encode the String `s` in the buffer using UTF-8. If a String with this exact contents has already been serialized using this method, instead simply returns the offset of the existing String. Usage of the method will incur into additional allocations, so it is advisable to use it only when it is known upfront that your message will have several repeated strings.
        Parameters:
        s - The String to encode.
        Returns:
        The offset in the buffer where the encoded String starts.
      • createString

        public int createString​(java.lang.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​(java.nio.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.
      • createByteVector

        public int createByteVector​(byte[] arr)
        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

        public int createByteVector​(byte[] arr,
                                    int offset,
                                    int length)
        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

        public int createByteVector​(java.nio.ByteBuffer byteBuffer)
        Create a byte array in the buffer. The source ByteBuffer position is advanced by Buffer.remaining() places after this call.
        Parameters:
        byteBuffer - A source ByteBuffer with data.
        Returns:
        The offset in the buffer where the encoded array 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.
      • startTable

        public void startTable​(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.
      • endTable

        public int endTable()
        Finish off writing the object that is under construction.
        Returns:
        The offset to the object inside dataBuffer().
        See Also:
        startTable(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

        protected void finish​(int root_table,
                              boolean size_prefix)
        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

        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.
      • finishSizePrefixed

        public void finishSizePrefixed​(int root_table)
        Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
        Parameters:
        root_table - An offset to be added to the buffer.
      • finish

        protected void finish​(int root_table,
                              java.lang.String file_identifier,
                              boolean size_prefix)
        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.
      • finish

        public void finish​(int root_table,
                           java.lang.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`.
      • finishSizePrefixed

        public void finishSizePrefixed​(int root_table,
                                       java.lang.String file_identifier)
        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

        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 java.nio.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:
        java.lang.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.
      • sizedInputStream

        public java.io.InputStream sizedInputStream()
        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.