@GwtCompatible public final class Bytes extends Object
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.
Modifier and Type | Method and Description |
---|---|
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 void |
reverse(byte[] array)
Reverses the elements of
array . |
static void |
reverse(byte[] array,
int fromIndex,
int toIndex)
Reverses the elements of
array between fromIndex inclusive and toIndex
exclusive. |
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() . |
public static int hashCode(byte value)
value
; equal to the result of invoking ((Byte)
value).hashCode()
.
Java 8 users: use Byte.hashCode(byte)
instead.
value
- a primitive byte
valuepublic static boolean contains(byte[] array, byte target)
true
if target
is present as an element anywhere in array
.array
- an array of byte
values, possibly emptytarget
- a primitive byte
valuetrue
if array[i] == target
for some value of i
public static int indexOf(byte[] array, byte target)
target
in array
.array
- an array of byte
values, possibly emptytarget
- a primitive byte
valuei
for which array[i] == target
, or -1
if no
such index exists.public static int indexOf(byte[] array, byte[] target)
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
.
array
- the array to search for the sequence target
target
- the array to search for as a sub-sequence of array
public static int lastIndexOf(byte[] array, byte target)
target
in array
.array
- an array of byte
values, possibly emptytarget
- a primitive byte
valuei
for which array[i] == target
, or -1
if no
such index exists.public static byte[] concat(byte[]... arrays)
concat(new byte[] {a, b}, new byte[] {}, new byte[] {c}
returns the array {a, b, c}
.arrays
- zero or more byte
arrayspublic static byte[] ensureCapacity(byte[] array, int minLength, int padding)
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.array
- the source arrayminLength
- the minimum length the returned array must guaranteepadding
- an extra amount to "grow" the array by if growth is necessaryarray
, with guaranteed minimum length minLength
IllegalArgumentException
- if minLength
or padding
is negativepublic static byte[] toArray(Collection<? extends Number> collection)
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.
collection
- a collection of Number
instancescollection
, in the same order, converted
to primitivesNullPointerException
- if collection
or any of its elements is nullCollection<Byte>
before 12.0)public static List<Byte> asList(byte... backingArray)
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.
backingArray
- the array to back the listpublic static void reverse(byte[] array)
array
. This is equivalent to Collections.reverse(Bytes.asList(array))
, but is likely to be more efficient.public static void reverse(byte[] array, int fromIndex, int toIndex)
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.IndexOutOfBoundsException
- if fromIndex < 0
, toIndex > array.length
, or
toIndex > fromIndex
Copyright © 2010–2017. All rights reserved.