Class ByteArrayUnmarshaller


  • public final class ByteArrayUnmarshaller
    extends Object
    Conversion routines to unmarshall Java binary types (short, int, long, float, double) from byte arrays.

    With sign extensions enabled, the marshalled data is interpreted as signed and the data will be appropriately converted into the return type container. With sign extensions disabled, unfilled bits in the container will be set to zero. For example, -1 as one signed byte is 0xFF. Using readShort with signExtend true, the resultant short will contain 0xFFFF, which is -1 in Java's signed format. With signExtend false, the resultant short will contain 0x00FF, which is 255.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double readDouble​(byte[] byteArray, int offset, boolean bigEndian)
      Returns a double value copied from eight consecutive bytes of the byte array starting at the offset.
      static float readFloat​(byte[] byteArray, int offset, boolean bigEndian)
      Returns a float value copied from four consecutive bytes of the byte array starting at the offset.
      static int readInt​(byte[] byteArray, int offset, boolean bigEndian)
      Returns an int value copied from four consecutive bytes starting at the offset.
      static int readInt​(byte[] byteArray, int offset, boolean bigEndian, int numBytes, boolean signExtend)
      Returns an int value copied from zero to four consecutive bytes of the byte array starting at the offset.
      static long readLong​(byte[] byteArray, int offset, boolean bigEndian)
      Returns a long value copied from eight consecutive bytes of the byte array starting at the offset.
      static long readLong​(byte[] byteArray, int offset, boolean bigEndian, int numBytes, boolean signExtend)
      Returns a long value copied from zero to eight consecutive bytes of the byte array starting at the offset.
      static short readShort​(byte[] byteArray, int offset, boolean bigEndian)
      Returns a short value copied from two consecutive bytes of the byte array starting at the offset.
      static short readShort​(byte[] byteArray, int offset, boolean bigEndian, int numBytes, boolean signExtend)
      Returns a short value copied from zero to two consecutive bytes of the byte array starting at the offset.
    • Method Detail

      • readShort

        public static short readShort​(byte[] byteArray,
                                      int offset,
                                      boolean bigEndian)
        Returns a short value copied from two consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        Returns:
        short
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readShort

        public static short readShort​(byte[] byteArray,
                                      int offset,
                                      boolean bigEndian,
                                      int numBytes,
                                      boolean signExtend)
        Returns a short value copied from zero to two consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        numBytes - the number of bytes to unmarshall, must be 0-2 inclusive
        signExtend - if true and numBytes < 2 then the topmost bytes of the returned short will be sign extended
        Returns:
        long
        Throws:
        NullPointerException - if byteArray is null
        IllegalArgumentException - if numBytes < 0 or numBytes > 2
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readInt

        public static int readInt​(byte[] byteArray,
                                  int offset,
                                  boolean bigEndian)
        Returns an int value copied from four consecutive bytes starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        Returns:
        int
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readInt

        public static int readInt​(byte[] byteArray,
                                  int offset,
                                  boolean bigEndian,
                                  int numBytes,
                                  boolean signExtend)
        Returns an int value copied from zero to four consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        numBytes - the number of bytes to unmarshall, must be 0-4 inclusive
        signExtend - if true and numBytes < 4 then the topmost bytes of the returned int will be sign extended
        Returns:
        int
        Throws:
        NullPointerException - if byteArray is null
        IllegalArgumentException - if numBytes < 0 or numBytes > 4
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readLong

        public static long readLong​(byte[] byteArray,
                                    int offset,
                                    boolean bigEndian)
        Returns a long value copied from eight consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        Returns:
        long
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readLong

        public static long readLong​(byte[] byteArray,
                                    int offset,
                                    boolean bigEndian,
                                    int numBytes,
                                    boolean signExtend)
        Returns a long value copied from zero to eight consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        numBytes - the number of bytes to unmarshall, must be 0-8 inclusive
        signExtend - if true and numBytes < 8 then the topmost bytes of the returned long will be sign extended
        Returns:
        long
        Throws:
        NullPointerException - if byteArray is null
        IllegalArgumentException - if numBytes < 0 or numBytes > 8
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readFloat

        public static float readFloat​(byte[] byteArray,
                                      int offset,
                                      boolean bigEndian)
        Returns a float value copied from four consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        Returns:
        float
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • readDouble

        public static double readDouble​(byte[] byteArray,
                                        int offset,
                                        boolean bigEndian)
        Returns a double value copied from eight consecutive bytes of the byte array starting at the offset.
        Parameters:
        byteArray - source
        offset - offset in the byte array
        bigEndian - if false the bytes will be copied in reverse (little endian) order
        Returns:
        double
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs