public class ByteArray extends Object implements Copyable, Encodable
Associated with the ByteArray is a byte [] value called "data" which holds the data bytes (starting from index 0) and the "length" variable specifies how many of those bytes are considered valid (which may be less than the actual length of the data variable).
Modifier and Type | Field and Description |
---|---|
byte[] |
data
The data bytes.
|
int |
length
The number of valid bytes in the data.
|
Constructor and Description |
---|
ByteArray() |
ByteArray(byte[] data)
This will actually use the "data" byte array as the data for the byte array.
|
Modifier and Type | Method and Description |
---|---|
void |
append(byte data)
Append a single byte of data.
|
void |
append(byte[] src,
int start,
int length)
Add bytes to the end of the ByteArray.
|
void |
appendInt(int value)
Append a four-byte integer value.
|
void |
appendInt(int value,
int numBytes)
Append an integer value as up to four bytes in big-endian format.
|
void |
appendLong(long value)
Append a long value as eight bytes in big-endian format.
|
static void |
asciiDecode(byte[] src,
int offset,
int length,
ByteArray dest) |
static void |
asciiDecode(char[] src,
int offset,
int length,
ByteArray dest) |
static int |
asciiDecode(Object src,
int offset,
int encodedCharCount,
byte[] dest,
int destOffset) |
static void |
asciiDecode(String src,
ByteArray dest) |
static String |
asciiEncode(byte[] src,
int offset,
int length) |
static void |
asciiEncode(byte[] src,
int offset,
int length,
ByteArray dest) |
static void |
asciiEncode(byte[] src,
int offset,
int length,
CharArray dest) |
static int |
asciiEncode(byte[] src,
int offset,
int count,
Object dest,
int destOffset)
Encode the bytes as ASCII to either a char or byte array.
|
static byte[] |
asciiEncodeToBytes(byte[] src,
int offset,
int length) |
ByteArray |
clear()
Set the "length" field to zero and return this CharArray object.
|
void |
copyFrom(byte[] data)
Copy the bytes from the supplied array, re-using the interal byte array if possible.
|
void |
copyFrom(byte[] data,
int offset,
int length)
Copy the bytes from the supplied array, re-using the interal byte array if possible.
|
boolean |
delete(int where,
int numBytes)
This removes bytes from within the data.
|
void |
ensureCapacity(int newCapacity)
This will ensure that the size of the "data" byte array is at least
newCapacity.
|
int |
getChecksum() |
Object |
getCopy()
Get a copy of this ByteArray with its own copy of this data.
|
static byte[] |
getEmpty()
Get an empty re-usable array of bytes.
|
void |
insert(int where,
byte[] src,
int start,
int length)
Insert bytes into the ByteArray.
|
void |
insertInt(int where,
int value,
int numBytes)
Insert an integer value as up to four bytes in big-endian format.
|
void |
insertLong(int where,
long value)
Insert a long value as eight bytes in big-endian format.
|
boolean |
makeSpace(int where,
int numBytes)
This adds space to the ByteArray at the specified index, increasing the
length value by numBytes.
|
static int |
maxSizeOfAsciiDecodedBytes(int encodedCharCount) |
void |
setData(byte[] data) |
void |
setLength(int len)
This sets the length of the valid data within the array, but does NOT extend OR truncate
the array.
|
static int |
sizeOfAsciiDecodedBytes(Object src,
int offset,
int encodedCharCount) |
static int |
sizeOfAsciiEncodedBytes(int count) |
byte[] |
takeData()
Take the data from the ByteArray leaving it with length 0 and with a new
empty data.
|
byte[] |
toBytes()
Return a copy of the data in the ByteArray as a byte array of the exact length of the data.
|
protected void |
toNewLength(int newLength)
Moves the data into a new data array of the specified length - which
must be greater than or equal to the number of elements in the array.
|
String |
toString()
Return a String representation of this object.
|
static String |
toString(byte[] data,
boolean putBrackets) |
static String |
toString(byte[] data,
int offset,
int length,
boolean putBrackets) |
void |
write(int where,
byte[] src,
int start,
int length)
This writes into the data at the specific location.
|
public byte[] data
public int length
public ByteArray()
public ByteArray(byte[] data)
public Object getCopy()
public static final byte[] getEmpty()
public byte[] takeData()
public boolean makeSpace(int where, int numBytes)
public void write(int where, byte[] src, int start, int length)
public void insert(int where, byte[] src, int start, int length)
where
- The index in this ByteArray where the data will go.src
- The source data bytes.start
- The start index in the source data.length
- The number of bytes to copy.public void append(byte[] src, int start, int length)
src
- The source data bytes.start
- The start index in the source data.length
- The number of bytes to copy.public void append(byte data)
data
- The byte to append.public void insertInt(int where, int value, int numBytes)
where
- the index at which to insert the integer.value
- the value to insert.numBytes
- the number of bytes (starting from the least significant) to insert.public void appendInt(int value, int numBytes)
value
- the value to append.numBytes
- the number of bytes (starting from the least significant) to insert.public void appendInt(int value)
value
- the value to append.public void insertLong(int where, long value)
where
- the index at which to insert the integer.value
- the value to insert.public void appendLong(long value)
value
- the value to append.public boolean delete(int where, int numBytes)
public void setLength(int len)
public byte[] toBytes()
public void copyFrom(byte[] data, int offset, int length)
public void copyFrom(byte[] data)
public void setData(byte[] data)
public ByteArray clear()
public void ensureCapacity(int newCapacity)
newCapacity
- The minimum size of the data array.protected void toNewLength(int newLength)
public static String toString(byte[] data, boolean putBrackets)
public static String toString(byte[] data, int offset, int length, boolean putBrackets)
public String toString()
Object
public int getChecksum()
public static String asciiEncode(byte[] src, int offset, int length)
public static byte[] asciiEncodeToBytes(byte[] src, int offset, int length)
public static void asciiDecode(String src, ByteArray dest) throws StreamCorruptedException
StreamCorruptedException
public static int sizeOfAsciiEncodedBytes(int count)
public static int asciiEncode(byte[] src, int offset, int count, Object dest, int destOffset)
src
- the source of the bytes.offset
- the source offset.count
- the number of bytes.dest
- the destination for the bytes, a char[] or byte[] or null to just check the number of bytes needed.destOffset
- the offset into the destination array.public static int maxSizeOfAsciiDecodedBytes(int encodedCharCount) throws StreamCorruptedException
StreamCorruptedException
public static int sizeOfAsciiDecodedBytes(Object src, int offset, int encodedCharCount) throws StreamCorruptedException
StreamCorruptedException
public static int asciiDecode(Object src, int offset, int encodedCharCount, byte[] dest, int destOffset) throws StreamCorruptedException
StreamCorruptedException
public static void asciiEncode(byte[] src, int offset, int length, CharArray dest)
public static void asciiEncode(byte[] src, int offset, int length, ByteArray dest)
public static void asciiDecode(byte[] src, int offset, int length, ByteArray dest) throws StreamCorruptedException
StreamCorruptedException
public static void asciiDecode(char[] src, int offset, int length, ByteArray dest) throws StreamCorruptedException
StreamCorruptedException