public class BinaryBuilder extends Object
BinaryBuilder
is similar in function to StringBuilder
,
although it does not allow mutation of previously written data.Constructor and Description |
---|
BinaryBuilder(int initialSize)
Creates a new buffer sized as specified.
|
Modifier and Type | Method and Description |
---|---|
void |
append(byte value)
Appends a byte to the buffer.
|
void |
append(byte[] values)
Appends a sequence of bytes to the buffer.
|
void |
append(byte[] values,
int offset,
int length)
Appends a subsequence of bytes to the buffer.
|
void |
append(char data)
Appends a character value to the buffer.
|
void |
append(CharSequence values)
Appends a sequence of character values to the buffer.
|
void |
append(double value)
Appends a a double value to the buffer.
|
void |
append(float value)
Appends a float value to the buffer.
|
void |
append(int value)
Appends an int value to the buffer.
|
void |
append(long value)
Appends a long value to the buffer.
|
void |
appendBase128(int value)
Appends an int value to the buffer, using
variable length base-128 encoding.
|
void |
appendBase128(long value)
Appends a long value to the buffer, using
variable length base-128 encoding.
|
void |
appendSingleByte(CharSequence values)
Appends a sequence of character values to the buffer using
only a single byte per character.
|
void |
appendUnsignedBase128(int value)
Appends an int value to the buffer, using
variable length base-128 encoding with
bias towards non-negative values.
|
void |
appendUnsignedBase128(long value)
Appends a long value to the buffer, using
variable length base-128 encoding with
bias towards non-negative values.
|
static int |
base128Size(int value)
Computes the length of the base-128 encoding of an
int value.
|
static int |
base128Size(long value)
Computes the length of the base-128 encoding of an
long value.
|
void |
copy(byte[] dest,
int offset)
Extracts the current contents of the builder to the
specified array at the given offset.
|
static void |
encodeInt32(int value,
byte[] data,
int offset)
Writes a 32-bit integer value into the specified position in
the given array.
|
static void |
encodeUnsignedBase128(long value,
byte[] data,
int offset)
Writes a 64-bit integer value using base-128 encoding into the specified
position in the given array.
|
void |
reset()
Resets the buffer to empty.
|
void |
set(int position,
int data)
Sets 4 bytes beginning at the specified position
to the given value.
|
int |
size()
Gets the current size of the binary sequence
represented by the builder.
|
byte[] |
toBinary()
Gets the current contents of the builder.
|
ByteBuffer |
toByteBuffer()
Gets a
ByteBuffer that wraps the internal bytes storage
of this builder object. |
String |
toString() |
static int |
unsignedBase128Size(int value)
Computes the length of the non-negative biased base-128 encoding
of an int value.
|
static int |
unsignedBase128Size(long value)
Computes the length of the non-negative biased base-128 encoding
of an long value.
|
void |
writeToStream(OutputStream out)
Writes the current contents of the builder to the
specified
OutputStream . |
public BinaryBuilder(int initialSize)
initialSize
- the size, in bytes, of the
internal storage bufferpublic int size()
public void set(int position, int data)
position
- the byte offset at which to
write the updatedata
- the 4 byte value to writepublic void append(byte value)
value
- the byte to appendpublic void append(byte[] values)
This is equivalent to calling append(values, 0, values.length)
.
values
- the bytes to appendpublic void append(byte[] values, int offset, int length)
values
- the sequence containing the bytes to appendoffset
- the starting position of the subsequencelength
- the length of the subsequencepublic void append(char data)
data
- the character to appendpublic void append(CharSequence values)
Functionally equivalent to calling append(char)
on
every element in the sequence, though slightly more efficient.
values
- the character values to appendpublic void appendSingleByte(CharSequence values)
values
- the character values to appendpublic void append(int value)
value
- the value to appendpublic void appendUnsignedBase128(int value)
appendBase128(int)
produces.
Negative values will always encode to the
maximum length.
Base-128 can produce more compact encodings, with the size being proportional to the size of the value. Encodings will vary between 1 and 5 bytes, depending on the value encoded.
value
- the unsigned value to append
in base-128public void appendBase128(int value)
Base-128 can produce more compact encodings, with the size being proportional to the size of the value. Encodings will vary between 1 and 5 bytes, depending on the value encoded.
value
- the signed value to append
in base-128public void append(long value)
value
- the value to appendpublic void appendUnsignedBase128(long value)
appendBase128(long)
produces.
Negative values will always encode to the
maximum length.
Base-128 can produce more compact encodings, with the size being proportional to the size of the value. Encodings will vary between 1 and 10 bytes, depending on the value encoded.
value
- the unsigned value to append
in base-128public void appendBase128(long value)
Base-128 can produce more compact encodings, with the size being proportional to the size of the value. Encodings will vary between 1 and 10 bytes, depending on the value encoded.
value
- the signed value to append
in base-128public void append(float value)
Float.floatToRawIntBits(float)
.value
- the value to appendpublic void append(double value)
Double.doubleToRawLongBits(double)
.value
- the value to appendpublic byte[] toBinary()
public ByteBuffer toByteBuffer()
ByteBuffer
that wraps the internal bytes storage
of this builder object. The byte buffer will wrap from the start
of the internal byte storage to its current size.public void copy(byte[] dest, int offset)
dest
- the target array into which to copyoffset
- the offset into the target array at
which to startpublic void writeToStream(OutputStream out) throws IOException
OutputStream
. This is more efficient
than calling out.write(toBinary())
, as it
avoids making an extra copy of the data.out
- the target to which to write the buffer contentsIOException
- if an error occurs during the writepublic void reset()
public static final int base128Size(int value)
value
- the value for which to determine the encoding sizeappendBase128(int)
public static final int unsignedBase128Size(int value)
value
- the value for which to determine the encoding sizeappendUnsignedBase128(int)
public static final int base128Size(long value)
value
- the value for which to determine the encoding sizeappendBase128(long)
public static final int unsignedBase128Size(long value)
value
- the value for which to determine the encoding sizeappendUnsignedBase128(int)
public static final void encodeInt32(int value, byte[] data, int offset)
value
- the value to writedata
- the buffer to updateoffset
- the byte offset at which to writepublic static final void encodeUnsignedBase128(long value, byte[] data, int offset)
value
- the value to write in base-128data
- the buffer to updateoffset
- the byte offset at which to writeCopyright © 2021 Actian Corporation. All rights reserved.