Package ej.bon

Class 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 int BIG_ENDIAN
      Access mode big endian.
      static int BYTE_SIZE
      The size of a byte.
      static int CHAR_SIZE
      The size of a char.
      static int INT_SIZE
      The size of an int.
      static int LITTLE_ENDIAN
      Access mode little endian.
      static int LONG_SIZE
      The size of a long.
      static int SHORT_SIZE
      The size of a short.
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteArray()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clear​(byte[] array, int offset, int length)
      Fills a zone of a byte array with 0.
      static int getPlatformEndianness()
      Gets whether the platform is in big endian or little endian.
      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.
      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.
      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.
      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.
      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.
      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.
      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.
      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.
      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.
      static void set​(byte[] array, byte value, int offset, int length)
      Fills a zone of a byte array with the given value.
      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.
      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.
      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.
      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.
      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.
      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.
    • Constructor Detail

      • ByteArray

        public ByteArray()
    • Method Detail

      • getPlatformEndianness

        public static int getPlatformEndianness()
        Gets whether the platform is in big endian or little endian.
        Returns:
        BIG_ENDIAN if the platform is in big endian, LITTLE_ENDIAN if 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 in
        offset - the offset of the value to read
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        endianness - the access mode (BIG_ENDIAN or LITTLE_ENDIAN)
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        endianness - the access mode (BIG_ENDIAN or LITTLE_ENDIAN)
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        endianness - the access mode (BIG_ENDIAN or LITTLE_ENDIAN)
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to read
        endianness - the access mode (BIG_ENDIAN or LITTLE_ENDIAN)
        Returns:
        the read value
        Throws:
        NullPointerException - if the given array is null
        ArrayIndexOutOfBoundsException - 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 in
        offset - the offset of the value to write
        value - the value to write
        Throws:
        NullPointerException - if the given array is null
        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)
        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 in
        offset - the offset of the value to write
        value - 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 in
        offset - the offset of the value to write
        value - 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 in
        offset - the offset of the value to write
        value - the value to write
        endianness - the access mode (BIG_ENDIAN or LITTLE_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 in
        offset - the offset of the value to write
        value - the value to write
        endianness - the access mode (BIG_ENDIAN or LITTLE_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 in
        offset - the offset of the value to write
        value - the value to write
        endianness - the access mode (BIG_ENDIAN or LITTLE_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 with 0.
        Parameters:
        array - the byte array to clear
        offset - the offset of the zone to clear
        length - 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 set
        value - the value to fill the zone with
        offset - the offset of the zone to set
        length - the length of the zone to set
        Throws:
        ArrayIndexOutOfBoundsException - if write outside the bounds of the given array