Class ByteStringBuilder
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Maximum size in bytes of a compact encoded value. -
Constructor Summary
ConstructorDescriptionCreates a new byte string builder with an initial capacity of 32 bytes.ByteStringBuilder
(int capacity) Creates a new byte string builder with the specified initial capacity.Creates a new byte string builder with the content of the providedByteString
.Creates a new byte string builder with the content of the provided byte string builder. -
Method Summary
Modifier and TypeMethodDescriptionappendBerLength
(int length) Appends the ASN.1 BER length encoding representation of the provided integer to this byte string builder.appendByte
(int b) Appends the provided byte to this byte string builder.appendBytes
(byte[] bytes) Appends the provided byte array to this byte string builder.appendBytes
(byte[] bytes, int offset, int length) Appends the provided byte array to this byte string builder.void
appendBytes
(DataInput stream, int length) Appends the providedDataInput
to this byte string builder.int
appendBytes
(InputStream stream, int length) Appends the providedInputStream
to this byte string builder.appendBytes
(ByteBuffer buffer) Appends the providedByteBuffer
to this byte string builder.appendBytes
(ByteBuffer buffer, int length) Appends the providedByteBuffer
to this byte string builder.appendBytes
(ByteString bytes) Appends the providedByteString
to this byte string builder.appendBytes
(ByteStringReader reader, int length) Appends the providedByteStringReader
to this byte string builder.appendCompactUnsigned
(long value) Appends the compact encoded bytes of the provided unsigned long to this byte string builder.appendInt
(int i) Appends the big-endian encoded bytes of the provided integer to this byte string builder.appendLong
(long l) Appends the big-endian encoded bytes of the provided long to this byte string builder.Appends the byte string representation of the provided object to this byte string builder.appendShort
(int i) Appends the big-endian encoded bytes of the provided short to this byte string builder.appendUtf8
(char[] chars) Appends the UTF-8 encoded bytes of the provided char array to this byte string builder.appendUtf8
(String s) Appends the UTF-8 encoded bytes of the provided string to this byte string builder.void
close()
Implementation ofOutputStream.close()
which doesn't do anything because data is written directly to a byte array.void
flush()
Implementation ofOutputStream.flush()
which doesn't do anything because data is written directly to a byte array.boolean
isEmpty()
Returnstrue
if this byte string builder has a length of zero.int
length()
Returns the length of this byte string builder.setByte
(int index, byte b) Sets the byte value at the specified index.setLength
(int newLength) Sets the length of this byte string builder.byte[]
Returns a byte array containing the contents of this byte string builder.Returns a modifiableByteBuffer
containing the contents of this byte string builder.Returns aByteString
containing the contents of this byte string builder.toString()
Returns the UTF-8 decoded string representation of this byte string builder.void
write
(byte[] bytes) Appends the provided bytes to this byte string builder.void
write
(byte[] bytes, int i, int i1) Appends the provided bytes to this byte string builder.void
write
(int i) Appends the provided byte to this byte string builder.Methods inherited from class java.io.OutputStream
nullOutputStream
-
Field Details
-
MAX_COMPACT_SIZE
public static final int MAX_COMPACT_SIZEMaximum size in bytes of a compact encoded value.- See Also:
-
-
Constructor Details
-
ByteStringBuilder
public ByteStringBuilder()Creates a new byte string builder with an initial capacity of 32 bytes. -
ByteStringBuilder
public ByteStringBuilder(int capacity) Creates a new byte string builder with the specified initial capacity.- Parameters:
capacity
- The initial capacity.- Throws:
IllegalArgumentException
- If thecapacity
is negative.
-
ByteStringBuilder
Creates a new byte string builder with the content of the providedByteString
. Its capacity is set to the length of the providedByteString
.- Parameters:
bs
- TheByteString
to copy
-
ByteStringBuilder
Creates a new byte string builder with the content of the provided byte string builder. Its capacity is set to the capacity of the provided byte string builder.- Parameters:
bs
- TheByteStringBuilder
to copy.
-
-
Method Details
-
appendByte
Appends the provided byte to this byte string builder.Note: this method accepts an
int
for ease of reading and writing.This method only keeps the lowest 8-bits of the provided
int
. Higher bits will be truncated. This method performs the equivalent of:int i = ...; int i8bits = i & 0xFF; // only use "i8bits"
int i = ...; byte b = (byte) i; // only use "b"
- Parameters:
b
- The byte to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
Appends the provided byte array to this byte string builder.An invocation of the form:
src.append(bytes)
Behaves in exactly the same way as the invocation:src.append(bytes, 0, bytes.length);
- Parameters:
bytes
- The byte array to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
Appends the provided byte array to this byte string builder.- Parameters:
bytes
- The byte array to be appended to this byte string builder.offset
- The offset of the byte array to be used; must be non-negative and no larger thanbytes.length
.length
- The length of the byte array to be used; must be non-negative and no larger thanbytes.length - offset
.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException
- Ifoffset
is negative or iflength
is negative or ifoffset + length
is greater thanbytes.length
.
-
appendBytes
Appends the providedByteBuffer
to this byte string builder.- Parameters:
buffer
- The byte buffer to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
Appends the providedByteBuffer
to this byte string builder.- Parameters:
buffer
- The byte buffer to be appended to this byte string builder.length
- The number of bytes to be appended frombuffer
.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException
- Iflength
is less than zero or greater thanbuffer.remaining()
.
-
appendBytes
Appends the providedByteString
to this byte string builder.- Parameters:
bytes
- The byte string to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
Appends the providedByteStringReader
to this byte string builder.- Parameters:
reader
- The byte string reader to be appended to this byte string builder.length
- The number of bytes to be appended fromreader
.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException
- Iflength
is less than zero or greater thanreader.remaining()
.
-
appendUtf8
Appends the UTF-8 encoded bytes of the provided char array to this byte string builder.- Parameters:
chars
- The char array whose UTF-8 encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
Appends the providedDataInput
to this byte string builder.- Parameters:
stream
- The data input stream to be appended to this byte string builder.length
- The maximum number of bytes to be appended fromstream
.- Throws:
IndexOutOfBoundsException
- Iflength
is less than zero.EOFException
- If this stream reaches the end before reading all the bytes.IOException
- If an I/O error occurs.
-
appendBytes
Appends the providedInputStream
to this byte string builder.- Parameters:
stream
- The input stream to be appended to this byte string builder.length
- The maximum number of bytes to be appended frombuffer
.- Returns:
- The number of bytes read from the input stream, or
-1
if the end of the input stream has been reached. - Throws:
IndexOutOfBoundsException
- Iflength
is less than zero.IOException
- If an I/O error occurs.
-
appendInt
Appends the big-endian encoded bytes of the provided integer to this byte string builder.- Parameters:
i
- The integer whose big-endian encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendLong
Appends the big-endian encoded bytes of the provided long to this byte string builder.- Parameters:
l
- The long whose big-endian encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendCompactUnsigned
Appends the compact encoded bytes of the provided unsigned long to this byte string builder. This method allows to encode unsigned long up to 56 bits using fewer bytes (from 1 to 8) than append(long). The encoding has the important property that it preserves ordering, so it can be used for keys.- Parameters:
value
- The long whose compact encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendObject
Appends the byte string representation of the provided object to this byte string builder. The object is converted to a byte string as follows:- if the object is an instance of
ByteString
then this method is equivalent to callingappendBytes(ByteString)
- if the object is a
byte[]
then this method is equivalent to callingappendBytes(byte[])
- if the object is a
char[]
then this method is equivalent to callingappendUtf8(char[])
- for all other types of object this method is equivalent to calling
appendUtf8(String)
with thetoString()
representation of the provided object.
Long
andInteger
objects like any other type ofObject
. More specifically, the following invocations are not equivalent:append(0)
is not equivalent toappend((Object) 0)
append(0L)
is not equivalent toappend((Object) 0L)
- Parameters:
o
- The object to be appended to this byte string builder.- Returns:
- This byte string builder.
- if the object is an instance of
-
appendShort
Appends the big-endian encoded bytes of the provided short to this byte string builder.Note: this method accepts an
int
for ease of reading and writing.This method only keeps the lowest 16-bits of the provided
int
. Higher bits will be truncated. This method performs the equivalent of:int i = ...; int i16bits = i & 0xFFFF; // only use "i16bits"
int i = ...; short s = (short) i; // only use "s"
- Parameters:
i
- The short whose big-endian encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendUtf8
Appends the UTF-8 encoded bytes of the provided string to this byte string builder.- Parameters:
s
- The string whose UTF-8 encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBerLength
Appends the ASN.1 BER length encoding representation of the provided integer to this byte string builder.- Parameters:
length
- The value to encode using the BER length encoding rules.- Returns:
- This byte string builder.
-
write
public void write(byte[] bytes) Appends the provided bytes to this byte string builder. Equivalent toappendBytes(byte[])
.- Overrides:
write
in classOutputStream
-
write
public void write(byte[] bytes, int i, int i1) Appends the provided bytes to this byte string builder. Equivalent toappendBytes(byte[], int, int)
.- Overrides:
write
in classOutputStream
-
write
public void write(int i) Appends the provided byte to this byte string builder. Equivalent toappendByte(int)
.- Specified by:
write
in classOutputStream
-
flush
public void flush()Implementation ofOutputStream.flush()
which doesn't do anything because data is written directly to a byte array.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
-
close
public void close()Implementation ofOutputStream.close()
which doesn't do anything because data is written directly to a byte array.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
-
isEmpty
public boolean isEmpty()Returnstrue
if this byte string builder has a length of zero.- Returns:
true
if this byte string builder has a length of zero.
-
length
public int length()Returns the length of this byte string builder.- Returns:
- The length of this byte string builder.
-
setByte
Sets the byte value at the specified index.An index ranges from zero to
length() - 1
. The first byte value of the byte string is at index zero, the next at index one, and so on, as for array indexing.- Parameters:
index
- The index of the byte to be set.b
- The byte to set on this byte string builder.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException
- If the index argument is negative or not less than length().
-
setLength
Sets the length of this byte string builder.If the
newLength
argument is less than the current length, the length is changed to the specified length.If the
newLength
argument is greater than or equal to the current length, then the capacity is increased and sufficient null bytes are appended so that length becomes thenewLength
argument.The
newLength
argument must be greater than or equal to0
.- Parameters:
newLength
- The new length.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException
- If thenewLength
argument is negative.
-
toByteArray
public byte[] toByteArray()Returns a byte array containing the contents of this byte string builder. On return, this byte string builder will be reset to a capacity of0
. Subsequent changes to this byte string builder will not impact the returned byte array and vice versa.- Returns:
- A byte array containing the contents of this byte string builder.
-
toByteString
Returns aByteString
containing the contents of this byte string builder. On return, this byte string builder will be reset to a capacity of0
. Subsequent changes to this byte string builder will not impact the returnedByteString
.- Returns:
- The
ByteString
containing the contents of this byte string builder.
-
toByteBuffer
Returns a modifiableByteBuffer
containing the contents of this byte string builder. On return, this byte string builder will be reset to a capacity of0
. Subsequent changes to this byte string builder will not impact the returnedByteBuffer
and vice versa.- Returns:
- The
ByteBuffer
containing the contents of this byte string builder.
-
toString
Returns the UTF-8 decoded string representation of this byte string builder. If UTF-8 decoding fails, the platform's default encoding will be used.
-