Class ByteArrayUtil


  • public class ByteArrayUtil
    extends java.lang.Object
    Various static utilities for dealing with byte arrays and formatting them. Created on Jul 26, 2005
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int byteArrayToInt​(byte[] raw, int offset, int length, boolean bigEndian)
      Converts an array of bytes into an unsigned integer.
      static long byteArrayToLong​(byte[] raw, int offset, int length, boolean bigEndian)
      Converts an array of bytes into a long.
      static java.lang.String formatByteArray​(byte[] raw)
      Simple formatting of byte array into a "ab cd ef" kind of a string
      static java.lang.String formatByteArray​(byte[] raw, boolean useSpace)
      Formats a byte array with possible spaces between bytes.
      static java.lang.String formatByteArray​(byte[] raw, int start, int length, boolean useSpace, boolean upperCase)
      Formats a byte array into a string.
      static void longToByteArray​(long value, byte[] dest, int offset, boolean bigEndian)
      Takes a long and lays it out into a destination array in big- or little-endian format.
      • Methods inherited from class java.lang.Object

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

      • formatByteArray

        public static java.lang.String formatByteArray​(byte[] raw,
                                                       boolean useSpace)
        Formats a byte array with possible spaces between bytes.
        Parameters:
        raw - Byte array to format.
        useSpace - flag to specify if space should be present between each byte.
        Returns:
        Byte array formatted as a String.
      • formatByteArray

        public static java.lang.String formatByteArray​(byte[] raw)
        Simple formatting of byte array into a "ab cd ef" kind of a string
        Parameters:
        raw - Byte array to format.
        Returns:
        Byte array formatted as a String.
      • formatByteArray

        public static java.lang.String formatByteArray​(byte[] raw,
                                                       int start,
                                                       int length,
                                                       boolean useSpace,
                                                       boolean upperCase)
        Formats a byte array into a string.
        Parameters:
        raw - Byte array to format.
        start - Start index into raw byte.
        length - Number of bytes to use from start index.
        useSpace - If true, then put space characted between bytes.
        upperCase - If true, then hex values are in uppercase, otherwise lowercase.
        Returns:
        Formatted byte array.
      • byteArrayToInt

        public static int byteArrayToInt​(byte[] raw,
                                         int offset,
                                         int length,
                                         boolean bigEndian)
        Converts an array of bytes into an unsigned integer. The length should be at most 4. When the length is 4 bytes, is there a way to make sure this an unsigned integer without using a long?
        Parameters:
        raw - Byte array to read into integer.
        offset - Start index for reading.
        length - How many bytes to read.
        bigEndian - True if the laid out integer is in big-endian format.
        Returns:
        Integer value
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if specified offset and length don't fit into the array.
      • longToByteArray

        public static void longToByteArray​(long value,
                                           byte[] dest,
                                           int offset,
                                           boolean bigEndian)
        Takes a long and lays it out into a destination array in big- or little-endian format. It will use 8 bytes of the array.
        Parameters:
        value - Long value to convert.
        dest - Destination byte array. Should be at least 8 long.
        offset - Offset into the byte array where the value should be put.
        bigEndian - True if the bytes should be laid out in big endian order.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if there is not enough space.
      • byteArrayToLong

        public static long byteArrayToLong​(byte[] raw,
                                           int offset,
                                           int length,
                                           boolean bigEndian)
        Converts an array of bytes into a long. The length should be at most 8.
        Parameters:
        raw - Byte array for input.
        offset - Index into the byte array where reading should start.
        length - How many bytes to read.
        bigEndian - If true, the big-endian byte order is assumed.
        Returns:
        long value
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if something is wrong with indexes.