public class ByteStringBuilder extends Object
StringBuilder
.
This class is not thread-safe.
Constructor and Description |
---|
ByteStringBuilder()
Constructs a new empty
ByteStringBuilder . |
ByteStringBuilder(int capacity)
Constructs a new empty
ByteStringBuilder that can hold at least capacity
bytes. |
Modifier and Type | Method and Description |
---|---|
ByteStringBuilder |
append(byte b)
Appends the byte
b . |
ByteStringBuilder |
append(byte[] array)
Appends the contents of the byte array
array . |
ByteStringBuilder |
append(byte[] array,
int off,
int len)
Appends the contents of the byte array
array . |
ByteStringBuilder |
append(ByteString string)
Appends the contents of the byte string
string . |
ByteStringBuilder |
append(CharSequence charSequence)
Appends the char sequence
charSequence interpreted as a sequence
of bytes using the virtual machine's default charset (see Charset.defaultCharset() ). |
ByteStringBuilder |
append(CharSequence charSequence,
Charset charset)
Appends a char sequence
charSequence interpreted as a sequence
of bytes using the specified Charset . |
int |
capacity()
Returns the current capacity of the builder.
|
int |
length()
Returns the number of bytes in this builder.
|
void |
setByteAt(int index,
byte value)
Sets the byte at position
index to value . |
void |
setLength(int newLength)
Sets the number of bytes in this builder to
newLength . |
ByteString |
toByteString()
Returns a byte string consisting of the bytes in this builder.
|
String |
toString()
Returns the interpretation of this builder's bytes as a
java.lang.String . |
void |
trimToSize()
Attempts to reduce the memory consumption of this builder by reducing its capacity
to a smaller, yet still sufficient value.
|
public ByteStringBuilder(int capacity)
ByteStringBuilder
that can hold at least capacity
bytes.capacity
- the initial required capacity, must not be negativeIllegalArgumentException
- if capacity
is negativepublic ByteStringBuilder()
ByteStringBuilder
.public int capacity()
public int length()
public void setLength(int newLength)
newLength
.
When newLength
is lesser than the current length len
,
drops the last (len - newLength)
bytes from the constructed sequence.
If newLength
is greater than len
, appends newLength - len
zero bytes.
No memory is freed when reducing the length of the sequence.
newLength
- the new length, must not be negativeIndexOutOfBoundsException
- if newLength
is negativepublic void trimToSize()
public void setByteAt(int index, byte value)
index
to value
.index
- the index of the byte to setvalue
- the new value of the byte at position index
IndexOutOfBoundsException
- if index
is negative or greater than or equal
to the current buffer lengthpublic ByteStringBuilder append(byte b)
b
.b
- the byte to appendpublic ByteStringBuilder append(byte[] array, int off, int len)
array
. off
is the offset in array
to start the write from, and len
is the
number of bytes that should be written.
Throws an IndexOutOfBoundsException
if off
or len
is negative, or if (off + len)
is greater than array.length
.
array
- the byte array, must not be null
off
- offset in array
to start fromlen
- number of bytes to writeNullPointerException
- if array
is null
IndexOutOfBoundsException
- if off
or len
is negative, or if
(off + len)
is greater than array.length
public ByteStringBuilder append(byte[] array)
array
.array
- the byte array to append, must not be null
NullPointerException
- if array
is null
public ByteStringBuilder append(ByteString string)
string
.string
- the byte string to append, must not be null
NullPointerException
- if string
is null
public ByteStringBuilder append(CharSequence charSequence, Charset charset)
charSequence
interpreted as a sequence
of bytes using the specified Charset
.charSequence
- the char sequence to append, must not be null
charset
- the charset to use for encoding, must not be null
NullPointerException
- if string
is null
IllegalArgumentException
- if charset
cannot does not provide encoding
capability (see Charset.canEncode()
)public ByteStringBuilder append(CharSequence charSequence)
charSequence
interpreted as a sequence
of bytes using the virtual machine's default charset (see Charset.defaultCharset()
).charSequence
- the char sequence to append, must not be null
NullPointerException
- if charSequence
is null
public ByteString toByteString()
Copyright © 2016. All rights reserved.