Package com.google.common.primitives
Class Bytes
- java.lang.Object
-
- com.google.common.primitives.Bytes
-
@GwtCompatible public final class Bytes extends java.lang.Object
Static utility methods pertaining tobyte
primitives, that are not already found in eitherByte
orArrays
, and interpret bytes as neither signed nor unsigned. The methods which specifically treat bytes as signed or unsigned are found inSignedBytes
andUnsignedBytes
.See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
-
-
Method Summary
All 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 boolean
contains(byte[] array, byte target)
Returnstrue
iftarget
is 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 int
hashCode(byte value)
Returns a hash code forvalue
; 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 valuetarget
inarray
.static int
indexOf(byte[] array, byte[] target)
Returns the start position of the first occurrence of the specifiedtarget
withinarray
, or-1
if there is no such occurrence.static int
lastIndexOf(byte[] array, byte target)
Returns the index of the last appearance of the valuetarget
inarray
.static byte[]
toArray(java.util.Collection<? extends java.lang.Number> collection)
Returns an array containing each value ofcollection
, converted to abyte
value in the manner ofNumber.byteValue()
.
-
-
-
Method Detail
-
hashCode
public static int hashCode(byte value)
Returns a hash code forvalue
; equal to the result of invoking((Byte) value).hashCode()
.- Parameters:
value
- a primitivebyte
value- Returns:
- a hash code for the value
-
contains
public static boolean contains(byte[] array, byte target)
Returnstrue
iftarget
is present as an element anywhere inarray
.- Parameters:
array
- an array ofbyte
values, possibly emptytarget
- a primitivebyte
value- Returns:
true
ifarray[i] == target
for some value ofi
-
indexOf
public static int indexOf(byte[] array, byte target)
Returns the index of the first appearance of the valuetarget
inarray
.- Parameters:
array
- an array ofbyte
values, possibly emptytarget
- a primitivebyte
value- Returns:
- the least index
i
for whicharray[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 specifiedtarget
withinarray
, or-1
if there is no such occurrence.More formally, returns the lowest index
i
such thatjava.util.Arrays.copyOfRange(array, i, i + target.length)
contains exactly the same elements astarget
.- Parameters:
array
- the array to search for the sequencetarget
target
- the array to search for as a sub-sequence ofarray
-
lastIndexOf
public static int lastIndexOf(byte[] array, byte target)
Returns the index of the last appearance of the valuetarget
inarray
.- Parameters:
array
- an array ofbyte
values, possibly emptytarget
- a primitivebyte
value- Returns:
- the greatest index
i
for whicharray[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 morebyte
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 asarray
, but guaranteed to be of a specified minimum length. Ifarray
already has a length of at leastminLength
, it is returned directly. Otherwise, a new array of sizeminLength + padding
is returned, containing the values ofarray
, and zeroes in the remaining places.- Parameters:
array
- the source arrayminLength
- the minimum length the returned array must guaranteepadding
- 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
- ifminLength
orpadding
is negative
-
toArray
public static byte[] toArray(java.util.Collection<? extends java.lang.Number> collection)
Returns an array containing each value ofcollection
, converted to abyte
value 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 ofNumber
instances- Returns:
- an array containing the same values as
collection
, in the same order, converted to primitives - Throws:
java.lang.NullPointerException
- ifcollection
or any of its elements is null- Since:
- 1.0 (parameter was
Collection<Byte>
before 12.0)
-
asList
public 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 tonull
will result in aNullPointerException
.The returned list maintains the values, but not the identities, of
Byte
objects written to or read from it. For example, whetherlist.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
-
-