/** * Compares the two specified {@code byte} values, treating them as unsigned values between 0 and * 255 inclusive. For example, {@code (byte) -127} is considered greater than {@code (byte) 127} * because it is seen as having the value of positive {@code 129}. * * @param a the first {@code byte} to compare * @param b the second {@code byte} to compare * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is * greater than {@code b}; or zero if they are equal */ public static int compare(byte a, byte b) { return toInt(a) - toInt(b); }
/** * Returns the {@code byte} value that, when treated as unsigned, is nearest in value to {@code * value}. * * @param value any {@code long} value * @return {@code (byte) 255} if {@code value >= 255}, {@code (byte) 0} if {@code value <= 0}, and * {@code value} cast to {@code byte} otherwise */ public static byte saturatedCast(long value) { if (value > toInt(MAX_VALUE)) { return MAX_VALUE; // -1 } if (value < 0) { return (byte) 0; } return (byte) value; }
/** * Compares the two specified {@code byte} values, treating them as unsigned values between 0 and * 255 inclusive. For example, {@code (byte) -127} is considered greater than {@code (byte) 127} * because it is seen as having the value of positive {@code 129}. * * @param a the first {@code byte} to compare * @param b the second {@code byte} to compare * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is * greater than {@code b}; or zero if they are equal */ public static int compare(byte a, byte b) { return toInt(a) - toInt(b); }
/** * Returns the least value present in {@code array}. * * @param array a <i>nonempty</i> array of {@code byte} values * @return the value present in {@code array} that is less than or equal to every other value in * the array * @throws IllegalArgumentException if {@code array} is empty */ public static byte min(byte... array) { checkArgument(array.length > 0); int min = toInt(array[0]); for (int i = 1; i < array.length; i++) { int next = toInt(array[i]); if (next < min) { min = next; } } return (byte) min; }
/** * Returns the greatest value present in {@code array}. * * @param array a <i>nonempty</i> array of {@code byte} values * @return the value present in {@code array} that is greater than or equal to every other value * in the array * @throws IllegalArgumentException if {@code array} is empty */ public static byte max(byte... array) { checkArgument(array.length > 0); int max = toInt(array[0]); for (int i = 1; i < array.length; i++) { int next = toInt(array[i]); if (next > max) { max = next; } } return (byte) max; }
static int decodeLengthUTF8(ByteBuffer buffer, int offset) { // UTF-8 strings use a clever variant of the 7-bit integer for packing the string length. // If the first byte is >= 0x80, then a second byte follows. For these values, the length // is WORD-length in big-endian & 0x7FFF. int length = UnsignedBytes.toInt(buffer.get(offset)); if ((length & 0x80) != 0) { length = ((length & 0x7F) << 8) | UnsignedBytes.toInt(buffer.get(offset + 1)); } return length; }
/** * Returns a string representation of {@code x} for the given radix, where {@code x} is treated as * unsigned. * * @param x the value to convert to a string. * @param radix the radix to use while working with {@code x} * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX} * and {@link Character#MAX_RADIX}. * @since 13.0 */ @Beta public static String toString(byte x, int radix) { checkArgument( radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX, "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX", radix); // Benchmarks indicate this is probably not worth optimizing. return Integer.toString(toInt(x), radix); }
@Override public int read() throws IOException { return (read(singleByte) == 1) ? UnsignedBytes.toInt(singleByte[0]) : -1; }
switch (bb.remaining()) { case 15: k2 ^= (long) toInt(bb.get(14)) << 48; // fall through case 14: k2 ^= (long) toInt(bb.get(13)) << 40; // fall through case 13: k2 ^= (long) toInt(bb.get(12)) << 32; // fall through case 12: k2 ^= (long) toInt(bb.get(11)) << 24; // fall through case 11: k2 ^= (long) toInt(bb.get(10)) << 16; // fall through case 10: k2 ^= (long) toInt(bb.get(9)) << 8; // fall through case 9: k2 ^= (long) toInt(bb.get(8)); // fall through case 8: k1 ^= bb.getLong(); break; case 7: k1 ^= (long) toInt(bb.get(6)) << 48; // fall through case 6: k1 ^= (long) toInt(bb.get(5)) << 40; // fall through case 5: k1 ^= (long) toInt(bb.get(4)) << 32; // fall through case 4: k1 ^= (long) toInt(bb.get(3)) << 24; // fall through case 3: k1 ^= (long) toInt(bb.get(2)) << 16; // fall through case 2:
/** * Returns a string containing the supplied {@code byte} values separated by {@code separator}. * For example, {@code join(":", (byte) 1, (byte) 2, (byte) 255)} returns the string {@code * "1:2:255"}. * * @param separator the text that should appear between consecutive values in the resulting string * (but not at the start or end) * @param array an array of {@code byte} values, possibly empty */ public static String join(String separator, byte... array) { checkNotNull(separator); if (array.length == 0) { return ""; } // For pre-sizing a builder, just get the right order of magnitude StringBuilder builder = new StringBuilder(array.length * (3 + separator.length())); builder.append(toInt(array[0])); for (int i = 1; i < array.length; i++) { builder.append(separator).append(toString(array[i])); } return builder.toString(); }
@Override public int read() throws IOException { return (read(singleByte) == 1) ? UnsignedBytes.toInt(singleByte[0]) : -1; }
@Override public int read() throws IOException { byte[] b = new byte[1]; return read(b) == -1 ? -1 : UnsignedBytes.toInt(b[0]); }
public void testToInt() { assertEquals(0, UnsignedBytes.toInt((byte) 0)); assertEquals(1, UnsignedBytes.toInt((byte) 1)); assertEquals(127, UnsignedBytes.toInt((byte) 127)); assertEquals(128, UnsignedBytes.toInt((byte) -128)); assertEquals(129, UnsignedBytes.toInt((byte) -127)); assertEquals(255, UnsignedBytes.toInt((byte) -1)); }
numHashFunctions = UnsignedBytes.toInt(din.readByte()); dataLength = din.readInt();
@Override public Integer get() { ItemComponent itemComp = getItem().getComponent(ItemComponent.class); if (itemComp != null) { return UnsignedBytes.toInt(itemComp.stackCount); } return 1; } });
@Override public Integer get() { ItemComponent itemComp = getTargetItem().getComponent(ItemComponent.class); if (itemComp != null) { return UnsignedBytes.toInt(itemComp.stackCount); } return 1; } });
@Override public HashCode hashBytes(byte[] input, int off, int len) { checkPositionIndexes(off, off + len, input.length); int h1 = seed; int i; for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE) { int k1 = mixK1(getIntLittleEndian(input, off + i)); h1 = mixH1(h1, k1); } int k1 = 0; for (int shift = 0; i < len; i++, shift += 8) { k1 ^= toInt(input[off + i]) << shift; } h1 ^= mixK1(k1); return fmix(h1, len); }
@Override public HashCode hashBytes(byte[] input, int off, int len) { checkPositionIndexes(off, off + len, input.length); int h1 = seed; int i; for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE) { int k1 = mixK1(getIntLittleEndian(input, off + i)); h1 = mixH1(h1, k1); } int k1 = 0; for (int shift = 0; i < len; i++, shift += 8) { k1 ^= toInt(input[off + i]) << shift; } h1 ^= mixK1(k1); return fmix(h1, len); }
public void testCheckedCast() { for (byte value : VALUES) { assertEquals(value, UnsignedBytes.checkedCast(UnsignedBytes.toInt(value))); } assertCastFails(256L); assertCastFails(-1L); assertCastFails(Long.MAX_VALUE); assertCastFails(Long.MIN_VALUE); }
public void testSaturatedCast() { for (byte value : VALUES) { assertEquals(value, UnsignedBytes.saturatedCast(UnsignedBytes.toInt(value))); } assertEquals(GREATEST, UnsignedBytes.saturatedCast(256L)); assertEquals(LEAST, UnsignedBytes.saturatedCast(-1L)); assertEquals(GREATEST, UnsignedBytes.saturatedCast(Long.MAX_VALUE)); assertEquals(LEAST, UnsignedBytes.saturatedCast(Long.MIN_VALUE)); }