public class ByteArray
extends java.lang.Object
The class itself represents a byte array, and it provides operations that can be performed on the byte array. All internal data is wiped following use so that no unnecessary copies of the byte array exist in memory.
| Constructor and Description |
|---|
ByteArray()
The default constructor.
|
ByteArray(java.math.BigInteger bi)
Creates a new
ByteArray object that contains a copy of the
bytes the specified BigInteger represents. |
ByteArray(byte[] ba)
Creates a new
ByteArray object that contains a copy of the
contents of the specified byte array. |
ByteArray(ByteArray byteArray)
Creates a new
ByteArray object that contains a copy of the
contents of the specified ByteArray. |
ByteArray(long l)
Creates a new
ByteArray object that contains a copy of the
bytes in the specified long value. |
| Modifier and Type | Method and Description |
|---|---|
void |
append(byte b)
Appends a byte to this
ByteArray object. |
void |
append(byte[] ba)
Appends a byte array to this
ByteArray object. |
static byte[] |
append(byte[] ba1,
byte[] ba2)
Appends one byte array to another.
|
static byte[] |
append(byte[] ba1,
byte[] ba2,
int off,
int len)
Appends a section of one byte array to another.
|
void |
append(ByteArray byteArray)
Appends a
ByteArray object to this ByteArray
object. |
int |
calculateASNLength()
Deprecated.
this function was intended for internal JTK use only; deprecated
since 6.1
|
void |
fromHexString(java.lang.String hexString)
Sets the byte array the object contains by from a hexadecimal string.
|
byte[] |
getArray()
Returns the byte array this object contains.
|
int |
getLength()
Returns the length of the byte array this object contains.
|
boolean |
isEqual(byte[] ba)
Checks if the byte array this object is identical to the specified byte
array.
|
boolean |
isEqual(ByteArray byteArray)
Checks if this
ByteArray object is identical to the specified
ByteArray object. |
void |
removeLeadingZeros()
Checks the beginning of the byte array this object contains for bytes
0x00.
|
void |
setArray(byte[] ba)
Sets the byte array this object contains to be a copy of the specified
byte array.
|
void |
tail(int offset)
Truncates the beginning of byte array this object contains.
|
static byte |
toByte(int i)
Deprecated.
has the same effect as a direct cast, please use this instead,
ex: byte b = (byte)-234823; deprecated since 6.1
|
java.lang.String |
toHexString()
Converts the byte array this object contains to a hexadecimal string
of format 012345..., where every two characters represent the hexadecimal
value of a single byte.
|
static int |
toInt(byte b)
Deprecated.
this function was intended for internal JTK use only; deprecated
since 6.1
|
void |
truncate(int length)
Truncates the end of the byte array this object contains.
|
void |
wipe()
Wipes all internal data.
|
public ByteArray()
public ByteArray(byte[] ba)
ByteArray object that contains a copy of the
contents of the specified byte array.
If the provided byte array is null, then the
ByteArray created will also contain a null byte
array.
ba - a byte arraypublic ByteArray(ByteArray byteArray)
ByteArray object that contains a copy of the
contents of the specified ByteArray.
If the provided ByteArray contains a null byte
array, then the ByteArray created will also contain a
null byte array.
byteArray - a ByteArray objectpublic ByteArray(long l)
ByteArray object that contains a copy of the
bytes in the specified long value.l - a long valuepublic ByteArray(java.math.BigInteger bi)
ByteArray object that contains a copy of the
bytes the specified BigInteger represents.
This is done by converting the BigInteger to it's
two's-complement representation using big-endian byte-order (most
significant byte is in the zeroth element).
bi - a BigInteger valuepublic static byte[] append(byte[] ba1,
byte[] ba2)
The byte array specified by ba2 is appended to
ba1. This is done by creating a new byte array and copying
the contents of ba1 into the first part of the new byte array,
and the contents of ba2 into the remainder.
SecureUtils.append(byte[], byte[]), this API does not wipe
either ba1 or ba2ba1 - the byte[] to which ba2 will be appendedba2 - the byte[] that will be appended to ba1byte[] resulting from append operationpublic static byte[] append(byte[] ba1,
byte[] ba2,
int off,
int len)
A specified section of the byte array denoted by the argument
ba2, is appended to ba1. This is done by creating
a new byte array and copying the contents of ba1 into the first
part of the new byte array, and the specified contents of ba2
into the remainder.
The specified contents of ba2 are the bytes starting at
off and extending for len bytes.
SecureUtils.append(byte[], byte[], int, int), this API does
not wipe either ba1 or ba2ba1 - the byte[] to which part of ba2 will be
appendedba2 - the byte[] that will be appended to ba1off - the location of the start of the data in ba2 that will be
appended to ba1len - the number of bytes from ba2 to append to ba1byte[] resulting from append operationpublic byte[] getArray()
It returns the actual byte array, not a copy.
public void setArray(byte[] ba)
ba - a byte arraypublic int getLength()
public boolean isEqual(byte[] ba)
ba - the byte array to compare withtrue if the byte arrays are identical; false
otherwisepublic boolean isEqual(ByteArray byteArray)
ByteArray object is identical to the specified
ByteArray object.
Two ByteArray objects are considered "equal" if the byte
arrays they contain are identical.
byteArray - the ByteArray object to compare withtrue if the ByteArray objects are identical;
false otherwisepublic void append(byte[] ba)
ByteArray object.
The specified byte array is simply appended to the end of the byte array that this object contains.
ba - the byte array to be appendedpublic void append(byte b)
ByteArray object.
The specified byte is simply appended to the end of the byte array that this object contains.
b - the byte to be appendedpublic void append(ByteArray byteArray)
ByteArray object to this ByteArray
object.
The byte array that the specified ByteArray object contains
is simply appended to the end of the byte array that this object contains.
byteArray - the ByteArray object to be appendedpublic void tail(int offset)
throws java.lang.IllegalArgumentException
All bytes before the indicated offset are discarded.
offset - the number of bytes that will be truncated from the beginning of the
byte array this object containsjava.lang.IllegalArgumentException - thrown if the offset is less than zero, or greater than the length of
the byte array this object containspublic void truncate(int length)
throws java.lang.IllegalArgumentException
All bytes after the indicated length are discarded.
length - the length at which to truncate the byte array this object containsjava.lang.IllegalArgumentException - thrown if length is less than zero, or greater than the length of the
byte array this object containspublic void removeLeadingZeros()
Any leading sequence of zero bytes are discarded.
public void wipe()
public java.lang.String toHexString()
Each byte will be converted into a string in the following range (00..FF).
public void fromHexString(java.lang.String hexString)
throws java.lang.IllegalArgumentException,
java.lang.NumberFormatException
The hexadecimal must contain only hexadecimal digits that are not separated by any other character. The conversion is not case sensitive.
hexString - a hexadecimal stringjava.lang.IllegalArgumentException - thrown if the string does not contain an even number of characters (all
hexadecimal strings contain an even number of characters)java.lang.NumberFormatException - thrown if the string contains any characters that are not hexadecimal
digits [0..9],[A..F],[a..f]public static byte toByte(int i)
No error checking is done, since the converstion will raise runtime errors if the values passed are invalid.
i - a positive integerpublic static int toInt(byte b)
This is a hack for getting around the fact that Java doesn't allow unsigned bytes.
b - a signed bytepublic int calculateASNLength()