001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.primitives;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019import static com.google.common.base.Preconditions.checkPositionIndexes;
020import static java.security.AccessController.doPrivileged;
021import static java.util.Objects.requireNonNull;
022
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.annotations.J2ktIncompatible;
025import com.google.common.annotations.VisibleForTesting;
026import com.google.errorprone.annotations.CanIgnoreReturnValue;
027import com.google.j2objc.annotations.J2ObjCIncompatible;
028import java.lang.reflect.Field;
029import java.nio.ByteOrder;
030import java.security.PrivilegedActionException;
031import java.security.PrivilegedExceptionAction;
032import java.util.Arrays;
033import java.util.Comparator;
034import java.util.Objects;
035import org.jspecify.annotations.Nullable;
036import sun.misc.Unsafe;
037
038/**
039 * Static utility methods pertaining to {@code byte} primitives that interpret values as
040 * <i>unsigned</i> (that is, any negative value {@code b} is treated as the positive value {@code
041 * 256 + b}). The corresponding methods that treat the values as signed are found in {@link
042 * SignedBytes}, and the methods for which signedness is not an issue are in {@link Bytes}.
043 *
044 * <p>See the Guava User Guide article on <a
045 * href="https://github.com/google/guava/wiki/PrimitivesExplained">primitive utilities</a>.
046 *
047 * @author Kevin Bourrillion
048 * @author Martin Buchholz
049 * @author Hiroshi Yamauchi
050 * @author Louis Wasserman
051 * @since 1.0
052 */
053@J2ktIncompatible
054@GwtIncompatible
055public final class UnsignedBytes {
056  private UnsignedBytes() {}
057
058  /**
059   * The largest power of two that can be represented as an unsigned {@code byte}.
060   *
061   * @since 10.0
062   */
063  public static final byte MAX_POWER_OF_TWO = (byte) 0x80;
064
065  /**
066   * The largest value that fits into an unsigned byte.
067   *
068   * @since 13.0
069   */
070  public static final byte MAX_VALUE = (byte) 0xFF;
071
072  private static final int UNSIGNED_MASK = 0xFF;
073
074  /**
075   * Returns the value of the given byte as an integer, when treated as unsigned. That is, returns
076   * {@code value + 256} if {@code value} is negative; {@code value} itself otherwise.
077   *
078   * <p><b>Java 8+ users:</b> use {@link Byte#toUnsignedInt(byte)} instead.
079   *
080   * @since 6.0
081   */
082  public static int toInt(byte value) {
083    return value & UNSIGNED_MASK;
084  }
085
086  /**
087   * Returns the {@code byte} value that, when treated as unsigned, is equal to {@code value}, if
088   * possible.
089   *
090   * @param value a value between 0 and 255 inclusive
091   * @return the {@code byte} value that, when treated as unsigned, equals {@code value}
092   * @throws IllegalArgumentException if {@code value} is negative or greater than 255
093   */
094  @CanIgnoreReturnValue
095  public static byte checkedCast(long value) {
096    checkArgument(value >> Byte.SIZE == 0, "out of range: %s", value);
097    return (byte) value;
098  }
099
100  /**
101   * Returns the {@code byte} value that, when treated as unsigned, is nearest in value to {@code
102   * value}.
103   *
104   * @param value any {@code long} value
105   * @return {@code (byte) 255} if {@code value >= 255}, {@code (byte) 0} if {@code value <= 0}, and
106   *     {@code value} cast to {@code byte} otherwise
107   */
108  public static byte saturatedCast(long value) {
109    if (value > toInt(MAX_VALUE)) {
110      return MAX_VALUE; // -1
111    }
112    if (value < 0) {
113      return (byte) 0;
114    }
115    return (byte) value;
116  }
117
118  /**
119   * Compares the two specified {@code byte} values, treating them as unsigned values between 0 and
120   * 255 inclusive. For example, {@code (byte) -127} is considered greater than {@code (byte) 127}
121   * because it is seen as having the value of positive {@code 129}.
122   *
123   * @param a the first {@code byte} to compare
124   * @param b the second {@code byte} to compare
125   * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
126   *     greater than {@code b}; or zero if they are equal
127   */
128  public static int compare(byte a, byte b) {
129    return toInt(a) - toInt(b);
130  }
131
132  /**
133   * Returns the least value present in {@code array}, treating values as unsigned.
134   *
135   * @param array a <i>nonempty</i> array of {@code byte} values
136   * @return the value present in {@code array} that is less than or equal to every other value in
137   *     the array according to {@link #compare}
138   * @throws IllegalArgumentException if {@code array} is empty
139   */
140  public static byte min(byte... array) {
141    checkArgument(array.length > 0);
142    int min = toInt(array[0]);
143    for (int i = 1; i < array.length; i++) {
144      int next = toInt(array[i]);
145      if (next < min) {
146        min = next;
147      }
148    }
149    return (byte) min;
150  }
151
152  /**
153   * Returns the greatest value present in {@code array}, treating values as unsigned.
154   *
155   * @param array a <i>nonempty</i> array of {@code byte} values
156   * @return the value present in {@code array} that is greater than or equal to every other value
157   *     in the array according to {@link #compare}
158   * @throws IllegalArgumentException if {@code array} is empty
159   */
160  public static byte max(byte... array) {
161    checkArgument(array.length > 0);
162    int max = toInt(array[0]);
163    for (int i = 1; i < array.length; i++) {
164      int next = toInt(array[i]);
165      if (next > max) {
166        max = next;
167      }
168    }
169    return (byte) max;
170  }
171
172  /**
173   * Returns a string representation of x, where x is treated as unsigned.
174   *
175   * @since 13.0
176   */
177  public static String toString(byte x) {
178    return toString(x, 10);
179  }
180
181  /**
182   * Returns a string representation of {@code x} for the given radix, where {@code x} is treated as
183   * unsigned.
184   *
185   * @param x the value to convert to a string.
186   * @param radix the radix to use while working with {@code x}
187   * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX}
188   *     and {@link Character#MAX_RADIX}.
189   * @since 13.0
190   */
191  public static String toString(byte x, int radix) {
192    checkArgument(
193        radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX,
194        "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX",
195        radix);
196    // Benchmarks indicate this is probably not worth optimizing.
197    return Integer.toString(toInt(x), radix);
198  }
199
200  /**
201   * Returns the unsigned {@code byte} value represented by the given decimal string.
202   *
203   * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte}
204   *     value
205   * @throws NullPointerException if {@code string} is null (in contrast to {@link
206   *     Byte#parseByte(String)})
207   * @since 13.0
208   */
209  @CanIgnoreReturnValue
210  public static byte parseUnsignedByte(String string) {
211    return parseUnsignedByte(string, 10);
212  }
213
214  /**
215   * Returns the unsigned {@code byte} value represented by a string with the given radix.
216   *
217   * @param string the string containing the unsigned {@code byte} representation to be parsed.
218   * @param radix the radix to use while parsing {@code string}
219   * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte} with
220   *     the given radix, or if {@code radix} is not between {@link Character#MIN_RADIX} and {@link
221   *     Character#MAX_RADIX}.
222   * @throws NullPointerException if {@code string} is null (in contrast to {@link
223   *     Byte#parseByte(String)})
224   * @since 13.0
225   */
226  @CanIgnoreReturnValue
227  public static byte parseUnsignedByte(String string, int radix) {
228    int parse = Integer.parseInt(checkNotNull(string), radix);
229    // We need to throw a NumberFormatException, so we have to duplicate checkedCast. =(
230    if (parse >> Byte.SIZE == 0) {
231      return (byte) parse;
232    } else {
233      throw new NumberFormatException("out of range: " + parse);
234    }
235  }
236
237  /**
238   * Returns a string containing the supplied {@code byte} values separated by {@code separator}.
239   * For example, {@code join(":", (byte) 1, (byte) 2, (byte) 255)} returns the string {@code
240   * "1:2:255"}.
241   *
242   * @param separator the text that should appear between consecutive values in the resulting string
243   *     (but not at the start or end)
244   * @param array an array of {@code byte} values, possibly empty
245   */
246  public static String join(String separator, byte... array) {
247    checkNotNull(separator);
248    if (array.length == 0) {
249      return "";
250    }
251
252    // For pre-sizing a builder, just get the right order of magnitude
253    StringBuilder builder = new StringBuilder(array.length * (3 + separator.length()));
254    builder.append(toInt(array[0]));
255    for (int i = 1; i < array.length; i++) {
256      builder.append(separator).append(toString(array[i]));
257    }
258    return builder.toString();
259  }
260
261  /**
262   * Returns a comparator that compares two {@code byte} arrays <a
263   * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it
264   * compares, using {@link #compare(byte, byte)}), the first pair of values that follow any common
265   * prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For
266   * example, {@code [] < [0x01] < [0x01, 0x7F] < [0x01, 0x80] < [0x02]}. Values are treated as
267   * unsigned.
268   *
269   * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays
270   * support only identity equality), but it is consistent with {@link
271   * java.util.Arrays#equals(byte[], byte[])}.
272   *
273   * <p><b>Java 9+ users:</b> Use {@link Arrays#compareUnsigned(byte[], byte[])
274   * Arrays::compareUnsigned}.
275   *
276   * @since 2.0
277   */
278  public static Comparator<byte[]> lexicographicalComparator() {
279    return LexicographicalComparatorHolder.BEST_COMPARATOR;
280  }
281
282  @VisibleForTesting
283  static Comparator<byte[]> lexicographicalComparatorJavaImpl() {
284    return LexicographicalComparatorHolder.PureJavaComparator.INSTANCE;
285  }
286
287  /**
288   * Provides a lexicographical comparator implementation; either a Java implementation or a faster
289   * implementation based on {@link Unsafe}.
290   *
291   * <p>Uses reflection to gracefully fall back to the Java implementation if {@code Unsafe} isn't
292   * available.
293   */
294  @VisibleForTesting
295  static class LexicographicalComparatorHolder {
296    static final String UNSAFE_COMPARATOR_NAME =
297        LexicographicalComparatorHolder.class.getName() + "$UnsafeComparator";
298
299    static final Comparator<byte[]> BEST_COMPARATOR = getBestComparator();
300
301    @SuppressWarnings("SunApi") // b/345822163
302    @VisibleForTesting
303    enum UnsafeComparator implements Comparator<byte[]> {
304      INSTANCE;
305
306      static final boolean BIG_ENDIAN = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN);
307
308      /*
309       * The following static final fields exist for performance reasons.
310       *
311       * In UnsignedBytesBenchmark, accessing the following objects via static final fields is the
312       * fastest (more than twice as fast as the Java implementation, vs ~1.5x with non-final static
313       * fields, on x86_32) under the Hotspot server compiler. The reason is obviously that the
314       * non-final fields need to be reloaded inside the loop.
315       *
316       * And, no, defining (final or not) local variables out of the loop still isn't as good
317       * because the null check on the theUnsafe object remains inside the loop and
318       * BYTE_ARRAY_BASE_OFFSET doesn't get constant-folded.
319       *
320       * The compiler can treat static final fields as compile-time constants and can constant-fold
321       * them while (final or not) local variables are run time values.
322       */
323
324      static final Unsafe theUnsafe = getUnsafe();
325
326      /** The offset to the first element in a byte array. */
327      static final int BYTE_ARRAY_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
328
329      static {
330        // fall back to the safer pure java implementation unless we're in
331        // a 64-bit JVM with an 8-byte aligned field offset.
332        if (!(Objects.equals(System.getProperty("sun.arch.data.model"), "64")
333            && (BYTE_ARRAY_BASE_OFFSET % 8) == 0
334            // sanity check - this should never fail
335            && theUnsafe.arrayIndexScale(byte[].class) == 1)) {
336          throw new Error(); // force fallback to PureJavaComparator
337        }
338      }
339
340      /**
341       * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple
342       * call to Unsafe.getUnsafe when integrating into a jdk.
343       *
344       * @return a sun.misc.Unsafe
345       */
346      private static Unsafe getUnsafe() {
347        try {
348          return Unsafe.getUnsafe();
349        } catch (SecurityException e) {
350          // that's okay; try reflection instead
351        }
352        try {
353          return doPrivileged(
354              (PrivilegedExceptionAction<Unsafe>)
355                  () -> {
356                    Class<Unsafe> k = Unsafe.class;
357                    for (Field f : k.getDeclaredFields()) {
358                      f.setAccessible(true);
359                      Object x = f.get(null);
360                      if (k.isInstance(x)) {
361                        return k.cast(x);
362                      }
363                    }
364                    throw new NoSuchFieldError("the Unsafe");
365                  });
366        } catch (PrivilegedActionException e) {
367          throw new RuntimeException("Could not initialize intrinsics", e.getCause());
368        }
369      }
370
371      @Override
372      // Long.compareUnsigned is available under Android, which is what we really care about.
373      @SuppressWarnings("Java7ApiChecker")
374      public int compare(byte[] left, byte[] right) {
375        int stride = 8;
376        int minLength = Math.min(left.length, right.length);
377        int strideLimit = minLength & ~(stride - 1);
378        int i;
379
380        /*
381         * Compare 8 bytes at a time. Benchmarking on x86 shows a stride of 8 bytes is no slower
382         * than 4 bytes even on 32-bit. On the other hand, it is substantially faster on 64-bit.
383         */
384        for (i = 0; i < strideLimit; i += stride) {
385          long lw = theUnsafe.getLong(left, BYTE_ARRAY_BASE_OFFSET + (long) i);
386          long rw = theUnsafe.getLong(right, BYTE_ARRAY_BASE_OFFSET + (long) i);
387          if (lw != rw) {
388            if (BIG_ENDIAN) {
389              return Long.compareUnsigned(lw, rw);
390            }
391
392            /*
393             * We want to compare only the first index where left[index] != right[index]. This
394             * corresponds to the least significant nonzero byte in lw ^ rw, since lw and rw are
395             * little-endian. Long.numberOfTrailingZeros(diff) tells us the least significant
396             * nonzero bit, and zeroing out the first three bits of L.nTZ gives us the shift to get
397             * that least significant nonzero byte.
398             */
399            int n = Long.numberOfTrailingZeros(lw ^ rw) & ~0x7;
400            return ((int) ((lw >>> n) & UNSIGNED_MASK)) - ((int) ((rw >>> n) & UNSIGNED_MASK));
401          }
402        }
403
404        // The epilogue to cover the last (minLength % stride) elements.
405        for (; i < minLength; i++) {
406          int result = UnsignedBytes.compare(left[i], right[i]);
407          if (result != 0) {
408            return result;
409          }
410        }
411        return left.length - right.length;
412      }
413
414      @Override
415      public String toString() {
416        return "UnsignedBytes.lexicographicalComparator() (sun.misc.Unsafe version)";
417      }
418    }
419
420    enum PureJavaComparator implements Comparator<byte[]> {
421      INSTANCE;
422
423      @Override
424      public int compare(byte[] left, byte[] right) {
425        int minLength = Math.min(left.length, right.length);
426        for (int i = 0; i < minLength; i++) {
427          int result = UnsignedBytes.compare(left[i], right[i]);
428          if (result != 0) {
429            return result;
430          }
431        }
432        return left.length - right.length;
433      }
434
435      @Override
436      public String toString() {
437        return "UnsignedBytes.lexicographicalComparator() (pure Java version)";
438      }
439    }
440
441    /**
442     * Returns the Unsafe-using Comparator, or falls back to the pure-Java implementation if unable
443     * to do so.
444     */
445    static Comparator<byte[]> getBestComparator() {
446      Comparator<byte[]> arraysCompareUnsignedComparator =
447          ArraysCompareUnsignedComparatorMaker.INSTANCE.tryMakeArraysCompareUnsignedComparator();
448      if (arraysCompareUnsignedComparator != null) {
449        return arraysCompareUnsignedComparator;
450      }
451
452      try {
453        Class<?> theClass = Class.forName(UNSAFE_COMPARATOR_NAME);
454
455        // requireNonNull is safe because the class is an enum.
456        Object[] constants = requireNonNull(theClass.getEnumConstants());
457
458        // yes, UnsafeComparator does implement Comparator<byte[]>
459        @SuppressWarnings("unchecked")
460        Comparator<byte[]> comparator = (Comparator<byte[]>) constants[0];
461        return comparator;
462      } catch (Throwable t) { // ensure we really catch *everything*
463        return lexicographicalComparatorJavaImpl();
464      }
465    }
466  }
467
468  private enum ArraysCompareUnsignedComparatorMaker {
469    INSTANCE {
470      /** Implementation used by non-J2ObjC environments. */
471      // We use Arrays.compareUnsigned only after confirming that it's available at runtime.
472      @SuppressWarnings("Java8ApiChecker")
473      @IgnoreJRERequirement
474      @Override
475      @J2ObjCIncompatible
476      @Nullable Comparator<byte[]> tryMakeArraysCompareUnsignedComparator() {
477        try {
478          // Compare AbstractFuture.VarHandleAtomicHelperMaker.
479          Arrays.class.getMethod("compareUnsigned", byte[].class, byte[].class);
480        } catch (NoSuchMethodException beforeJava9) {
481          return null;
482        }
483        return ArraysCompareUnsignedComparator.INSTANCE;
484      }
485    };
486
487    /** Implementation used by J2ObjC environments, overridden for other environments. */
488    @Nullable Comparator<byte[]> tryMakeArraysCompareUnsignedComparator() {
489      return null;
490    }
491  }
492
493  @J2ObjCIncompatible
494  enum ArraysCompareUnsignedComparator implements Comparator<byte[]> {
495    INSTANCE;
496
497    @Override
498    // We use the class only after confirming that Arrays.compareUnsigned is available at runtime.
499    @SuppressWarnings("Java8ApiChecker")
500    @IgnoreJRERequirement
501    public int compare(byte[] left, byte[] right) {
502      return Arrays.compareUnsigned(left, right);
503    }
504  }
505
506  private static byte flip(byte b) {
507    return (byte) (b ^ 0x80);
508  }
509
510  /**
511   * Sorts the array, treating its elements as unsigned bytes.
512   *
513   * @since 23.1
514   */
515  public static void sort(byte[] array) {
516    checkNotNull(array);
517    sort(array, 0, array.length);
518  }
519
520  /**
521   * Sorts the array between {@code fromIndex} inclusive and {@code toIndex} exclusive, treating its
522   * elements as unsigned bytes.
523   *
524   * @since 23.1
525   */
526  public static void sort(byte[] array, int fromIndex, int toIndex) {
527    checkNotNull(array);
528    checkPositionIndexes(fromIndex, toIndex, array.length);
529    for (int i = fromIndex; i < toIndex; i++) {
530      array[i] = flip(array[i]);
531    }
532    Arrays.sort(array, fromIndex, toIndex);
533    for (int i = fromIndex; i < toIndex; i++) {
534      array[i] = flip(array[i]);
535    }
536  }
537
538  /**
539   * Sorts the elements of {@code array} in descending order, interpreting them as unsigned 8-bit
540   * integers.
541   *
542   * @since 23.1
543   */
544  public static void sortDescending(byte[] array) {
545    checkNotNull(array);
546    sortDescending(array, 0, array.length);
547  }
548
549  /**
550   * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex}
551   * exclusive in descending order, interpreting them as unsigned 8-bit integers.
552   *
553   * @since 23.1
554   */
555  public static void sortDescending(byte[] array, int fromIndex, int toIndex) {
556    checkNotNull(array);
557    checkPositionIndexes(fromIndex, toIndex, array.length);
558    for (int i = fromIndex; i < toIndex; i++) {
559      array[i] ^= Byte.MAX_VALUE;
560    }
561    Arrays.sort(array, fromIndex, toIndex);
562    for (int i = fromIndex; i < toIndex; i++) {
563      array[i] ^= Byte.MAX_VALUE;
564    }
565  }
566}