Class LEB128


  • public class LEB128
    extends java.lang.Object
    Class to hold result of reading a LEB128 value, along with size and position metadata.

    Note: If a LEB128 value that would result in a native value longer than 64bits is attempted to be read, an IOException will be thrown, and the stream's position will be left at the last read byte.

    If this was a valid (but overly large) LEB128, the caller's stream will be left still pointing to LEB data.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int asInt32()
      Returns the value as an signed int32.
      long asLong()
      Returns the value as a 64bit primitive long.
      int asUInt32()
      Returns the value as an unsigned int32.
      static long decode​(byte[] bytes, boolean isSigned)
      Decodes a LEB128 number from a byte array and returns it as a long.
      static long decode​(byte[] bytes, int offset, boolean isSigned)
      Decodes a LEB128 number from a byte array and returns it as a long.
      int getLength()
      Returns the number of bytes that were used to store the LEB128 value in the stream it was read from.
      long getOffset()
      Returns the offset of the LEB128 value in the stream it was read from.
      static int readAsInt32​(BinaryReader reader)
      Reads a LEB128 signed number from the BinaryReader and returns it as a java 32 bit int.
      static long readAsLong​(BinaryReader reader, boolean isSigned)
      Reads a LEB128 number from the BinaryReader and returns it as a java 64 bit long int.
      static int readAsUInt32​(BinaryReader reader)
      Reads a LEB128 unsigned number from the BinaryReader and returns it as a java 32 bit int.
      static LEB128 readSignedValue​(BinaryReader reader)
      Reads an signed LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.
      static LEB128 readUnsignedValue​(BinaryReader reader)
      Reads an unsigned LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.
      static LEB128 readValue​(BinaryReader reader, boolean isSigned)
      Reads a LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • asUInt32

        public int asUInt32()
                     throws java.io.IOException
        Returns the value as an unsigned int32. If the actual value is outside the positive range of a java int (ie. 0.. Integer.MAX_VALUE), an exception is thrown.
        Returns:
        int in the range of 0 to Integer.MAX_VALUE
        Throws:
        java.io.IOException - if value is outside range
      • asInt32

        public int asInt32()
                    throws java.io.IOException
        Returns the value as an signed int32. If the actual value is outside the range of a java int (ie. Integer.MIN_VALUE.. Integer.MAX_VALUE), an exception is thrown.
        Returns:
        int in the range of Integer.MIN_VALUE to Integer.MAX_VALUE
        Throws:
        java.io.IOException - if value is outside range
      • getOffset

        public long getOffset()
        Returns the offset of the LEB128 value in the stream it was read from.
        Returns:
        stream offset of the LEB128 value
      • getLength

        public int getLength()
        Returns the number of bytes that were used to store the LEB128 value in the stream it was read from.
        Returns:
        number of bytes used to store the read LEB128 value
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • readValue

        public static LEB128 readValue​(BinaryReader reader,
                                       boolean isSigned)
                                throws java.io.IOException
        Reads a LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.

        See readAsLong(BinaryReader, boolean).

        Parameters:
        reader - BinaryReader to read bytes from
        isSigned - true if the value is signed
        Returns:
        a LEB128 instance with the read LEB128 value with metadata
        Throws:
        java.io.IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
      • readUnsignedValue

        public static LEB128 readUnsignedValue​(BinaryReader reader)
                                        throws java.io.IOException
        Reads an unsigned LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.

        See readAsLong(BinaryReader, boolean).

        Parameters:
        reader - BinaryReader to read bytes from
        Returns:
        a LEB128 instance with the read LEB128 value with metadata
        Throws:
        java.io.IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
      • readSignedValue

        public static LEB128 readSignedValue​(BinaryReader reader)
                                      throws java.io.IOException
        Reads an signed LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.

        See readAsLong(BinaryReader, boolean).

        Parameters:
        reader - BinaryReader to read bytes from
        Returns:
        a LEB128 instance with the read LEB128 value with metadata
        Throws:
        java.io.IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
      • readAsInt32

        public static int readAsInt32​(BinaryReader reader)
                               throws java.io.IOException
        Reads a LEB128 signed number from the BinaryReader and returns it as a java 32 bit int.

        If the value of the number can not fit in the int type, an IOException will be thrown.

        Parameters:
        reader - BinaryReader to read bytes from
        Returns:
        signed int32 value
        Throws:
        java.io.IOException - if error reading bytes or value is outside the range of a signed int32
      • readAsUInt32

        public static int readAsUInt32​(BinaryReader reader)
                                throws java.io.IOException
        Reads a LEB128 unsigned number from the BinaryReader and returns it as a java 32 bit int.

        If the value of the number can not fit in the positive range of the int type, an IOException will be thrown.

        Parameters:
        reader - BinaryReader to read bytes from
        Returns:
        unsigned int32 value 0..Integer.MAX_VALUE
        Throws:
        java.io.IOException - if error reading bytes or value is outside the positive range of a java 32 bit int (ie. 0..Integer.MAX_VALUE)
      • readAsLong

        public static long readAsLong​(BinaryReader reader,
                                      boolean isSigned)
                               throws java.io.IOException
        Reads a LEB128 number from the BinaryReader and returns it as a java 64 bit long int.

        Large unsigned integers that use all 64 bits are be returned in a java native 'long' type, which is signed. It is up to the caller to treat the value as unsigned.

        Large integers that use more than 64 bits will cause an IOException to be thrown.

        Parameters:
        reader - BinaryReader to read bytes from
        isSigned - true if the value is signed
        Returns:
        long integer value. Caller must treat it as unsigned if isSigned parameter was set to false
        Throws:
        java.io.IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
      • decode

        public static long decode​(byte[] bytes,
                                  boolean isSigned)
                           throws java.io.IOException
        Decodes a LEB128 number from a byte array and returns it as a long.

        See readAsLong(BinaryReader, boolean).

        Parameters:
        bytes - the bytes representing the LEB128 number
        isSigned - true if the value is signed
        Returns:
        long integer value. Caller must treat it as unsigned if isSigned parameter was set to false
        Throws:
        java.io.IOException - if error reading bytes or value is outside the range of a java 64 bit int
      • decode

        public static long decode​(byte[] bytes,
                                  int offset,
                                  boolean isSigned)
                           throws java.io.IOException
        Decodes a LEB128 number from a byte array and returns it as a long.

        See readAsLong(BinaryReader, boolean).

        Parameters:
        bytes - the bytes representing the LEB128 number
        offset - offset in byte array of where to start reading bytes
        isSigned - true if the value is signed
        Returns:
        long integer value. Caller must treat it as unsigned if isSigned parameter was set to false
        Throws:
        java.io.IOException - if error reading bytes or value is outside the range of a java 64 bit int