public abstract class ByteArraySupport extends Object
ByteArraySupport.littleEndian()
or ByteArraySupport.bigEndian()
respectively to access byte arrays in
little-endian or big-endian order.
The methods of this class are not safe for use by multiple concurrent threads. If a byte array is to be used by more than one thread then access to the byte array should be controlled by appropriate synchronization.
Unaligned accesses are allowed.
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 byteOffset)
Reads the byte at the given byte offset from the start of the buffer.
|
abstract byte |
getByte(byte[] buffer,
long byteOffset)
Reads the byte at the given byte offset from the start of the buffer.
|
abstract double |
getDouble(byte[] buffer,
int byteOffset)
Reads the double at the given byte offset from the start of the buffer.
|
abstract double |
getDouble(byte[] buffer,
long byteOffset)
Reads the double at the given byte offset from the start of the buffer.
|
abstract float |
getFloat(byte[] buffer,
int byteOffset)
Reads the float at the given byte offset from the start of the buffer.
|
abstract float |
getFloat(byte[] buffer,
long byteOffset)
Reads the float at the given byte offset from the start of the buffer.
|
abstract int |
getInt(byte[] buffer,
int byteOffset)
Reads the int at the given byte offset from the start of the buffer.
|
abstract int |
getInt(byte[] buffer,
long byteOffset)
Reads the int at the given byte offset from the start of the buffer.
|
abstract long |
getLong(byte[] buffer,
int byteOffset)
Reads the long at the given byte offset from the start of the buffer.
|
abstract long |
getLong(byte[] buffer,
long byteOffset)
Reads the long at the given byte offset from the start of the buffer.
|
abstract short |
getShort(byte[] buffer,
int byteOffset)
Reads the short at the given byte offset from the start of the buffer.
|
abstract short |
getShort(byte[] buffer,
long byteOffset)
Reads the short at the given byte offset from the start of the buffer.
|
boolean |
inBounds(byte[] buffer,
int startByteOffset,
int length)
Checks if an access is in bounds of the given buffer.
|
boolean |
inBounds(byte[] buffer,
long startByteOffset,
long 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 byteOffset,
byte value)
Writes the given byte at the given byte offset from the start of the buffer.
|
abstract void |
putByte(byte[] buffer,
long byteOffset,
byte value)
Writes the given byte at the given byte offset from the start of the buffer.
|
abstract void |
putDouble(byte[] buffer,
int byteOffset,
double value)
Writes the given double at the given byte offset from the start of the buffer.
|
abstract void |
putDouble(byte[] buffer,
long byteOffset,
double value)
Writes the given double at the given byte offset from the start of the buffer.
|
abstract void |
putFloat(byte[] buffer,
int byteOffset,
float value)
Writes the given float at the given byte offset from the start of the buffer.
|
abstract void |
putFloat(byte[] buffer,
long byteOffset,
float value)
Writes the given float at the given byte offset from the start of the buffer.
|
abstract void |
putInt(byte[] buffer,
int byteOffset,
int value)
Writes the given int at the given byte offset from the start of the buffer.
|
abstract void |
putInt(byte[] buffer,
long byteOffset,
int value)
Writes the given int at the given byte offset from the start of the buffer.
|
abstract void |
putLong(byte[] buffer,
int byteOffset,
long value)
Writes the given long at the given byte offset from the start of the buffer.
|
abstract void |
putLong(byte[] buffer,
long byteOffset,
long value)
Writes the given long at the given byte offset from the start of the buffer.
|
abstract void |
putShort(byte[] buffer,
int byteOffset,
short value)
Writes the given short at the given byte offset from the start of the buffer.
|
abstract void |
putShort(byte[] buffer,
long byteOffset,
short value)
Writes the given short at the given byte offset from the start of the buffer.
|
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 startByteOffset, 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
IndexOutOfBoundsException
s.
buffer
- the byte arraystartByteOffset
- the start byte offset of the accesslength
- the number of bytes accessedpublic final boolean inBounds(byte[] buffer, long startByteOffset, long 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
IndexOutOfBoundsException
s.
buffer
- the byte arraystartByteOffset
- the start byte offset of the accesslength
- the number of bytes accessedpublic abstract byte getByte(byte[] buffer, int byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset at which the byte will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length
public abstract byte getByte(byte[] buffer, long byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset at which the byte will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length
public abstract void putByte(byte[] buffer, int byteOffset, byte value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset at which the byte will be writtenvalue
- the byte value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length
public abstract void putByte(byte[] buffer, long byteOffset, byte value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset at which the byte will be writtenvalue
- the byte value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length
public abstract short getShort(byte[] buffer, int byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the short will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 1
public abstract short getShort(byte[] buffer, long byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the short will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 1
public abstract void putShort(byte[] buffer, int byteOffset, short value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the short will be writtenvalue
- the short value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 1
public abstract void putShort(byte[] buffer, long byteOffset, short value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the short will be writtenvalue
- the short value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 1
public abstract int getInt(byte[] buffer, int byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the int will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract int getInt(byte[] buffer, long byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the int will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract void putInt(byte[] buffer, int byteOffset, int value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the int will be writtenvalue
- the int value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract void putInt(byte[] buffer, long byteOffset, int value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the int will be writtenvalue
- the int value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract long getLong(byte[] buffer, int byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the int will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract long getLong(byte[] buffer, long byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the int will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract void putLong(byte[] buffer, int byteOffset, long value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the int will be writtenvalue
- the int value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract void putLong(byte[] buffer, long byteOffset, long value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the int will be writtenvalue
- the int value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract float getFloat(byte[] buffer, int byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the float will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract float getFloat(byte[] buffer, long byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the float will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract void putFloat(byte[] buffer, int byteOffset, float value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the float will be writtenvalue
- the float value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract void putFloat(byte[] buffer, long byteOffset, float value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the float will be writtenvalue
- the float value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 3
public abstract double getDouble(byte[] buffer, int byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the double will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract double getDouble(byte[] buffer, long byteOffset) throws IndexOutOfBoundsException
buffer
- the byte array to read frombyteOffset
- the byte offset from which the double will be readIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract void putDouble(byte[] buffer, int byteOffset, double value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the double will be writtenvalue
- the double value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7
public abstract void putDouble(byte[] buffer, long byteOffset, double value) throws IndexOutOfBoundsException
buffer
- the byte array to write inbyteOffset
- the byte offset from which the double will be writtenvalue
- the double value to be writtenIndexOutOfBoundsException
- if and only if
byteOffset < 0 || byteOffset >= buffer.length - 7