Class Bytes


  • public final class Bytes
    extends java.lang.Object
    This class came from the Apache Commons Id sandbox project in support of the UUIDGenerator implementation.

    Static methods for managing byte arrays (all methods follow Big Endian order where most significant bits are in front).

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] append​(byte[] a, byte[] b)
      Appends two bytes array into one.
      static boolean areEqual​(byte[] a, byte[] b)
      Compares two byte arrays for equality.
      static int compareTo​(byte[] lhs, byte[] rhs)
      Compares two byte arrays as specified by Comparable.
      static byte[] toBytes​(long n)
      Returns a 8-byte array built from a long.
      static byte[] toBytes​(long n, byte[] b)
      Build a 8-byte array from a long.
      static long toLong​(byte[] b)
      Build a long from first 8 bytes of the array.
      static short toShort​(byte[] b)
      Build a short from first 2 bytes of the array.
      • Methods inherited from class java.lang.Object

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

      • append

        public static byte[] append​(byte[] a,
                                    byte[] b)
        Appends two bytes array into one.
        Parameters:
        a - A byte[].
        b - A byte[].
        Returns:
        A byte[].
      • toBytes

        public static byte[] toBytes​(long n)
        Returns a 8-byte array built from a long.
        Parameters:
        n - The number to convert.
        Returns:
        A byte[].
      • toBytes

        public static byte[] toBytes​(long n,
                                     byte[] b)
        Build a 8-byte array from a long. No check is performed on the array length.
        Parameters:
        n - The number to convert.
        b - The array to fill.
        Returns:
        A byte[].
      • toLong

        public static long toLong​(byte[] b)
        Build a long from first 8 bytes of the array.
        Parameters:
        b - The byte[] to convert.
        Returns:
        A long.
      • areEqual

        public static boolean areEqual​(byte[] a,
                                       byte[] b)
        Compares two byte arrays for equality.
        Parameters:
        a - A byte[].
        b - A byte[].
        Returns:
        True if the arrays have identical contents.
      • compareTo

        public static int compareTo​(byte[] lhs,
                                    byte[] rhs)

        Compares two byte arrays as specified by Comparable.

        Parameters:
        lhs - - left hand value in the comparison operation.
        rhs - - right hand value in the comparison operation.
        Returns:
        a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs.
      • toShort

        public static short toShort​(byte[] b)
        Build a short from first 2 bytes of the array.
        Parameters:
        b - The byte[] to convert.
        Returns:
        A short.