Class ByteArray


  • public class ByteArray
    extends java.lang.Object

    Various static methods helpful for manipulating arrays of bytes and extracting values from them.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int bigEndianToUnsignedInt​(byte[] b)
      Extract an unsigned big endian integer from an entire byte array.
      static int bigEndianToUnsignedInt​(byte[] b, int offset, int length)
      Extract an unsigned big endian integer from a byte array.
      static byte[] concatenate​(byte[] src1, byte[] src2)
      Concatenate the entire contents of two byte arrays into a new byte array.
      static byte[] concatenate​(byte[] src1, int offset1, int length1, byte[] src2, int offset2, int length2)
      Concatenate portions of two byte arrays into a new byte array.
      static byte[] extractBytes​(byte[] src, int offset, int length)
      Extract a portion of a byte array into a new byte array.
      static byte[] intToBigEndianArray​(int value, int length)
      Extract an integer into a big endian byte array.
      static byte[] intToLittleEndianArray​(int value, int length)
      Extract an integer into a little endian byte array.
      static void main​(java.lang.String[] arg)
      Testing.
      static byte[] readFully​(java.lang.String filename)  
      static void swapEndianness​(byte[] src, int byteCount, int wordLength)
      Swap the byte order (endianness) of fixed length words within a byte array.
      • Methods inherited from class java.lang.Object

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

      • readFully

        public static final byte[] readFully​(java.lang.String filename)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • bigEndianToUnsignedInt

        public static final int bigEndianToUnsignedInt​(byte[] b,
                                                       int offset,
                                                       int length)

        Extract an unsigned big endian integer from a byte array.

        Parameters:
        b - the byte array containing the big endian encoded integer
        offset - the offset of the start of the integer in the byte array
        length - the number of bytes that the integer occupies in the byte array
        Returns:
        the unsigned integer value
      • bigEndianToUnsignedInt

        public static final int bigEndianToUnsignedInt​(byte[] b)

        Extract an unsigned big endian integer from an entire byte array.

        Parameters:
        b - the byte array which is the big endian encoded integer
        Returns:
        the unsigned integer value
      • intToBigEndianArray

        public static final byte[] intToBigEndianArray​(int value,
                                                       int length)

        Extract an integer into a big endian byte array.

        Parameters:
        value - the integer value
        length - the length of the byte array
        Returns:
        the byte array which is the big endian encoded integer
      • intToLittleEndianArray

        public static final byte[] intToLittleEndianArray​(int value,
                                                          int length)

        Extract an integer into a little endian byte array.

        Parameters:
        value - the integer value
        length - the length of the byte array
        Returns:
        the byte array which is the little endian encoded integer
      • extractBytes

        public static final byte[] extractBytes​(byte[] src,
                                                int offset,
                                                int length)

        Extract a portion of a byte array into a new byte array.

        Parameters:
        src - the source byte array
        offset - the offset of the bytes to be extracted
        length - the number of bytes to be extracted
        Returns:
        a new byte array containing the extracted bytes
      • concatenate

        public static final byte[] concatenate​(byte[] src1,
                                               int offset1,
                                               int length1,
                                               byte[] src2,
                                               int offset2,
                                               int length2)

        Concatenate portions of two byte arrays into a new byte array.

        Parameters:
        src1 - the first byte array
        offset1 - the offset of the bytes to be extracted from the first byte array
        length1 - the number of bytes to be extracted from the first byte array
        src2 - the second byte array
        offset2 - the offset of the bytes to be extracted from the second byte array
        length2 - the number of bytes to be extracted from the second byte array
        Returns:
        a new byte array containing the bytes extracted from the first array followed by the bytes extracted from the second array
      • concatenate

        public static final byte[] concatenate​(byte[] src1,
                                               byte[] src2)

        Concatenate the entire contents of two byte arrays into a new byte array.

        Parameters:
        src1 - the first byte array
        src2 - the second byte array
        Returns:
        a new byte array containing the bytes extracted from the first array followed by the bytes extracted from the second array
      • swapEndianness

        public static final void swapEndianness​(byte[] src,
                                                int byteCount,
                                                int wordLength)

        Swap the byte order (endianness) of fixed length words within a byte array.

        Parameters:
        src - the byte array (swapped in place)
        byteCount - the number of bytes in the array to swap
        wordLength - the length in bytes of each word
      • main

        public static void main​(java.lang.String[] arg)

        Testing.

        Parameters:
        arg - ignored