Top Description Inners Constructors Methods
com.google.common.primitives

public final Class Bytes

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkElementIndex, .Preconditions.checkNotNull, .Preconditions.checkPositionIndexes

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.

See the Guava User Guide article on primitive utilities.

Author
Kevin Bourrillion
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static List<Byte>

Returns:

a list view of the array
asList
(byte...
the array to back the list
backingArray
)

Returns a fixed-size list backed by the specified array, similar to Arrays#asList(Object[]).

public static byte[]

Returns:

a single array containing all the values from the source arrays, in order
concat
(byte[]...
zero or more byte arrays
arrays
)

Returns the values from each provided array combined into a single array.

public static boolean

Returns:

true if array[i] == target for some value of i
contains
(byte[]
an array of byte values, possibly empty
array
,
byte
a primitive byte value
target
)

Returns true if target is present as an element anywhere in array.

public static byte[]

Returns:

an array containing the values of array, with guaranteed minimum length minLength
ensureCapacity
(byte[]
the source array
array
,
int
the minimum length the returned array must guarantee
minLength
,
int
an extra amount to "grow" the array by if growth is necessary
padding
)

Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.

public static int

Returns:

a hash code for the value
hashCode
(byte
a primitive byte value
value
)

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

public static int

Returns:

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

Returns the index of the first appearance of the value target in array.

private static int
indexOf(byte[] array, byte target, int start, int end)

public static int
indexOf(byte[]
the array to search for the sequence target
array
,
byte[]
the array to search for as a sub-sequence of array
target
)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

public static int

Returns:

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

Returns the index of the last appearance of the value target in array.

private static int
lastIndexOf(byte[] array, byte target, int start, int end)

public static void
reverse(byte[] array)

Reverses the elements of array.

public static void
reverse(byte[] array, int fromIndex, int toIndex)

Reverses the elements of array between fromIndex inclusive and toIndex exclusive.

public static void
rotate(byte[] array, int distance)

Performs a right rotation of array of "distance" places, so that the first element is moved to index "distance", and the element at index i ends up at index (distance + i) mod array.length.

public static void
rotate(byte[] array, int distance, int fromIndex, int toIndex)

Performs a right rotation of array between fromIndex inclusive and toIndex exclusive.

public static byte[]

Returns:

an array containing the same values as collection, in the same order, converted to primitives
toArray
(Collection<? extends Number>
a collection of Number instances
collection
)

Returns an array containing each value of collection, converted to a byte value in the manner of Number#byteValue.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Bytesback to summary
private Bytes()

Method Detail

asListback to summary
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.

The returned list is serializable.

Parameters
backingArray:byte[]

the array to back the list

Returns:List<Byte>

a list view of the array

concatback to summary
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:byte[][]

zero or more byte arrays

Returns:byte[]

a single array containing all the values from the source arrays, in order

containsback to summary
public static boolean contains(byte[] array, byte target)

Returns true if target is present as an element anywhere in array.

Parameters
array:byte[]

an array of byte values, possibly empty

target:byte

a primitive byte value

Returns:boolean

true if array[i] == target for some value of i

ensureCapacityback to summary
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:byte[]

the source array

minLength:int

the minimum length the returned array must guarantee

padding:int

an extra amount to "grow" the array by if growth is necessary

Returns:byte[]

an array containing the values of array, with guaranteed minimum length minLength

Exceptions
IllegalArgumentException:
if minLength or padding is negative
hashCodeback to summary
public static int hashCode(byte value)

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

Java 8+ users: use Byte#hashCode(byte) instead.

Parameters
value:byte

a primitive byte value

Returns:int

a hash code for the value

indexOfback to summary
public static int indexOf(byte[] array, byte target)

Returns the index of the first appearance of the value target in array.

Parameters
array:byte[]

an array of byte values, possibly empty

target:byte

a primitive byte value

Returns:int

the least index i for which array[i] == target, or -1 if no such index exists.

indexOfback to summary
private static int indexOf(byte[] array, byte target, int start, int end)
indexOfback to summary
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 Arrays.copyOfRange(array, i, i + target.length) contains exactly the same elements as target.

Parameters
array:byte[]

the array to search for the sequence target

target:byte[]

