Class LittleEndianUtils


  • public class LittleEndianUtils
    extends java.lang.Object
    Utilities to read and write 16, 32, and 64 bit integers with support for little-endian encoding, as used in zip files. Zip files actually use unsigned data types. We use Java's native (signed) data types but will use long (64 bit) to ensure we can fit the whole range for the 16 and 32 bit fields.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int readUnsigned2Le​(java.nio.ByteBuffer bytes)
      Reads 2 bytes in little-endian format and converts them into a 16-bit value.
      static long readUnsigned4Le​(java.nio.ByteBuffer bytes)
      Reads 4 bytes in little-endian format and converts them into a 32-bit value.
      static long readUnsigned8Le​(java.nio.ByteBuffer bytes)
      Reads 8 bytes in little-endian format and converts them into a 64-bit value.
      static void writeUnsigned2Le​(java.nio.ByteBuffer output, int value)
      Writes 2 bytes in little-endian format, converting them from a 16-bit value.
      static void writeUnsigned4Le​(java.nio.ByteBuffer output, long value)
      Writes 4 bytes in little-endian format, converting them from a 32-bit value.
      static void writeUnsigned8Le​(java.nio.ByteBuffer output, long value)
      Writes 8 bytes in little-endian format, converting them from a signed 64-bit value.
      • Methods inherited from class java.lang.Object

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

      • readUnsigned8Le

        public static long readUnsigned8Le​(java.nio.ByteBuffer bytes)
                                    throws java.io.IOException
        Reads 8 bytes in little-endian format and converts them into a 64-bit value.
        Parameters:
        bytes - from where should the bytes be read; the first 8 bytes of the source will be read.
        Returns:
        the 64-bit value
        Throws:
        java.io.IOException - failed to read the value.
      • readUnsigned4Le

        public static long readUnsigned4Le​(java.nio.ByteBuffer bytes)
                                    throws java.io.IOException
        Reads 4 bytes in little-endian format and converts them into a 32-bit value.
        Parameters:
        bytes - from where should the bytes be read; the first 4 bytes of the source will be read
        Returns:
        the 32-bit value
        Throws:
        java.io.IOException - failed to read the value
      • readUnsigned2Le

        public static int readUnsigned2Le​(java.nio.ByteBuffer bytes)
                                   throws java.io.IOException
        Reads 2 bytes in little-endian format and converts them into a 16-bit value.
        Parameters:
        bytes - from where should the bytes be read; the first 2 bytes of the source will be read
        Returns:
        the 16-bit value
        Throws:
        java.io.IOException - failed to read the value
      • writeUnsigned8Le

        public static void writeUnsigned8Le​(java.nio.ByteBuffer output,
                                            long value)
                                     throws java.io.IOException
        Writes 8 bytes in little-endian format, converting them from a signed 64-bit value.
        Parameters:
        output - the output stream where the bytes will be written.
        value - the 64-bit value to convert.
        Throws:
        java.io.IOException - failed to write the value data.
      • writeUnsigned4Le

        public static void writeUnsigned4Le​(java.nio.ByteBuffer output,
                                            long value)
                                     throws java.io.IOException
        Writes 4 bytes in little-endian format, converting them from a 32-bit value.
        Parameters:
        output - the output stream where the bytes will be written
        value - the 32-bit value to convert
        Throws:
        java.io.IOException - failed to write the value data
      • writeUnsigned2Le

        public static void writeUnsigned2Le​(java.nio.ByteBuffer output,
                                            int value)
                                     throws java.io.IOException
        Writes 2 bytes in little-endian format, converting them from a 16-bit value.
        Parameters:
        output - the output stream where the bytes will be written
        value - the 16-bit value to convert
        Throws:
        java.io.IOException - failed to write the value data