public abstract class ByteArraySupport extends Object
ByteArraySupport.littleEndian() or ByteArraySupport.bigEndian() respectively to access byte arrays in
little-endian or big-endian order.| Modifier and Type | Method and Description |
|---|---|
static ByteArraySupport |
bigEndian()
Enables accessing multibyte Java primitives from byte arrays in big-endian order.
|
abstract byte |
getByte(byte[] buffer,
int index)
Reads the byte at the given index.
|
abstract double |
getDouble(byte[] buffer,
int index)
Reads the double at the given index.
|
abstract float |
getFloat(byte[] buffer,
int index)
Reads the float at the given index.
|
abstract int |
getInt(byte[] buffer,
int index)
Reads the int at the given index.
|
abstract long |
getLong(byte[] buffer,
int index)
Reads the int at the given index.
|
abstract short |
getShort(byte[] buffer,
int index)
Reads the short at the given index.
|
boolean |
inBounds(byte[] buffer,
int startIndex,
int length)
Checks if an access is in bounds of the given buffer.
|
static ByteArraySupport |
littleEndian()
Enables accessing multibyte Java primitives from byte arrays in little-endian order.
|
abstract void |
putByte(byte[] buffer,
int index,
byte value)
Writes the given byte at the given index.
|
abstract void |
putDouble(byte[] buffer,
int index,
double value)
Writes the given double at the given index.
|
abstract void |
putFloat(byte[] buffer,
int index,
float value)
Writes the given float at the given index.
|
abstract void |
putInt(byte[] buffer,
int index,
int value)
Writes the given int at the given index.
|
abstract void |
putLong(byte[] buffer,
int index,
long value)
Writes the given int at the given index.
|
abstract void |
putShort(byte[] buffer,
int index,
short value)
Writes the given short at the given index.
|
public static ByteArraySupport littleEndian()
Example usage:
byte[] buffer = new byte[]{0, 0, 0, 0};
ByteArraySupport.littleEndian().putShort(buffer, 2, (short) 1);
// buffer[2] == (byte) 1
// buffer[3] == (byte) 0
ByteArraySupport.littleEndian().putShort(buffer, 3, (short) 1);
// throws IndexOutOfBoundsException exception
public static ByteArraySupport bigEndian()
Example usage:
byte[] buffer = new byte[]{0, 0, 0, 0};
ByteArraySupport.bigEndian().putShort(buffer, 0, (short) 1);
// buffer[0] == (byte) 0
// buffer[1] == (byte) 1
public final boolean inBounds(byte[] buffer,
int startIndex,
int length)
If out-of-bounds accesses are expected to happen frequently, it is faster (~1.2x in
interpreter mode) to use this method to check for them than catching
IndexOutOfBoundsExceptions.
buffer - The byte arraystartIndex - The start index of the accesslength - The number of bytes accessedpublic abstract byte getByte(byte[] buffer,
int index)
throws IndexOutOfBoundsException
buffer - The byte array to read fromindex - The index from which the byte will be readIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's sizepublic abstract void putByte(byte[] buffer,
int index,
byte value)
throws IndexOutOfBoundsException
buffer - The byte array to write inindex - The index at which the byte will be writtenvalue - The byte value to be writtenIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's sizepublic abstract short getShort(byte[] buffer,
int index)
throws IndexOutOfBoundsException
buffer - The byte array to read fromindex - The index from which the short will be readIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus onepublic abstract void putShort(byte[] buffer,
int index,
short value)
throws IndexOutOfBoundsException
buffer - The byte array to write inindex - The index at which the short will be writtenvalue - The short value to be writtenIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus onepublic abstract int getInt(byte[] buffer,
int index)
throws IndexOutOfBoundsException
buffer - The byte array to read fromindex - The index from which the int will be readIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus threepublic abstract void putInt(byte[] buffer,
int index,
int value)
throws IndexOutOfBoundsException
buffer - The byte array to write inindex - The index at which the int will be writtenvalue - The int value to be writtenIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus threepublic abstract long getLong(byte[] buffer,
int index)
throws IndexOutOfBoundsException
buffer - The byte array to read fromindex - The index from which the int will be readIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus sevenpublic abstract void putLong(byte[] buffer,
int index,
long value)
throws IndexOutOfBoundsException
buffer - The byte array to write inindex - The index at which the int will be writtenvalue - The int value to be writtenIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus sevenpublic abstract float getFloat(byte[] buffer,
int index)
throws IndexOutOfBoundsException
buffer - The byte array to read fromindex - The index from which the float will be readIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus threepublic abstract void putFloat(byte[] buffer,
int index,
float value)
throws IndexOutOfBoundsException
buffer - The byte array to write inindex - The index at which the float will be writtenvalue - The float value to be writtenIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus threepublic abstract double getDouble(byte[] buffer,
int index)
throws IndexOutOfBoundsException
buffer - The byte array to read fromindex - The index from which the double will be readIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus sevenpublic abstract void putDouble(byte[] buffer,
int index,
double value)
throws IndexOutOfBoundsException
buffer - The byte array to write inindex - The index at which the double will be writtenvalue - The double value to be writtenIndexOutOfBoundsException - If index is negative or not smaller than the
buffer's size minus seven