Package io.objectbox.flatbuffers
Class FlexBuffersBuilder
- java.lang.Object
-
- io.objectbox.flatbuffers.FlexBuffersBuilder
-
public class FlexBuffersBuilder extends java.lang.ObjectHelper class that builds FlexBuffersThis 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFlexBuffersBuilder.Value
-
Field Summary
Fields Modifier and Type Field Description private ReadWriteBufbbstatic intBUILDER_FLAG_NONENo keys or strings will be sharedstatic intBUILDER_FLAG_SHARE_ALLReserved for the future.static intBUILDER_FLAG_SHARE_KEY_VECTORSReserved for the future.static intBUILDER_FLAG_SHARE_KEYSKeys will be shared between elements.static intBUILDER_FLAG_SHARE_KEYS_AND_STRINGSStrings and keys will be shared between elements.static intBUILDER_FLAG_SHARE_STRINGSStrings will be shared between elements.private booleanfinishedprivate intflagsprivate java.util.Comparator<FlexBuffersBuilder.Value>keyComparatorprivate java.util.HashMap<java.lang.String,java.lang.Integer>keyPoolprivate java.util.ArrayList<FlexBuffersBuilder.Value>stackprivate java.util.HashMap<java.lang.String,java.lang.Integer>stringPoolprivate static intWIDTH_16private static intWIDTH_32private static intWIDTH_64private static intWIDTH_8
-
Constructor Summary
Constructors Constructor Description FlexBuffersBuilder()Constructs a newly allocatedFlexBuffersBuilderwithBUILDER_FLAG_SHARE_KEYSset.FlexBuffersBuilder(int bufSize)Constructs a newly allocatedFlexBuffersBuilderwithBUILDER_FLAG_SHARE_KEYSset.FlexBuffersBuilder(ReadWriteBuf bb, int flags)FlexBuffersBuilder(java.nio.ByteBuffer bb)Constructs a newly allocatedFlexBuffersBuilder.FlexBuffersBuilder(java.nio.ByteBuffer bb, int flags)Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private intalign(int alignment)voidclear()Reset the FlexBuffersBuilder by purging all data that it holds.private FlexBuffersBuilder.ValuecreateKeyVector(int start, int length)private FlexBuffersBuilder.ValuecreateVector(int key, int start, int length, boolean typed, boolean fixed, FlexBuffersBuilder.Value keys)intendMap(java.lang.String key, int start)Finishes a map, but writing the information in the bufferintendVector(java.lang.String key, int start, boolean typed, boolean fixed)Finishes a vector, but writing the information in the bufferjava.nio.ByteBufferfinish()Finish writing the message into the buffer.ReadWriteBufgetBuffer()Return `ByteBuffer` containing FlexBuffer message.intputBlob(byte[] value)Adds a byte array into the messageintputBlob(java.lang.String key, byte[] val)Adds a byte array into the messagevoidputBoolean(boolean val)Insert a single boolean into the buffervoidputBoolean(java.lang.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.voidputFloat(java.lang.String key, double val)Adds a 64-bit float into the buff.voidputFloat(java.lang.String key, float val)Adds 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 buffvoidputInt(java.lang.String key, int val)Adds a integer into the buffvoidputInt(java.lang.String key, long val)Adds a integer into the buffprivate intputKey(java.lang.String key)voidputNull()Insert a null value into the buffervoidputNull(java.lang.String key)Insert a null value into the bufferintputString(java.lang.String value)Adds a String into the bufferintputString(java.lang.String key, java.lang.String val)Adds 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.private voidputUInt(java.lang.String key, long value)private voidputUInt64(java.lang.String key, long value)voidputUInt64(java.math.BigInteger value)Adds a 64-bit unsigned integer (stored asBigInteger) into the buff.intstartMap()Start a new map in the buffer.intstartVector()Start a new vector in the buffer.(package private) static intwidthUInBits(long len)private voidwriteAny(FlexBuffersBuilder.Value val, int byteWidth)private FlexBuffersBuilder.ValuewriteBlob(int key, byte[] blob, int type, boolean trailing)private voidwriteDouble(double val, int byteWidth)private voidwriteInt(long value, int byteWidth)private voidwriteOffset(long val, int byteWidth)private FlexBuffersBuilder.ValuewriteString(int key, java.lang.String s)
-
-
-
Field Detail
-
BUILDER_FLAG_NONE
public static final int BUILDER_FLAG_NONE
No keys or strings will be shared- See Also:
- Constant Field Values
-
BUILDER_FLAG_SHARE_KEYS
public static final int BUILDER_FLAG_SHARE_KEYS
Keys 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:
- Constant Field Values
-
BUILDER_FLAG_SHARE_STRINGS
public static final int BUILDER_FLAG_SHARE_STRINGS
Strings 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:
- Constant Field Values
-
BUILDER_FLAG_SHARE_KEYS_AND_STRINGS
public static final int BUILDER_FLAG_SHARE_KEYS_AND_STRINGS
Strings and keys will be shared between elements.- See Also:
- Constant Field Values
-
BUILDER_FLAG_SHARE_KEY_VECTORS
public static final int BUILDER_FLAG_SHARE_KEY_VECTORS
Reserved for the future.- See Also:
- Constant Field Values
-
BUILDER_FLAG_SHARE_ALL
public static final int BUILDER_FLAG_SHARE_ALL
Reserved for the future.- See Also:
- Constant Field Values
-
WIDTH_8
private static final int WIDTH_8
- See Also:
- Constant Field Values
-
WIDTH_16
private static final int WIDTH_16
- See Also:
- Constant Field Values
-
WIDTH_32
private static final int WIDTH_32
- See Also:
- Constant Field Values
-
WIDTH_64
private static final int WIDTH_64
- See Also:
- Constant Field Values
-
bb
private final ReadWriteBuf bb
-
stack
private final java.util.ArrayList<FlexBuffersBuilder.Value> stack
-
keyPool
private final java.util.HashMap<java.lang.String,java.lang.Integer> keyPool
-
stringPool
private final java.util.HashMap<java.lang.String,java.lang.Integer> stringPool
-
flags
private final int flags
-
finished
private boolean finished
-
keyComparator
private java.util.Comparator<FlexBuffersBuilder.Value> keyComparator
-
-
Constructor Detail
-
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 public FlexBuffersBuilder(java.nio.ByteBuffer bb, int flags)Deprecated.Constructs a newly allocatedFlexBuffersBuilder.- Parameters:
bb- `ByteBuffer` that will hold the messageflags- Share flags
-
FlexBuffersBuilder
public FlexBuffersBuilder(ReadWriteBuf bb, int flags)
-
FlexBuffersBuilder
public FlexBuffersBuilder(java.nio.ByteBuffer bb)
Constructs a newly allocatedFlexBuffersBuilder. By default same keys will be serialized only once- Parameters:
bb- `ByteBuffer` that will hold the message
-
-
Method Detail
-
clear
public void clear()
Reset the FlexBuffersBuilder by purging all data that it holds.
-
getBuffer
public ReadWriteBuf 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
public void putNull(java.lang.String key)
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
public void putBoolean(java.lang.String key, boolean val)Insert a single boolean into the buffer- Parameters:
key- key used to store element in mapval- true or false
-
putKey
private int putKey(java.lang.String key)
-
putInt
public void putInt(int val)
Adds a integer into the buff- Parameters:
val- integer
-
putInt
public void putInt(java.lang.String key, int val)Adds a integer into the buff- Parameters:
key- key used to store element in mapval- integer
-
putInt
public void putInt(java.lang.String key, long val)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
public void putUInt64(java.math.BigInteger value)
Adds a 64-bit unsigned integer (stored asBigInteger) into the buff. Warning: This operation might be very slow.- Parameters:
value- integer representing unsigned value
-
putUInt64
private void putUInt64(java.lang.String key, long value)
-
putUInt
private void putUInt(java.lang.String key, long value)
-
putFloat
public void putFloat(float value)
Adds a 32-bit float into the buff.- Parameters:
value- float representing value
-
putFloat
public void putFloat(java.lang.String key, float val)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
public void putFloat(java.lang.String key, double val)Adds a 64-bit float into the buff.- Parameters:
key- key used to store element in mapvalue- float representing value
-
putString
public int putString(java.lang.String value)
Adds a String into the buffer- Parameters:
value- string- Returns:
- start position of string in the buffer
-
putString
public int putString(java.lang.String key, java.lang.String val)Adds a String into the buffer- Parameters:
key- key used to store element in mapvalue- string- Returns:
- start position of string in the buffer
-
writeString
private FlexBuffersBuilder.Value writeString(int key, java.lang.String s)
-
widthUInBits
static int widthUInBits(long len)
-
writeBlob
private FlexBuffersBuilder.Value writeBlob(int key, byte[] blob, int type, boolean trailing)
-
align
private int align(int alignment)
-
writeInt
private void writeInt(long value, int byteWidth)
-
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
public int putBlob(java.lang.String key, byte[] val)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
public int endVector(java.lang.String key, int start, boolean typed, boolean fixed)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
public java.nio.ByteBuffer 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
-
createVector
private FlexBuffersBuilder.Value createVector(int key, int start, int length, boolean typed, boolean fixed, FlexBuffersBuilder.Value keys)
-
writeOffset
private void writeOffset(long val, int byteWidth)
-
writeAny
private void writeAny(FlexBuffersBuilder.Value val, int byteWidth)
-
writeDouble
private void writeDouble(double val, int byteWidth)
-
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
public int endMap(java.lang.String key, int start)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
-
createKeyVector
private FlexBuffersBuilder.Value createKeyVector(int start, int length)
-
-