Package ej.bon
Class ByteArray
- java.lang.Object
-
- ej.bon.ByteArray
-
public class ByteArray extends Object
This class provides some utilities to manage I/O on a byte array.
-
-
Field Summary
Fields Modifier and Type Field Description static intBIG_ENDIANAccess mode big endian.static intBYTE_SIZEThe size of a byte.static intCHAR_SIZEThe size of a char.static intINT_SIZEThe size of an int.static intLITTLE_ENDIANAccess mode little endian.static intLONG_SIZEThe size of a long.static intSHORT_SIZEThe size of a short.
-
Constructor Summary
Constructors Constructor Description ByteArray()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclear(byte[] array, int offset, int length)Fills a zone of a byte array with0.static intgetPlatformEndianness()Gets whether the platform is in big endian or little endian.static charreadChar(byte[] array, int offset)Reads a char in the given byte array at the given offset respecting the endianness of the platform.static charreadChar(byte[] array, int offset, int endianness)Reads a char in the given byte array at the given offset respecting the endianness of the array.static intreadInt(byte[] array, int offset)Reads an int in the given byte array at the given offset respecting the endianness of the platform.static intreadInt(byte[] array, int offset, int endianness)Reads an int in the given byte array at the given offset respecting the endianness of the array.static longreadLong(byte[] array, int offset)Reads a long in the given byte array at the given offset respecting the endianness of the platform.static longreadLong(byte[] array, int offset, int endianness)Reads a long in the given byte array at the given offset respecting the endianness of the array.static shortreadShort(byte[] array, int offset)Reads a short in the given byte array at the given offset respecting the endianness of the platform.static shortreadShort(byte[] array, int offset, int endianness)Reads a short in the given byte array at the given offset respecting the endianness of the array.static intreadUnsignedByte(byte[] array, int offset)Reads an unsigned-byte in the given byte array at the given offset respecting the endianness of the platform.static voidset(byte[] array, byte value, int offset, int length)Fills a zone of a byte array with the given value.static voidwriteInt(byte[] array, int offset, int value)Writes an int in the given byte array at the given offset respecting the endianness of the platform.static voidwriteInt(byte[] array, int offset, int value, int endianness)Writes an int in the given byte array at the given offset respecting the endianness of the array.static voidwriteLong(byte[] array, int offset, long value)Writes a long in the given byte array at the given offset respecting the endianness of the platform.static voidwriteLong(byte[] array, int offset, long value, int endianness)Writes a long in the given byte array at the given offset respecting the endianness of the array.static voidwriteShort(byte[] array, int offset, int value)Writes a short in the given byte array at the given offset respecting the endianness of the platform.static voidwriteShort(byte[] array, int offset, int value, int endianness)Writes a short in the given byte array at the given offset respecting the endianness of the array.
-
-
-
Field Detail
-
LITTLE_ENDIAN
public static final int LITTLE_ENDIAN
Access mode little endian.- See Also:
- Constant Field Values
-
BIG_ENDIAN
public static final int BIG_ENDIAN
Access mode big endian.- See Also:
- Constant Field Values
-
BYTE_SIZE
public static final int BYTE_SIZE
The size of a byte.- See Also:
- Constant Field Values
-
CHAR_SIZE
public static final int CHAR_SIZE
The size of a char.- See Also:
- Constant Field Values
-
SHORT_SIZE
public static final int SHORT_SIZE
The size of a short.- See Also:
- Constant Field Values
-
INT_SIZE
public static final int INT_SIZE
The size of an int.- See Also:
- Constant Field Values
-
LONG_SIZE
public static final int LONG_SIZE
The size of a long.- See Also:
- Constant Field Values
-
-
Method Detail
-
getPlatformEndianness
public static int getPlatformEndianness()
Gets whether the platform is in big endian or little endian.- Returns:
BIG_ENDIANif the platform is in big endian,LITTLE_ENDIANif in little endian
-
readUnsignedByte
public static int readUnsignedByte(byte[] array, int offset)Reads an unsigned-byte in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to read inoffset- the offset of the value to read- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
BYTE_SIZE
-
readShort
public static short readShort(byte[] array, int offset)Reads a short in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to read inoffset- the offset of the value to read- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
SHORT_SIZE
-
readChar
public static char readChar(byte[] array, int offset)Reads a char in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to read inoffset- the offset of the value to read- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
CHAR_SIZE
-
readInt
public static int readInt(byte[] array, int offset)Reads an int in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to read inoffset- the offset of the value to read- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
INT_SIZE
-
readLong
public static long readLong(byte[] array, int offset)Reads a long in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to read inoffset- the offset of the value to read- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
LONG_SIZE
-
readShort
public static short readShort(byte[] array, int offset, int endianness)Reads a short in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to read inoffset- the offset of the value to readendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
SHORT_SIZE
-
readChar
public static char readChar(byte[] array, int offset, int endianness)Reads a char in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to read inoffset- the offset of the value to readendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
CHAR_SIZE
-
readInt
public static int readInt(byte[] array, int offset, int endianness)Reads an int in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to read inoffset- the offset of the value to readendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
INT_SIZE
-
readLong
public static long readLong(byte[] array, int offset, int endianness)Reads a long in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to read inoffset- the offset of the value to readendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Returns:
- the read value
- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if read outside the bounds of the given array- See Also:
LONG_SIZE
-
writeShort
public static void writeShort(byte[] array, int offset, int value)Writes a short in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to write inoffset- the offset of the value to writevalue- the value to write- Throws:
NullPointerException- if the given array is nullArrayIndexOutOfBoundsException- if write outside the bounds of the given array- See Also:
SHORT_SIZE
-
writeInt
public static void writeInt(byte[] array, int offset, int value)Writes an int in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to write inoffset- the offset of the value to writevalue- the value to write- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array- See Also:
INT_SIZE
-
writeLong
public static void writeLong(byte[] array, int offset, long value)Writes a long in the given byte array at the given offset respecting the endianness of the platform.- Parameters:
array- the byte array to write inoffset- the offset of the value to writevalue- the value to write- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array- See Also:
LONG_SIZE
-
writeShort
public static void writeShort(byte[] array, int offset, int value, int endianness)Writes a short in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to write inoffset- the offset of the value to writevalue- the value to writeendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array- See Also:
SHORT_SIZE
-
writeInt
public static void writeInt(byte[] array, int offset, int value, int endianness)Writes an int in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to write inoffset- the offset of the value to writevalue- the value to writeendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array- See Also:
INT_SIZE
-
writeLong
public static void writeLong(byte[] array, int offset, long value, int endianness)Writes a long in the given byte array at the given offset respecting the endianness of the array.- Parameters:
array- the byte array to write inoffset- the offset of the value to writevalue- the value to writeendianness- the access mode (BIG_ENDIANorLITTLE_ENDIAN)- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array- See Also:
LONG_SIZE
-
clear
public static void clear(byte[] array, int offset, int length)Fills a zone of a byte array with0.- Parameters:
array- the byte array to clearoffset- the offset of the zone to clearlength- the length of the zone to clear- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array
-
set
public static void set(byte[] array, byte value, int offset, int length)Fills a zone of a byte array with the given value.- Parameters:
array- the byte array to setvalue- the value to fill the zone withoffset- the offset of the zone to setlength- the length of the zone to set- Throws:
ArrayIndexOutOfBoundsException- if write outside the bounds of the given array
-
-