Class EndianUtils


  • public class EndianUtils
    extends java.lang.Object
    General Endian Related Utilties.

    This class provides static utility methods for input/output operations on numbers in Big and Little Endian formats.

    Origin of code: Based on the version in POI

    • Constructor Summary

      Constructors 
      Constructor Description
      EndianUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int getIntBE​(byte[] data)
      Get a BE int value from the beginning of a byte array
      static int getIntBE​(byte[] data, int offset)
      Get a BE int value from a byte array
      static int getIntLE​(byte[] data)
      Get a LE int value from the beginning of a byte array
      static int getIntLE​(byte[] data, int offset)
      Get a LE int value from a byte array
      static long getLongLE​(byte[] data, int offset)
      Get a LE long value from a byte array
      static short getShortBE​(byte[] data)
      Get a BE short value from the beginning of a byte array
      static short getShortBE​(byte[] data, int offset)
      Get a BE short value from a byte array
      static short getShortLE​(byte[] data)
      Get a LE short value from the beginning of a byte array
      static short getShortLE​(byte[] data, int offset)
      Get a LE short value from a byte array
      static short getUByte​(byte[] data, int offset)
      get the unsigned value of a byte.
      static long getUIntBE​(byte[] data)
      Get a BE unsigned int value from a byte array
      static long getUIntBE​(byte[] data, int offset)
      Get a BE unsigned int value from a byte array
      static long getUIntLE​(byte[] data)
      Get a LE unsigned int value from a byte array
      static long getUIntLE​(byte[] data, int offset)
      Get a LE unsigned int value from a byte array
      static int getUShortBE​(byte[] data)
      Get a BE unsigned short value from the beginning of a byte array
      static int getUShortBE​(byte[] data, int offset)
      Get a BE unsigned short value from a byte array
      static int getUShortLE​(byte[] data)
      Get a LE unsigned short value from the beginning of a byte array
      static int getUShortLE​(byte[] data, int offset)
      Get a LE unsigned short value from a byte array
      static int readIntBE​(java.io.InputStream stream)
      Get a BE int value from an InputStream
      static int readIntLE​(java.io.InputStream stream)
      Get a LE int value from an InputStream
      static long readLongBE​(java.io.InputStream stream)
      Get a NE long value from an InputStream
      static long readLongLE​(java.io.InputStream stream)
      Get a LE long value from an InputStream
      static short readShortBE​(java.io.InputStream stream)
      Get a BE short value from an InputStream
      static short readShortLE​(java.io.InputStream stream)
      Get a LE short value from an InputStream
      static long readUE7​(java.io.InputStream stream)
      Gets the integer value that is stored in UTF-8 like fashion, in Big Endian but with the high bit on each number indicating if it continues or not
      static long readUIntBE​(java.io.InputStream stream)
      Get a BE unsigned int value from an InputStream
      static long readUIntLE​(java.io.InputStream stream)
      Get a LE unsigned int value from an InputStream
      static int readUShortBE​(java.io.InputStream stream)  
      static int readUShortLE​(java.io.InputStream stream)  
      static int ubyteToInt​(byte b)
      Convert an 'unsigned' byte to an integer.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EndianUtils

        public EndianUtils()
    • Method Detail

      • readShortLE

        public static short readShortLE​(java.io.InputStream stream)
                                 throws java.io.IOException,
                                        EndianUtils.BufferUnderrunException
        Get a LE short value from an InputStream
        Parameters:
        stream - the InputStream from which the short is to be read
        Returns:
        the short (16-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readShortBE

        public static short readShortBE​(java.io.InputStream stream)
                                 throws java.io.IOException,
                                        EndianUtils.BufferUnderrunException
        Get a BE short value from an InputStream
        Parameters:
        stream - the InputStream from which the short is to be read
        Returns:
        the short (16-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readUIntLE

        public static long readUIntLE​(java.io.InputStream stream)
                               throws java.io.IOException,
                                      EndianUtils.BufferUnderrunException
        Get a LE unsigned int value from an InputStream
        Parameters:
        stream - the InputStream from which the int is to be read
        Returns:
        the int (32-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readUIntBE

        public static long readUIntBE​(java.io.InputStream stream)
                               throws java.io.IOException,
                                      EndianUtils.BufferUnderrunException
        Get a BE unsigned int value from an InputStream
        Parameters:
        stream - the InputStream from which the int is to be read
        Returns:
        the int (32-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readIntLE

        public static int readIntLE​(java.io.InputStream stream)
                             throws java.io.IOException,
                                    EndianUtils.BufferUnderrunException
        Get a LE int value from an InputStream
        Parameters:
        stream - the InputStream from which the int is to be read
        Returns:
        the int (32-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readIntBE

        public static int readIntBE​(java.io.InputStream stream)
                             throws java.io.IOException,
                                    EndianUtils.BufferUnderrunException
        Get a BE int value from an InputStream
        Parameters:
        stream - the InputStream from which the int is to be read
        Returns:
        the int (32-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readLongLE

        public static long readLongLE​(java.io.InputStream stream)
                               throws java.io.IOException,
                                      EndianUtils.BufferUnderrunException
        Get a LE long value from an InputStream
        Parameters:
        stream - the InputStream from which the long is to be read
        Returns:
        the long (64-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readLongBE

        public static long readLongBE​(java.io.InputStream stream)
                               throws java.io.IOException,
                                      EndianUtils.BufferUnderrunException
        Get a NE long value from an InputStream
        Parameters:
        stream - the InputStream from which the long is to be read
        Returns:
        the long (64-bit) value
        Throws:
        java.io.IOException - will be propagated back to the caller
        EndianUtils.BufferUnderrunException - if the stream cannot provide enough bytes
      • readUE7

        public static long readUE7​(java.io.InputStream stream)
                            throws java.io.IOException
        Gets the integer value that is stored in UTF-8 like fashion, in Big Endian but with the high bit on each number indicating if it continues or not
        Throws:
        java.io.IOException
      • getShortLE

        public static short getShortLE​(byte[] data)
        Get a LE short value from the beginning of a byte array
        Parameters:
        data - the byte array
        Returns:
        the short (16-bit) value
      • getShortLE

        public static short getShortLE​(byte[] data,
                                       int offset)
        Get a LE short value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the short (16-bit) value
      • getUShortLE

        public static int getUShortLE​(byte[] data)
        Get a LE unsigned short value from the beginning of a byte array
        Parameters:
        data - the byte array
        Returns:
        the unsigned short (16-bit) value in an int
      • getUShortLE

        public static int getUShortLE​(byte[] data,
                                      int offset)
        Get a LE unsigned short value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the unsigned short (16-bit) value in an integer
      • getShortBE

        public static short getShortBE​(byte[] data)
        Get a BE short value from the beginning of a byte array
        Parameters:
        data - the byte array
        Returns:
        the short (16-bit) value
      • getShortBE

        public static short getShortBE​(byte[] data,
                                       int offset)
        Get a BE short value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the short (16-bit) value
      • getUShortBE

        public static int getUShortBE​(byte[] data)
        Get a BE unsigned short value from the beginning of a byte array
        Parameters:
        data - the byte array
        Returns:
        the unsigned short (16-bit) value in an int
      • getUShortBE

        public static int getUShortBE​(byte[] data,
                                      int offset)
        Get a BE unsigned short value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the unsigned short (16-bit) value in an integer
      • getIntLE

        public static int getIntLE​(byte[] data)
        Get a LE int value from the beginning of a byte array
        Parameters:
        data - the byte array
        Returns:
        the int (32-bit) value
      • getIntLE

        public static int getIntLE​(byte[] data,
                                   int offset)
        Get a LE int value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the int (32-bit) value
      • getIntBE

        public static int getIntBE​(byte[] data)
        Get a BE int value from the beginning of a byte array
        Parameters:
        data - the byte array
        Returns:
        the int (32-bit) value
      • getIntBE

        public static int getIntBE​(byte[] data,
                                   int offset)
        Get a BE int value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the int (32-bit) value
      • getUIntLE

        public static long getUIntLE​(byte[] data)
        Get a LE unsigned int value from a byte array
        Parameters:
        data - the byte array
        Returns:
        the unsigned int (32-bit) value in a long
      • getUIntLE

        public static long getUIntLE​(byte[] data,
                                     int offset)
        Get a LE unsigned int value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the unsigned int (32-bit) value in a long
      • getUIntBE

        public static long getUIntBE​(byte[] data)
        Get a BE unsigned int value from a byte array
        Parameters:
        data - the byte array
        Returns:
        the unsigned int (32-bit) value in a long
      • getUIntBE

        public static long getUIntBE​(byte[] data,
                                     int offset)
        Get a BE unsigned int value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the unsigned int (32-bit) value in a long
      • getLongLE

        public static long getLongLE​(byte[] data,
                                     int offset)
        Get a LE long value from a byte array
        Parameters:
        data - the byte array
        offset - a starting offset into the byte array
        Returns:
        the long (64-bit) value
      • ubyteToInt

        public static int ubyteToInt​(byte b)
        Convert an 'unsigned' byte to an integer. ie, don't carry across the sign.
        Parameters:
        b - Description of the Parameter
        Returns:
        Description of the Return Value
      • getUByte

        public static short getUByte​(byte[] data,
                                     int offset)
        get the unsigned value of a byte.
        Parameters:
        data - the byte array.
        offset - a starting offset into the byte array.
        Returns:
        the unsigned value of the byte as a 16 bit short