Class ByteArrayUtils


  • public class ByteArrayUtils
    extends Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte[] EMPTY  
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteArrayUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compare​(byte[] a1, byte[] a2)
      lexicographic comparison which returns 0 if one of the array is a subarray of the other one
      static int decode3Bytes​(byte[] a, int offset)  
      static long decode5Bytes​(byte[] a, int offset)  
      static long decode6Bytes​(byte[] a, int offset)  
      static int decodeInt​(byte[] a, int offset)  
      static int decodeIntLE​(byte[] a, int offset)  
      static long decodeLong​(byte[] a, int offset)  
      static long decodeLongLE​(byte[] a, int offset)  
      static short decodeShort​(byte[] a, int offset)  
      static int decodeShortLE​(byte[] a, int offset)
      Decode short little endian
      static int decodeUnsignedShort​(byte[] a, int offset)  
      static int decodeUnsignedShortLE​(byte[] a, int offset)  
      static byte[] encode3Bytes​(int x, byte[] a, int offset)  
      static byte[] encode5Bytes​(long x, byte[] a, int offset)  
      static byte[] encode6Bytes​(long x, byte[] a, int offset)  
      static byte[] encodeInt​(int x)  
      static byte[] encodeInt​(int x, byte[] a, int offset)
      write an int into a byte array at offset and returns the array
      static byte[] encodeLong​(long x)
      write a long in to a byte array of 8 bytes
      static byte[] encodeLong​(long x, byte[] a, int offset)
      write an long into a byte array at offset and returns the array
      static byte[] encodeLongLE​(long x, byte[] a, int offset)  
      static byte[] encodeShort​(int x, byte[] a, int offset)  
      static boolean equalsPrefix​(byte[] a, byte[] b, int n)
      Compares the first n bytes of two arrays.
      static byte[] minusOne​(byte[] a)  
      static byte[] plusOne​(byte[] a)
      If the array is considered binary representation of an integer, add 1 to the integer and returns the corresponding binary representation.
      static boolean startsWith​(byte[] a, byte[] b)
      returns true if a starts with b
    • Field Detail

      • EMPTY

        public static final byte[] EMPTY
    • Constructor Detail

      • ByteArrayUtils

        public ByteArrayUtils()
    • Method Detail

      • startsWith

        public static boolean startsWith​(byte[] a,
                                         byte[] b)
        returns true if a starts with b
        Parameters:
        a -
        b -
        Returns:
        true if a and b are not null, a.length ≥ b.length and a[i]=b[i] for i=0...b.length-1
        Throws:
        NullPointerException - if any of them is null
      • equalsPrefix

        public static boolean equalsPrefix​(byte[] a,
                                           byte[] b,
                                           int n)
        Compares the first n bytes of two arrays. The arrays must be at least n bytes long (otherwise false is returned)
        Parameters:
        a - - the first array to compare
        b - - the second array to compare
        n - - the number of bytes to compare
        Returns:
        true if a.length >= n, b.length >= n and a[i]==b[i] for i=0..n-1
        Throws:
        NullPointerException - if any of them is null
      • plusOne

        public static byte[] plusOne​(byte[] a)
        If the array is considered binary representation of an integer, add 1 to the integer and returns the corresponding binary representation. In case an overflow is detected (if the initial array was all 0XFF) an IllegalArgumentException is thrown.
        Parameters:
        a -
        Returns:
        a+1
      • minusOne

        public static byte[] minusOne​(byte[] a)
      • compare

        public static int compare​(byte[] a1,
                                  byte[] a2)
        lexicographic comparison which returns 0 if one of the array is a subarray of the other one
        Parameters:
        a1 -
        a2 -
      • encodeInt

        public static byte[] encodeInt​(int x,
                                       byte[] a,
                                       int offset)
        write an int into a byte array at offset and returns the array
      • encodeInt

        public static byte[] encodeInt​(int x)
      • encodeLong

        public static byte[] encodeLong​(long x,
                                        byte[] a,
                                        int offset)
        write an long into a byte array at offset and returns the array
      • encodeLongLE

        public static byte[] encodeLongLE​(long x,
                                          byte[] a,
                                          int offset)
      • encodeLong

        public static byte[] encodeLong​(long x)
        write a long in to a byte array of 8 bytes
        Parameters:
        x -
        Returns:
      • decodeLong

        public static long decodeLong​(byte[] a,
                                      int offset)
      • decodeLongLE

        public static long decodeLongLE​(byte[] a,
                                        int offset)
      • encode6Bytes

        public static byte[] encode6Bytes​(long x,
                                          byte[] a,
                                          int offset)
      • decode6Bytes

        public static long decode6Bytes​(byte[] a,
                                        int offset)
      • encode5Bytes

        public static byte[] encode5Bytes​(long x,
                                          byte[] a,
                                          int offset)
      • decode5Bytes

        public static long decode5Bytes​(byte[] a,
                                        int offset)
      • decodeInt

        public static int decodeInt​(byte[] a,
                                    int offset)
      • decodeIntLE

        public static int decodeIntLE​(byte[] a,
                                      int offset)
      • encode3Bytes

        public static byte[] encode3Bytes​(int x,
                                          byte[] a,
                                          int offset)
      • decode3Bytes

        public static int decode3Bytes​(byte[] a,
                                       int offset)
      • encodeShort

        public static byte[] encodeShort​(int x,
                                         byte[] a,
                                         int offset)
      • decodeShort

        public static short decodeShort​(byte[] a,
                                        int offset)
      • decodeUnsignedShort

        public static int decodeUnsignedShort​(byte[] a,
                                              int offset)
      • decodeShortLE

        public static int decodeShortLE​(byte[] a,
                                        int offset)
        Decode short little endian
        Parameters:
        a -
        offset -
        Returns:
      • decodeUnsignedShortLE

        public static int decodeUnsignedShortLE​(byte[] a,
                                                int offset)