Package io.objectbox.flatbuffers
Class FlexBuffersBuilder
java.lang.Object
io.objectbox.flatbuffers.FlexBuffersBuilder
Helper class that builds FlexBuffers
This class presents all necessary APIs to create FlexBuffers. A `ByteBuffer` will be used to store the data. It can be created internally, or passed down in the constructor.
There are some limitations when compared to original implementation in C++. Most notably:
No support for mutations (might change in the future).
Buffer size limited to
Integer.MAX_VALUESince Java does not support unsigned type, all unsigned operations accepts an immediate higher representation of similar type.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intNo keys or strings will be sharedstatic final intReserved for the future.static final intReserved for the future.static final intKeys will be shared between elements.static final intStrings and keys will be shared between elements.static final intStrings will be shared between elements. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newly allocatedFlexBuffersBuilderwithBUILDER_FLAG_SHARE_KEYSset.FlexBuffersBuilder(int bufSize) Constructs a newly allocatedFlexBuffersBuilderwithBUILDER_FLAG_SHARE_KEYSset.FlexBuffersBuilder(ReadWriteBuf bb, int flags) Constructs a newly allocatedFlexBuffersBuilder.FlexBuffersBuilder(ByteBuffer bb, int flags) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Reset the FlexBuffersBuilder by purging all data that it holds.intFinishes a map, but writing the information in the bufferintFinishes a vector, but writing the information in the bufferfinish()Finish writing the message into the buffer.Return `ByteBuffer` containing FlexBuffer message.intputBlob(byte[] value) Adds a byte array into the messageintAdds a byte array into the messagevoidputBoolean(boolean val) Insert a single boolean into the buffervoidputBoolean(String key, boolean val) Insert a single boolean into the buffervoidputFloat(double value) Adds a 64-bit float into the buff.voidputFloat(float value) Adds a 32-bit float into the buff.voidAdds a 64-bit float into the buff.voidAdds a 32-bit float into the buff.voidputInt(int val) Adds a integer into the buffvoidputInt(long value) Adds a 64-bit integer into the buffvoidAdds a integer into the buffvoidAdds a integer into the buffvoidputNull()Insert a null value into the buffervoidInsert a null value into the bufferintAdds a String into the bufferintAdds a String into the buffervoidputUInt(int value) Adds a unsigned integer into the buff.voidputUInt(long value) Adds a unsigned integer (stored in a signed 64-bit integer) into the buff.voidputUInt64(BigInteger value) Adds a 64-bit unsigned integer (stored asBigInteger) into the buff.intstartMap()Start a new map in the buffer.intStart a new vector in the buffer.
-
Field Details
-
BUILDER_FLAG_NONE
public static final int BUILDER_FLAG_NONENo keys or strings will be shared- See Also:
-
BUILDER_FLAG_SHARE_KEYS
public static final int BUILDER_FLAG_SHARE_KEYSKeys will be shared between elements. Identical keys will only be serialized once, thus possibly saving space. But serialization performance might be slower and consumes more memory.- See Also:
-
BUILDER_FLAG_SHARE_STRINGS
public static final int BUILDER_FLAG_SHARE_STRINGSStrings will be shared between elements. Identical strings will only be serialized once, thus possibly saving space. But serialization performance might be slower and consumes more memory. This is ideal if you expect many repeated strings on the message.- See Also:
-
BUILDER_FLAG_SHARE_KEYS_AND_STRINGS
public static final int BUILDER_FLAG_SHARE_KEYS_AND_STRINGSStrings and keys will be shared between elements.- See Also:
-
BUILDER_FLAG_SHARE_KEY_VECTORS
public static final int BUILDER_FLAG_SHARE_KEY_VECTORSReserved for the future.- See Also:
-
BUILDER_FLAG_SHARE_ALL
public static final int BUILDER_FLAG_SHARE_ALLReserved for the future.- See Also:
-
-
Constructor Details
-
FlexBuffersBuilder
public FlexBuffersBuilder(int bufSize) Constructs a newly allocatedFlexBuffersBuilderwithBUILDER_FLAG_SHARE_KEYSset.- Parameters:
bufSize- size of buffer in bytes.
-
FlexBuffersBuilder
public FlexBuffersBuilder()Constructs a newly allocatedFlexBuffersBuilderwithBUILDER_FLAG_SHARE_KEYSset. -
FlexBuffersBuilder
Deprecated.Constructs a newly allocatedFlexBuffersBuilder.- Parameters:
bb- `ByteBuffer` that will hold the messageflags- Share flags
-
FlexBuffersBuilder
-
FlexBuffersBuilder
Constructs a newly allocatedFlexBuffersBuilder. By default same keys will be serialized only once- Parameters:
bb- `ByteBuffer` that will hold the message
-
-
Method Details
-
clear
public void clear()Reset the FlexBuffersBuilder by purging all data that it holds. -
getBuffer
Return `ByteBuffer` containing FlexBuffer message.#finish()must be called before calling this function otherwise an assert will trigger.- Returns:
- `ByteBuffer` with finished message
-
putNull
public void putNull()Insert a null value into the buffer -
putNull
Insert a null value into the buffer- Parameters:
key- key used to store element in map
-
putBoolean
public void putBoolean(boolean val) Insert a single boolean into the buffer- Parameters:
val- true or false
-
putBoolean
Insert a single boolean into the buffer- Parameters:
key- key used to store element in mapval- true or false
-
putInt
public void putInt(int val) Adds a integer into the buff- Parameters:
val- integer
-
putInt
Adds a integer into the buff- Parameters:
key- key used to store element in mapval- integer
-
putInt
Adds a integer into the buff- Parameters:
key- key used to store element in mapval- 64-bit integer
-
putInt
public void putInt(long value) Adds a 64-bit integer into the buff- Parameters:
value- integer
-
putUInt
public void putUInt(int value) Adds a unsigned integer into the buff.- Parameters:
value- integer representing unsigned value
-
putUInt
public void putUInt(long value) Adds a unsigned integer (stored in a signed 64-bit integer) into the buff.- Parameters:
value- integer representing unsigned value
-
putUInt64
Adds a 64-bit unsigned integer (stored asBigInteger) into the buff. Warning: This operation might be very slow.- Parameters:
value- integer representing unsigned value
-
putFloat
public void putFloat(float value) Adds a 32-bit float into the buff.- Parameters:
value- float representing value
-
putFloat
Adds a 32-bit float into the buff.- Parameters:
key- key used to store element in mapvalue- float representing value
-
putFloat
public void putFloat(double value) Adds a 64-bit float into the buff.- Parameters:
value- float representing value
-
putFloat
Adds a 64-bit float into the buff.- Parameters:
key- key used to store element in mapvalue- float representing value
-
putString
Adds a String into the buffer- Parameters:
value- string- Returns:
- start position of string in the buffer
-
putString
Adds a String into the buffer- Parameters:
key- key used to store element in mapvalue- string- Returns:
- start position of string in the buffer
-
putBlob
public int putBlob(byte[] value) Adds a byte array into the message- Parameters:
value- byte array- Returns:
- position in buffer as the start of byte array
-
putBlob
Adds a byte array into the message- Parameters:
key- key used to store element in mapvalue- byte array- Returns:
- position in buffer as the start of byte array
-
startVector
public int startVector()Start a new vector in the buffer.- Returns:
- a reference indicating position of the vector in buffer. This reference must be passed along when the vector is finished using endVector()
-
endVector
Finishes a vector, but writing the information in the buffer- Parameters:
key- key used to store element in mapstart- reference for beginning of the vector. Returned bystartVector()typed- boolean indicating whether vector is typedfixed- boolean indicating whether vector is fixed- Returns:
- Reference to the vector
-
finish
Finish writing the message into the buffer. After that no other element must be inserted into the buffer. Also, you must call this function before start using the FlexBuffer message- Returns:
- `ByteBuffer` containing the FlexBuffer message
-
startMap
public int startMap()Start a new map in the buffer.- Returns:
- a reference indicating position of the map in buffer. This reference must be passed along when the map is finished using endMap()
-
endMap
Finishes a map, but writing the information in the buffer- Parameters:
key- key used to store element in mapstart- reference for beginning of the map. Returned bystartMap()- Returns:
- Reference to the map
-