the array to search for as a sub-sequence of array

lastIndexOfback to summary
public static int lastIndexOf(byte[] array, byte target)

Returns the index of the last appearance of the value target in array.

Parameters
array:byte[]

an array of byte values, possibly empty

target:byte

a primitive byte value

Returns:int

the greatest index i for which array[i] == target, or -1 if no such index exists.

lastIndexOfback to summary
private static int lastIndexOf(byte[] array, byte target, int start, int end)
reverseback to summary
public static void reverse(byte[] array)

Reverses the elements of array. This is equivalent to Collections.reverse(Bytes.asList(array)), but is likely to be more efficient.

Since
23.1
reverseback to summary
public static void reverse(byte[] array, int fromIndex, int toIndex)

Reverses the elements of array between fromIndex inclusive and toIndex exclusive. This is equivalent to Collections.reverse(Bytes.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.

Exceptions
IndexOutOfBoundsException:
if fromIndex < 0, toIndex > array.length, or toIndex > fromIndex
Since
23.1
rotateback to summary
public static void rotate(byte[] array, int distance)

Performs a right rotation of array of "distance" places, so that the first element is moved to index "distance", and the element at index i ends up at index (distance + i) mod array.length. This is equivalent to Collections.rotate(Bytes.asList(array), distance), but is somewhat faster.

The provided "distance" may be negative, which will rotate left.

Since
32.0.0
rotateback to summary
public static void rotate(byte[] array, int distance, int fromIndex, int toIndex)

Performs a right rotation of array between fromIndex inclusive and toIndex exclusive. This is equivalent to Collections.rotate(Bytes.asList(array).subList(fromIndex, toIndex), distance), but is somewhat faster.

The provided "distance" may be negative, which will rotate left.

Exceptions
IndexOutOfBoundsException:
if fromIndex < 0, toIndex > array.length, or toIndex > fromIndex
Since
32.0.0
toArrayback to summary
public static byte[] toArray(Collection<? extends Number> collection)

Returns an array containing each value of collection, converted to a byte value in the manner of Number#byteValue.

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:Collection<? extends Number>

a collection of Number instances

Returns:byte[]

an array containing the same values as collection, in the same order, converted to primitives

Exceptions
NullPointerException:
if collection or any of its elements is null
Since
1.0 (parameter was Collection<Byte> before 12.0)
com.google.common.primitives back to summary

private Class Bytes.ByteArrayAsList

extends AbstractList<Byte>
implements RandomAccess, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.RandomAccess
Annotations
@GwtCompatible

Field Summary

Modifier and TypeField and Description
pack-priv final byte[]
pack-priv final int
private static final long
pack-priv final int
Inherited from java.util.AbstractList:
modCount

Constructor Summary

AccessConstructor and Description
pack-priv
ByteArrayAsList(byte[] array)

pack-priv
ByteArrayAsList(byte[] array, int start, int end)

Method Summary

Modifier and TypeMethod and Description
public boolean
contains(Object
element whose presence in this list is to be tested
target
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.List.contains, java.util.Collection.contains.

Returns true if this list contains the specified element.
public boolean
equals(Object
the object to be compared for equality with this list
object
)

Overrides java.util.AbstractList.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Compares the specified object with this list for equality.
public Byte
get(int
index of the element to return
index
)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Returns the element at the specified position in this list.
public int
hashCode()

Overrides java.util.AbstractList.hashCode.

Implements java.util.List.hashCode, java.util.Collection.hashCode.

Returns the hash code value for this list.
public int
indexOf(Object
element to search for
target
)

Overrides java.util.AbstractList.indexOf.

Implements java.util.List.indexOf.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.List.isEmpty, java.util.Collection.isEmpty.

Returns true if this list contains no elements.
public int
lastIndexOf(Object
element to search for
target
)

Overrides java.util.AbstractList.lastIndexOf.

Implements java.util.List.lastIndexOf.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
public Byte
set(int
index of the element to replace
index
,
Byte
element to be stored at the specified position
element
)

Overrides java.util.AbstractList.set.

Implements java.util.List.set.

Replaces the element at the specified position in this list with the specified element (optional operation).
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Returns the number of elements in this list.
public List<Byte>
subList(int
low endpoint (inclusive) of the subList
fromIndex
,
int
high endpoint (exclusive) of the subList
toIndex
)

Overrides java.util.AbstractList.subList.

Implements java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
pack-priv byte[]
public String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.
Inherited from java.util.AbstractList:
addaddaddAllcleariteratorlistIteratorlistIteratorremoveremoveRange

Field Detail

arrayback to summary
pack-priv final byte[] array
endback to summary
pack-priv final int end
serialVersionUIDback to summary
private static final long serialVersionUID
startback to summary
pack-priv final int start

Constructor Detail

ByteArrayAsListback to summary
pack-priv ByteArrayAsList(byte[] array)
ByteArrayAsListback to summary
pack-priv ByteArrayAsList(byte[] array, int start, int end)

Method Detail

containsback to summary
public boolean contains(Object target)

Overrides java.util.AbstractCollection.contains.

Implements java.util.List.contains, java.util.Collection.contains.

Doc from java.util.List.contains.

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).

Parameters
target:Object

element whose presence in this list is to be tested

Returns:boolean

true if this list contains the specified element

Annotations
@Override
equalsback to summary
public boolean equals(Object object)

Overrides java.util.AbstractList.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Doc from java.util.AbstractList.equals.

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.

Parameters
object:Object

the object to be compared for equality with this list

Returns:boolean

true if the specified object is equal to this list

Annotations
@Override
getback to summary
public Byte get(int index)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Doc from java.util.AbstractList.get.

Returns the element at the specified position in this list.

Parameters
index:int

Doc from java.util.List.get.

index of the element to return

Returns:Byte

Doc from java.util.List.get.

the element at the specified position in this list

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.util.AbstractList.hashCode.

Implements java.util.List.hashCode, java.util.Collection.hashCode.

Doc from java.util.AbstractList.hashCode.

Returns the hash code value for this list.

Returns:int

the hash code value for this list

Annotations
@Override
indexOfback to summary
public int indexOf(Object target)

Overrides java.util.AbstractList.indexOf.

Implements java.util.List.indexOf.

Doc from java.util.AbstractList.indexOf.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

Parameters
target:Object

Doc from java.util.List.indexOf.

element to search for

Returns:int

Doc from java.util.List.indexOf.

the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.List.isEmpty, java.util.Collection.isEmpty.

Doc from java.util.List.isEmpty.

Returns true if this list contains no elements.

Returns:boolean

true if this list contains no elements

Annotations
@Override
lastIndexOfback to summary
public int lastIndexOf(Object target)

Overrides java.util.AbstractList.lastIndexOf.

Implements java.util.List.lastIndexOf.

Doc from java.util.AbstractList.lastIndexOf.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

Parameters
target:Object

Doc from java.util.List.lastIndexOf.

element to search for

Returns:int

Doc from java.util.List.lastIndexOf.

the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element

Annotations
@Override
setback to summary
public Byte set(int index, Byte element)

Overrides java.util.AbstractList.set.

Implements java.util.List.set.

Doc from java.util.AbstractList.set.

Replaces the element at the specified position in this list with the specified element (optional operation).

Parameters
index:int

Doc from java.util.List.set.

index of the element to replace

element:Byte

Doc from java.util.List.set.

element to be stored at the specified position

Returns:Byte

Doc from java.util.List.set.

the element previously at the specified position

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Doc from java.util.List.size.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this list

Annotations
@Override
subListback to summary
public List<Byte> subList(int fromIndex, int toIndex)

Overrides java.util.AbstractList.subList.

Implements java.util.List.subList.

Doc from java.util.AbstractList.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Parameters
fromIndex:int

Doc from java.util.List.subList.

low endpoint (inclusive) of the subList

toIndex:int

Doc from java.util.List.subList.

high endpoint (exclusive) of the subList

Returns:List<Byte>

Doc from java.util.List.subList.

a view of the specified range within this list

Annotations
@Override
toByteArrayback to summary
pack-priv byte[] toByteArray()
toStringback to summary
public String toString()

Overrides java.util.AbstractCollection.toString.

Doc from java.util.AbstractCollection.toString.

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String#valueOf(Object).

Returns:String

a string representation of this collection

Annotations
@Override