Class ByteUtil


  • public final class ByteUtil
    extends java.lang.Object
    Byte utitlity.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void copyBlock​(byte[] src, byte[] dst)
      Copies one byte block to another.
      void copyBlock​(byte[] src, int srcOff, byte[] dst, int dstOff, int len)
      Copies the specified byte sequence of the given source array to the specified destination array.
      void copyBlock​(int[] src, int[] dst)
      Copies one int block to another.
      void copyBlock​(int[] src, int srcOff, int[] dst, int dstOff, int len)
      Copies the specified int sequence of the given source array to the specified destination array.
      boolean equalsBlock​(byte[] a, byte[] b)
      Check two blocks for equality.
      boolean equalsBlock​(byte[] dataA, int offsetDataA, byte[] dataB, int offsetDataB, int len)
      Check two blocks for equality.
      static ByteUtil getInstance()
      Get the instance
      int indexOf​(byte[] source, int sourceOffset, int sourceCount, byte[] target, int targetOffset, int targetCount, int index)
      The source is the byte array being searched, and the target is the string being searched for.
      int toByte​(char c, int radix)
      Convert the given char to an int.
      byte[] toByteArray​(java.lang.String s)
      Convert the given string with hex values to a byte array.
      byte[] toBytes​(byte[] src, int len)
      Prepare a byte array to a byte array of a defined size
      byte[] toBytes​(int inputSrc)
      Convert an integer value to a 4 byte array
      byte[] toBytes​(long src)
      Convert from a long value to a 8 byte array
      int toInteger​(byte[] src)
      Build an integer from first 4 bytes of the array.
      int toInteger​(ByteArray src)
      Build an integer from first 4 bytes of the array.
      long toLong​(byte[] src)
      Build a long from first 8 bytes of the array.
      long toLong​(ByteArray src)
      Build a long from first 8 bytes of the array.
      • Methods inherited from class java.lang.Object

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

      • getInstance

        public static ByteUtil getInstance()
        Get the instance
        Returns:
        the instance
      • equalsBlock

        public boolean equalsBlock​(byte[] dataA,
                                   int offsetDataA,
                                   byte[] dataB,
                                   int offsetDataB,
                                   int len)
        Check two blocks for equality. The specified sub blocks of the given byte arrays are checked for equality.
        Parameters:
        dataA - the first byte array to be compared
        offsetDataA - the offset indicating the start position of the sub-block of a
        dataB - the second byte array to be compared
        offsetDataB - the offset indicating the start position of the sub-block of b
        len - the number of bytes to be compared
        Returns:
        true, if the two sub-blocks are equal, false otherwise
      • equalsBlock

        public boolean equalsBlock​(byte[] a,
                                   byte[] b)
        Check two blocks for equality.
        Parameters:
        a - the first byte array to be compared
        b - the second byte array to be compared
        Returns:
        true, if the two blocks are equal, false otherwise
      • toByteArray

        public byte[] toByteArray​(java.lang.String s)
        Convert the given string with hex values to a byte array. For example "001122" is turned into {0, 0x11, 0x22}. All characters outside the range of '0'-'9', 'a'-'z', and 'A'-'Z' or simply ignored.
        Parameters:
        s - the string to convert
        Returns:
        the byte array
      • toByte

        public int toByte​(char c,
                          int radix)
        Convert the given char to an int. '0' maps to 0, 'a' and 'A' to 10, 'z' and 'Z' to 36, and so on. For all other characters and for resulting values equal to or larger than radix -1 is returned.
        Parameters:
        c - the string to convert
        radix - the radix
        Returns:
        the converted byte
      • copyBlock

        public void copyBlock​(byte[] src,
                              int srcOff,
                              byte[] dst,
                              int dstOff,
                              int len)
        Copies the specified byte sequence of the given source array to the specified destination array. Beginning at the specified srcOff position, len bytes of the source array are copied to the given destination array, starting at dstOff.
        Parameters:
        src - the source byte array
        srcOff - the offset indicating the start position within the first byte array; the following len bytes will be copied to the destination array
        dst - the destination array to which to copy the bytes
        dstOff - the offset indicating the start position within the destination byte array, to which the bytes shall be copied
        len - the number of bytes to be copied
      • copyBlock

        public void copyBlock​(byte[] src,
                              byte[] dst)
        Copies one byte block to another.
        Parameters:
        src - the source byte array
        dst - the destination array to which to copy the bytes
      • copyBlock

        public void copyBlock​(int[] src,
                              int srcOff,
                              int[] dst,
                              int dstOff,
                              int len)
        Copies the specified int sequence of the given source array to the specified destination array. Beginning at the specified srcOff position, len ints of the source array are copied to the given destination array, starting at dstOff.
        Parameters:
        src - the source int array
        srcOff - the offset indicating the start position within the first int array; the following len ints will be copied to the destination array
        dst - the destination array to which to copy the ints
        dstOff - the offset indicating the start position within the destination int array, to which the ints shall be copied
        len - the number of ints to be copied
      • copyBlock

        public void copyBlock​(int[] src,
                              int[] dst)
        Copies one int block to another.
        Parameters:
        src - the source int array
        dst - the destination array to which to copy the ints
      • toBytes

        public byte[] toBytes​(int inputSrc)
        Convert an integer value to a 4 byte array
        Parameters:
        inputSrc - the array to convert (must be of size 4)
        Returns:
        the converted byte array
      • toBytes

        public byte[] toBytes​(long src)
        Convert from a long value to a 8 byte array
        Parameters:
        src - the array to convert (must be of size 8)
        Returns:
        the converted byte array
      • toBytes

        public byte[] toBytes​(byte[] src,
                              int len)
        Prepare a byte array to a byte array of a defined size
        Parameters:
        src - the array to prepare
        len - the size of the returned byte array
        Returns:
        the prepared byte array with the defined size
      • toInteger

        public int toInteger​(ByteArray src)
        Build an integer from first 4 bytes of the array.
        Parameters:
        src - The byte[] to convert.
        Returns:
        the integer value.
      • toInteger

        public int toInteger​(byte[] src)
        Build an integer from first 4 bytes of the array.
        Parameters:
        src - The byte[] to convert.
        Returns:
        the integer value.
      • toLong

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

        public long toLong​(ByteArray src)
        Build a long from first 8 bytes of the array.
        Parameters:
        src - The byte[] to convert.
        Returns:
        the long value.
      • indexOf

        public int indexOf​(byte[] source,
                           int sourceOffset,
                           int sourceCount,
                           byte[] target,
                           int targetOffset,
                           int targetCount,
                           int index)
        The source is the byte array being searched, and the target is the string being searched for.
        Parameters:
        source - the bytes being searched.
        sourceOffset - offset of the source array.
        sourceCount - count of the source array.
        target - the bytes being searched for.
        targetOffset - offset of the target array.
        targetCount - count of the target array.
        index - the index to begin searching from.
        Returns:
        the index within this array of the first occurrence of the specified sub array, starting at the specified index.