Class Bytes
- java.lang.Object
- 
- com.google.common.primitives.Bytes
 
- 
 @GwtCompatible public final class Bytes extends java.lang.Object Static utility methods pertaining tobyteprimitives, that are not already found in eitherByteorArrays, and interpret bytes as neither signed nor unsigned. The methods which specifically treat bytes as signed or unsigned are found inSignedBytesandUnsignedBytes.See the Guava User Guide article on primitive utilities. - Since:
- 1.0
- Author:
- Kevin Bourrillion
 
- 
- 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<java.lang.Byte>asList(byte... backingArray)Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]).static byte[]concat(byte[]... arrays)Returns the values from each provided array combined into a single array.static booleancontains(byte[] array, byte target)Returnstrueiftargetis present as an element anywhere inarray.static byte[]ensureCapacity(byte[] array, int minLength, int padding)Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length.static inthashCode(byte value)Returns a hash code forvalue; equal to the result of invoking((Byte) value).hashCode().static intindexOf(byte[] array, byte target)Returns the index of the first appearance of the valuetargetinarray.static intindexOf(byte[] array, byte[] target)Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.static intlastIndexOf(byte[] array, byte target)Returns the index of the last appearance of the valuetargetinarray.static voidreverse(byte[] array)Reverses the elements ofarray.static voidreverse(byte[] array, int fromIndex, int toIndex)Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive.static voidrotate(byte[] array, int distance)Performs a right rotation ofarrayof "distance" places, so that the first element is moved to index "distance", and the element at indexiends up at index(distance + i) mod array.length.static voidrotate(byte[] array, int distance, int fromIndex, int toIndex)Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive.static byte[]toArray(java.util.Collection<? extends java.lang.Number> collection)Returns an array containing each value ofcollection, converted to abytevalue in the manner ofNumber.byteValue().
 
- 
- 
- 
Method Detail- 
hashCodepublic static int hashCode(byte value) Returns a hash code forvalue; equal to the result of invoking((Byte) value).hashCode().Java 8+ users: use Byte.hashCode(byte)instead.- Parameters:
- value- a primitive- bytevalue
- Returns:
- a hash code for the value
 
 - 
containspublic static boolean contains(byte[] array, byte target) Returnstrueiftargetis present as an element anywhere inarray.- Parameters:
- array- an array of- bytevalues, possibly empty
- target- a primitive- bytevalue
- Returns:
- trueif- array[i] == targetfor some value of- i
 
 - 
indexOfpublic static int indexOf(byte[] array, byte target) Returns the index of the first appearance of the valuetargetinarray.- Parameters:
- array- an array of- bytevalues, possibly empty
- target- a primitive- bytevalue
- Returns:
- the least index ifor whicharray[i] == target, or-1if no such index exists.
 
 - 
indexOfpublic static int indexOf(byte[] array, byte[] target) Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.More formally, returns the lowest index isuch thatArrays.copyOfRange(array, i, i + target.length)contains exactly the same elements astarget.- Parameters:
- array- the array to search for the sequence- target
- target- the array to search for as a sub-sequence of- array
 
 - 
lastIndexOfpublic static int lastIndexOf(byte[] array, byte target) Returns the index of the last appearance of the valuetargetinarray.- Parameters:
- array- an array of- bytevalues, possibly empty
- target- a primitive- bytevalue
- Returns:
- the greatest index ifor whicharray[i] == target, or-1if no such index exists.
 
 - 
concatpublic 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- bytearrays
- Returns:
- a single array containing all the values from the source arrays, in order
- Throws:
- java.lang.IllegalArgumentException- if the total number of elements in- arraysdoes not fit in an- int
 
 - 
ensureCapacitypublic static byte[] ensureCapacity(byte[] array, int minLength, int padding) Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length. Ifarrayalready has a length of at leastminLength, it is returned directly. Otherwise, a new array of sizeminLength + paddingis returned, containing the values ofarray, 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 lengthminLength
- Throws:
- java.lang.IllegalArgumentException- if- minLengthor- paddingis negative
 
 - 
toArraypublic static byte[] toArray(java.util.Collection<? extends java.lang.Number> collection) Returns an array containing each value ofcollection, converted to abytevalue in the manner ofNumber.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- a collection of- Numberinstances
- Returns:
- an array containing the same values as collection, in the same order, converted to primitives
- Throws:
- java.lang.NullPointerException- if- collectionor any of its elements is null
- Since:
- 1.0 (parameter was Collection<Byte>before 12.0)
 
 - 
asListpublic static java.util.List<java.lang.Byte> asList(byte... backingArray) Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]). The list supportsList.set(int, Object), but any attempt to set a value tonullwill result in aNullPointerException.The returned list maintains the values, but not the identities, of Byteobjects written to or read from it. For example, whetherlist.get(0) == list.get(0)is true for the returned list is unspecified.The returned list is serializable. - Parameters:
- backingArray- the array to back the list
- Returns:
- a list view of the array
 
 - 
reversepublic static void reverse(byte[] array) Reverses the elements ofarray. This is equivalent toCollections.reverse(Bytes.asList(array)), but is likely to be more efficient.- Since:
- 23.1
 
 - 
reversepublic static void reverse(byte[] array, int fromIndex, int toIndex) Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.reverse(Bytes.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.- Throws:
- java.lang.IndexOutOfBoundsException- if- fromIndex < 0,- toIndex > array.length, or- toIndex > fromIndex
- Since:
- 23.1
 
 - 
rotatepublic static void rotate(byte[] array, int distance) Performs a right rotation ofarrayof "distance" places, so that the first element is moved to index "distance", and the element at indexiends up at index(distance + i) mod array.length. This is equivalent toCollections.rotate(Bytes.asList(array), distance), but is somewhat faster.The provided "distance" may be negative, which will rotate left. - Since:
- 32.0.0
 
 - 
rotatepublic static void rotate(byte[] array, int distance, int fromIndex, int toIndex) Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.rotate(Bytes.asList(array).subList(fromIndex, toIndex), distance), but is somewhat faster.The provided "distance" may be negative, which will rotate left. - Throws:
- java.lang.IndexOutOfBoundsException- if- fromIndex < 0,- toIndex > array.length, or- toIndex > fromIndex
- Since:
- 32.0.0
 
 
- 
 
-