public class ByteArrayBuilder extends Object
StringBuilder for strings.
ByteArrayOutputStream also provides equivalent
functionality, but its write methods are synchronized.| Modifier and Type | Field and Description |
|---|---|
static int |
MINIMUM_READ_BUFFER_SIZE |
| Constructor and Description |
|---|
ByteArrayBuilder()
Construct a
ByteArrayBuilder with the default initial size of 20. |
ByteArrayBuilder(byte[] array)
Construct a
ByteArrayBuilder from an existing byte array. |
ByteArrayBuilder(ByteArrayBuilder bab)
Construct a
ByteArrayBuilder from another ByteArrayBuilder. |
ByteArrayBuilder(int size)
Construct a
ByteArrayBuilder with the specified initial size. |
ByteArrayBuilder(int size,
int increment)
Construct a
ByteArrayBuilder with the specified initial size and increment. |
| Modifier and Type | Method and Description |
|---|---|
ByteArrayBuilder |
append(byte[] array)
Append a byte array.
|
ByteArrayBuilder |
append(byte[] array,
int start,
int end)
Append a specified range of bytes from a byte array.
|
ByteArrayBuilder |
append(ByteArrayBuilder bab)
Append another
ByteArrayBuilder. |
ByteArrayBuilder |
append(InputStream is)
Append the contents of an
InputStream. |
ByteArrayBuilder |
append(int b)
Append an
int as a single byte. |
ByteArrayBuilder |
create()
Convenience method to create a
ByteArrayBuilder. |
ByteArrayBuilder |
create(int size)
Convenience method to create a
ByteArrayBuilder, specifying the initial size. |
byte |
get(int index)
Get a nominated byte from the array.
|
ByteArrayBuilder |
insert(int index,
byte[] array)
Insert a byte array at the nominated point in the array.
|
ByteArrayBuilder |
insert(int index,
ByteArrayBuilder bab)
Insert another
ByteArrayBuilder at the nominated point in the array. |
ByteArrayBuilder |
insert(int index,
int b)
Insert an
int as a single byte at the nominated point in the array. |
int |
length()
Get the length of the
ByteArrayBuilder (the number of bytes used). |
byte |
set(int index,
int value)
Set a nominated byte in the array to a given value.
|
byte[] |
toByteArray()
Get a byte array of only the bytes used.
|
byte[] |
toByteArray(int start)
Get a byte array of only the bytes used, starting from a nominated point.
|
byte[] |
toByteArray(int start,
int end)
Get a byte array of only the bytes used, using a nominated start and end point.
|
public static final int MINIMUM_READ_BUFFER_SIZE
public ByteArrayBuilder(int size,
int increment)
ByteArrayBuilder with the specified initial size and increment.size - the initial size (may be zero)increment - the amount to increment the size by when necessaryIllegalArgumentException - if the size is negative or the increment is not
positivepublic ByteArrayBuilder(int size)
ByteArrayBuilder with the specified initial size.size - the initial sizeIllegalArgumentException - if the size is negativepublic ByteArrayBuilder()
ByteArrayBuilder with the default initial size of 20.public ByteArrayBuilder(byte[] array)
ByteArrayBuilder from an existing byte array.array - the byte arraypublic ByteArrayBuilder(ByteArrayBuilder bab)
ByteArrayBuilder from another ByteArrayBuilder.bab - the other ByteArrayBuilderpublic int length()
ByteArrayBuilder (the number of bytes used).public byte[] toByteArray()
public byte[] toByteArray(int start)
start - the start indexIndexOutOfBoundsException - if the start index is outside the bounds of the arraypublic byte[] toByteArray(int start,
int end)
start - the start indexend - the end indexIndexOutOfBoundsException - if the start or end index is not validpublic byte set(int index,
int value)
index - the index to be modifiedvalue - the new valueIndexOutOfBoundsException - if the index is not validpublic byte get(int index)
index - the index of the byteIndexOutOfBoundsException - if the index is not validpublic ByteArrayBuilder append(int b)
int as a single byte.b - the new valuethis (for chaining)public ByteArrayBuilder append(byte[] array)
array - the byte arraythis (for chaining)public ByteArrayBuilder append(byte[] array, int start, int end)
array - the byte arraystart - the start indexend - the end indexthis (for chaining)public ByteArrayBuilder append(ByteArrayBuilder bab)
ByteArrayBuilder.bab - the other ByteArrayBuilderthis (for chaining)public ByteArrayBuilder append(InputStream is) throws IOException
InputStream.is - the InputStreamthis (for chaining)IOException - if thrown by the InputStreampublic ByteArrayBuilder insert(int index, int b)
int as a single byte at the nominated point in the array. All
following bytes will be moved up make room.index - the index at which to insertb - the new valuethis (for chaining)IndexOutOfBoundsException - if the index is not validpublic ByteArrayBuilder insert(int index, byte[] array)
index - the index at which to insertarray - the byte arraythis (for chaining)IndexOutOfBoundsException - if the index is not validpublic ByteArrayBuilder insert(int index, ByteArrayBuilder bab)
ByteArrayBuilder at the nominated point in the array. All
following bytes will be moved up make room.index - the index at which to insertbab - the other ByteArrayBuilderthis (for chaining)IndexOutOfBoundsException - if the index is not validpublic ByteArrayBuilder create()
ByteArrayBuilder. Supports the idiom:
ByteArrayBuilder bab = ByteArrayBuilder.create().append(0).append(1).append(2);
ByteArrayBuilderpublic ByteArrayBuilder create(int size)
ByteArrayBuilder, specifying the initial size.
Supports the idiom (for example):
InputStream is = new FileInputStream("data.bin");
byte[] fileContents = ByteArrayBuilder.create(1000).append(is).toByteArray();
size - the initial sizeByteArrayBuilderIllegalArgumentException - if the size is negativeCopyright © 2020. All rights reserved.