Class Bytes


  • public class Bytes
    extends java.lang.Object
    Utilities for manipulating bytes.
    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte[] NO_BYTES
      A shared empty array of bytes.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] createRandom​(int length)
      Creates an array of random bytes.
      static byte[] createRandom​(int length, java.util.Random random)
      Creates an array of random bytes.
      static boolean startsWith​(byte[] bytes, byte[] prefix)
      Determines if the given byte array starts with the specified prefix.
      static java.lang.String toHexString​(byte[] bytes)
      Converts an array of bytes into a hex string, with each character pair representing the hexadecimal value of the byte.
      • Methods inherited from class java.lang.Object

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

      • NO_BYTES

        public static final byte[] NO_BYTES
        A shared empty array of bytes.
    • Method Detail

      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)
        Converts an array of bytes into a hex string, with each character pair representing the hexadecimal value of the byte.
        Parameters:
        bytes - The values to convert.
        Returns:
        A lowercase string with hexadecimal digits, each pair representing a byte in the byte array.
      • createRandom

        public static byte[] createRandom​(int length)
        Creates an array of random bytes.
        Parameters:
        length - The number of bytes to create.
        Returns:
        A new array of the given length filled with random bytes.
        Throws:
        java.lang.IllegalArgumentException - if the given length is negative.
      • createRandom

        public static byte[] createRandom​(int length,
                                          java.util.Random random)
        Creates an array of random bytes.
        Parameters:
        length - The number of bytes to create.
        random - The random number generator to use.
        Returns:
        A new array of the given length filled with random bytes.
        Throws:
        java.lang.NullPointerException - if the given random number generator is null.
        java.lang.IllegalArgumentException - if the given length is negative.
      • startsWith

        public static boolean startsWith​(byte[] bytes,
                                         byte[] prefix)
        Determines if the given byte array starts with the specified prefix. Neither byte array is modified by this method.
        Parameters:
        bytes - The bytes being examined.
        prefix - The prefix to compare with the given bytes.
        Returns:
        Whether the given bytes start with the specified prefix.