Class ByteUtils


  • public final class ByteUtils
    extends java.lang.Object
    Utility methods for reading and writing bytes.
    Since:
    1.14
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte[] EMPTY_BYTE_ARRAY
      Empty array.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long fromLittleEndian​(byte[] bytes)
      Reads the given byte array as a little endian long.
      static long fromLittleEndian​(byte[] bytes, int off, int length)
      Reads the given byte array as a little endian long.
      static long fromLittleEndian​(java.io.DataInput in, int length)
      Reads the given number of bytes from the given input as little endian long.
      static long fromLittleEndian​(java.io.InputStream in, int length)
      Reads the given number of bytes from the given stream as a little endian long.
      static long fromLittleEndian​(ByteUtils.ByteSupplier supplier, int length)
      Reads the given number of bytes from the given supplier as a little endian long.
      static void toLittleEndian​(byte[] b, long value, int off, int length)
      Inserts the given value into the array as a little endian sequence of the given length starting at the given offset.
      static void toLittleEndian​(java.io.DataOutput out, long value, int length)
      Writes the given value to the given stream as a little endian array of the given length.
      static void toLittleEndian​(java.io.OutputStream out, long value, int length)
      Writes the given value to the given stream as a little endian array of the given length.
      static void toLittleEndian​(ByteUtils.ByteConsumer consumer, long value, int length)
      Provides the given value to the given consumer as a little endian sequence of the given length.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
        Empty array.
        Since:
        1.21
    • Method Detail

      • fromLittleEndian

        public static long fromLittleEndian​(byte[] bytes)
        Reads the given byte array as a little endian long.
        Parameters:
        bytes - the byte array to convert
        Returns:
        the number read
      • fromLittleEndian

        public static long fromLittleEndian​(byte[] bytes,
                                            int off,
                                            int length)
        Reads the given byte array as a little endian long.
        Parameters:
        bytes - the byte array to convert
        off - the offset into the array that starts the value
        length - the number of bytes representing the value
        Returns:
        the number read
        Throws:
        java.lang.IllegalArgumentException - if len is bigger than eight
      • fromLittleEndian

        public static long fromLittleEndian​(java.io.InputStream in,
                                            int length)
                                     throws java.io.IOException
        Reads the given number of bytes from the given stream as a little endian long.
        Parameters:
        in - the stream to read from
        length - the number of bytes representing the value
        Returns:
        the number read
        Throws:
        java.lang.IllegalArgumentException - if len is bigger than eight
        java.io.IOException - if reading fails or the stream doesn't contain the given number of bytes anymore
      • fromLittleEndian

        public static long fromLittleEndian​(ByteUtils.ByteSupplier supplier,
                                            int length)
                                     throws java.io.IOException
        Reads the given number of bytes from the given supplier as a little endian long.

        Typically used by our InputStreams that need to count the bytes read as well.

        Parameters:
        supplier - the supplier for bytes
        length - the number of bytes representing the value
        Returns:
        the number read
        Throws:
        java.lang.IllegalArgumentException - if len is bigger than eight
        java.io.IOException - if the supplier fails or doesn't supply the given number of bytes anymore
      • fromLittleEndian

        public static long fromLittleEndian​(java.io.DataInput in,
                                            int length)
                                     throws java.io.IOException
        Reads the given number of bytes from the given input as little endian long.
        Parameters:
        in - the input to read from
        length - the number of bytes representing the value
        Returns:
        the number read
        Throws:
        java.lang.IllegalArgumentException - if len is bigger than eight
        java.io.IOException - if reading fails or the stream doesn't contain the given number of bytes anymore
      • toLittleEndian

        public static void toLittleEndian​(byte[] b,
                                          long value,
                                          int off,
                                          int length)
        Inserts the given value into the array as a little endian sequence of the given length starting at the given offset.
        Parameters:
        b - the array to write into
        value - the value to insert
        off - the offset into the array that receives the first byte
        length - the number of bytes to use to represent the value
      • toLittleEndian

        public static void toLittleEndian​(java.io.OutputStream out,
                                          long value,
                                          int length)
                                   throws java.io.IOException
        Writes the given value to the given stream as a little endian array of the given length.
        Parameters:
        out - the stream to write to
        value - the value to write
        length - the number of bytes to use to represent the value
        Throws:
        java.io.IOException - if writing fails
      • toLittleEndian

        public static void toLittleEndian​(ByteUtils.ByteConsumer consumer,
                                          long value,
                                          int length)
                                   throws java.io.IOException
        Provides the given value to the given consumer as a little endian sequence of the given length.
        Parameters:
        consumer - the consumer to provide the bytes to
        value - the value to provide
        length - the number of bytes to use to represent the value
        Throws:
        java.io.IOException - if writing fails
      • toLittleEndian

        public static void toLittleEndian​(java.io.DataOutput out,
                                          long value,
                                          int length)
                                   throws java.io.IOException
        Writes the given value to the given stream as a little endian array of the given length.
        Parameters:
        out - the output to write to
        value - the value to write
        length - the number of bytes to use to represent the value
        Throws:
        java.io.IOException - if writing fails