public final class UnsignedBytes
extends java.lang.Object
byte primitives that interpret values as
unsigned (that is, any negative value b is treated as the positive value 256 + b). The corresponding methods that treat the values as signed are found in SignedBytes, and the methods for which signedness is not an issue are in Bytes.
See the Guava User Guide article on primitive utilities.
| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
UnsignedBytes.LexicographicalComparatorHolder
Provides a lexicographical comparator implementation; either a Java implementation or a faster
implementation based on
Unsafe. |
| Modifier and Type | Field and Description |
|---|---|
static byte |
MAX_POWER_OF_TWO
The largest power of two that can be represented as an unsigned
byte. |
static byte |
MAX_VALUE
The largest value that fits into an unsigned byte.
|
private static int |
UNSIGNED_MASK |
| Modifier | Constructor and Description |
|---|---|
private |
UnsignedBytes() |
| Modifier and Type | Method and Description |
|---|---|
static byte |
checkedCast(long value)
Returns the
byte value that, when treated as unsigned, is equal to value, if
possible. |
static int |
compare(byte a,
byte b)
Compares the two specified
byte values, treating them as unsigned values between 0 and
255 inclusive. |
private static byte |
flip(byte b) |
static java.lang.String |
join(java.lang.String separator,
byte... array)
Returns a string containing the supplied
byte values separated by separator. |
static java.util.Comparator<byte[]> |
lexicographicalComparator()
Returns a comparator that compares two
byte arrays lexicographically. |
(package private) static java.util.Comparator<byte[]> |
lexicographicalComparatorJavaImpl() |
static byte |
max(byte... array)
Returns the greatest value present in
array, treating values as unsigned. |
static byte |
min(byte... array)
Returns the least value present in
array, treating values as unsigned. |
static byte |
parseUnsignedByte(java.lang.String string)
Returns the unsigned
byte value represented by the given decimal string. |
static byte |
parseUnsignedByte(java.lang.String string,
int radix)
Returns the unsigned
byte value represented by a string with the given radix. |
static byte |
saturatedCast(long value)
Returns the
byte value that, when treated as unsigned, is nearest in value to value. |
static void |
sort(byte[] array)
Sorts the array, treating its elements as unsigned bytes.
|
static void |
sort(byte[] array,
int fromIndex,
int toIndex)
Sorts the array between
fromIndex inclusive and toIndex exclusive, treating its
elements as unsigned bytes. |
static void |
sortDescending(byte[] array)
Sorts the elements of
array in descending order, interpreting them as unsigned 8-bit
integers. |
static void |
sortDescending(byte[] array,
int fromIndex,
int toIndex)
Sorts the elements of
array between fromIndex inclusive and toIndex
exclusive in descending order, interpreting them as unsigned 8-bit integers. |
static int |
toInt(byte value)
Returns the value of the given byte as an integer, when treated as unsigned.
|
static java.lang.String |
toString(byte x)
Returns a string representation of x, where x is treated as unsigned.
|
static java.lang.String |
toString(byte x,
int radix)
Returns a string representation of
x for the given radix, where x is treated as
unsigned. |
public static final byte MAX_POWER_OF_TWO
byte.public static final byte MAX_VALUE
private static final int UNSIGNED_MASK
public static int toInt(byte value)
value + 256 if value is negative; value itself otherwise.
Java 8 users: use Byte.toUnsignedInt(byte) instead.
public static byte checkedCast(long value)
byte value that, when treated as unsigned, is equal to value, if
possible.value - a value between 0 and 255 inclusivebyte value that, when treated as unsigned, equals valuejava.lang.IllegalArgumentException - if value is negative or greater than 255public static byte saturatedCast(long value)
byte value that, when treated as unsigned, is nearest in value to value.value - any long value(byte) 255 if value >= 255, (byte) 0 if value <= 0, and
value cast to byte otherwisepublic static int compare(byte a,
byte b)
byte values, treating them as unsigned values between 0 and
255 inclusive. For example, (byte) -127 is considered greater than (byte) 127
because it is seen as having the value of positive 129.a - the first byte to compareb - the second byte to comparea is less than b; a positive value if a is
greater than b; or zero if they are equalpublic static byte min(byte... array)
array, treating values as unsigned.array - a nonempty array of byte valuesarray that is less than or equal to every other value in
the array according to compare(byte, byte)java.lang.IllegalArgumentException - if array is emptypublic static byte max(byte... array)
array, treating values as unsigned.array - a nonempty array of byte valuesarray that is greater than or equal to every other value
in the array according to compare(byte, byte)java.lang.IllegalArgumentException - if array is emptypublic static java.lang.String toString(byte x)
public static java.lang.String toString(byte x,
int radix)
x for the given radix, where x is treated as
unsigned.x - the value to convert to a string.radix - the radix to use while working with xjava.lang.IllegalArgumentException - if radix is not between Character.MIN_RADIX
and Character.MAX_RADIX.public static byte parseUnsignedByte(java.lang.String string)
byte value represented by the given decimal string.java.lang.NumberFormatException - if the string does not contain a valid unsigned byte
valuejava.lang.NullPointerException - if string is null (in contrast to Byte.parseByte(String))public static byte parseUnsignedByte(java.lang.String string,
int radix)
byte value represented by a string with the given radix.string - the string containing the unsigned byte representation to be parsed.radix - the radix to use while parsing stringjava.lang.NumberFormatException - if the string does not contain a valid unsigned byte with
the given radix, or if radix is not between Character.MIN_RADIX and Character.MAX_RADIX.java.lang.NullPointerException - if string is null (in contrast to Byte.parseByte(String))public static java.lang.String join(java.lang.String separator,
byte... array)
byte values separated by separator.
For example, join(":", (byte) 1, (byte) 2, (byte) 255) returns the string "1:2:255".separator - the text that should appear between consecutive values in the resulting string
(but not at the start or end)array - an array of byte values, possibly emptypublic static java.util.Comparator<byte[]> lexicographicalComparator()
byte arrays lexicographically. That is, it
compares, using compare(byte, byte)), the first pair of values that follow any common
prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For
example, [] < [0x01] < [0x01, 0x7F] < [0x01, 0x80] < [0x02]. Values are treated as
unsigned.
The returned comparator is inconsistent with Object.equals(Object) (since arrays
support only identity equality), but it is consistent with Arrays.equals(byte[], byte[]).
static java.util.Comparator<byte[]> lexicographicalComparatorJavaImpl()
private static byte flip(byte b)
public static void sort(byte[] array)
public static void sort(byte[] array,
int fromIndex,
int toIndex)
fromIndex inclusive and toIndex exclusive, treating its
elements as unsigned bytes.public static void sortDescending(byte[] array)
array in descending order, interpreting them as unsigned 8-bit
integers.public static void sortDescending(byte[] array,
int fromIndex,
int toIndex)
array between fromIndex inclusive and toIndex
exclusive in descending order, interpreting them as unsigned 8-bit integers.