com.google.common.primitives
Class Bytes

java.lang.Object
  extended by com.google.common.primitives.Bytes

public final class Bytes
extends Object

Static utility methods pertaining to byte primitives, that are not already found in either Byte or Arrays, and interpret bytes as neither signed nor unsigned. The methods which specifically treat bytes as signed or unsigned are found in SignedBytes and UnsignedBytes.

Since:
2009.09.15 tentative
Author:
Kevin Bourrillion

Method Summary
static List<Byte> asList(byte... backingArray)
          Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
static byte[] concat(byte[]... arrays)
          Returns the values from each provided array combined into a single array.
static boolean contains(byte[] array, byte target)
          Returns true if target is present as an element anywhere in array.
static byte[] ensureCapacity(byte[] array, int minLength, int padding)
          Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.
static int hashCode(byte value)
          Returns a hash code for value; equal to the result of invoking ((Byte) value).hashCode().
static int indexOf(byte[] array, byte target)
          Returns the index of the first appearance of the value target in array.
static int indexOf(byte[] array, byte[] target)
          Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.
static int lastIndexOf(byte[] array, byte target)
          Returns the index of the last appearance of the value target in array.
static byte[] toArray(Collection<Byte> collection)
          Copies a collection of Byte instances into a new array of primitive byte values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

hashCode

public static int hashCode(byte value)
Returns a hash code for value; equal to the result of invoking ((Byte) value).hashCode().

Parameters:
value - a primitive byte value
Returns:
a hash code for the value

contains

public static boolean contains(byte[] array,
                               byte target)
Returns true if target is present as an element anywhere in array.

Parameters:
array - an array of byte values, possibly empty
target - a primitive byte value
Returns:
true if array[i] == target for some value of i

indexOf

public static int indexOf(byte[] array,
                          byte target)
Returns the index of the first appearance of the value target in array.

Parameters:
array - an array of byte values, possibly empty
target - a primitive byte value
Returns:
the least index i for which array[i] == target, or -1 if no such index exists.

indexOf

public static int indexOf(byte[] array,
                          byte[] target)
Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

More formally, returns the lowest index i such that java.util.Arrays.copyOfRange(array, i, i + target.length) contains exactly the same elements as target.

Parameters:
array - the array to search for the sequence target
target - the array to search for as a sub-sequence of array

lastIndexOf

public static int lastIndexOf(byte[] array,
                              byte target)
Returns the index of the last appearance of the value target in array.

Parameters:
array - an array of byte values, possibly empty
target - a primitive byte value
Returns:
the greatest index i for which array[i] == target, or -1 if no such index exists.

concat

public static byte[] concat(byte[]... arrays)
Returns the values from each provided array combined into a single array. For example, concat(new byte[] {a, b}, new byte[] {}, new byte[] {c} returns the array {a, b, c}.

Parameters:
arrays - zero or more byte arrays
Returns:
a single array containing all the values from the source arrays, in order

ensureCapacity

public static byte[] ensureCapacity(byte[] array,
                                    int minLength,
                                    int padding)
Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. If array already has a length of at least minLength, it is returned directly. Otherwise, a new array of size minLength + padding is returned, containing the values of array, and zeroes in the remaining places.

Parameters:
array - the source array
minLength - the minimum length the returned array must guarantee
padding - an extra amount to "grow" the array by if growth is necessary
Returns:
an array containing the values of array, with guaranteed minimum length minLength
Throws:
IllegalArgumentException - if minLength or padding is negative

toArray

public static byte[] toArray(Collection<Byte> collection)
Copies a collection of Byte instances into a new array of primitive byte values.

Elements are copied from the argument collection as if by collection.toArray(). Calling this method is as thread-safe as calling that method.

Parameters:
collection - a collection of Byte objects
Returns:
an array containing the same values as collection, in the same order, converted to primitives
Throws:
NullPointerException - if collection or any of its elements is null

asList

public static List<Byte> asList(byte... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). The list supports List.set(int, Object), but any attempt to set a value to null will result in a NullPointerException.

The returned list maintains the values, but not the identities, of Byte objects written to or read from it. For example, whether list.get(0) == list.get(0) is true for the returned list is unspecified.

Parameters:
backingArray - the array to back the list
Returns:
a list view of the array