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.lang.Byte.toUnsignedInt;
021import static java.lang.Integer.parseInt;
022import static java.util.Objects.requireNonNull;
023
024import com.google.common.annotations.GwtIncompatible;
025import com.google.common.annotations.J2ktIncompatible;
026import com.google.common.annotations.VisibleForTesting;
027import com.google.errorprone.annotations.CanIgnoreReturnValue;
028import com.google.errorprone.annotations.InlineMe;
029import com.google.j2objc.annotations.J2ObjCIncompatible;
030import java.lang.reflect.Field;
031import java.nio.ByteOrder;
032import java.security.PrivilegedExceptionAction;
033import java.util.Arrays;
034import java.util.Comparator;
035import java.util.Objects;
036import org.jspecify.annotations.Nullable;
037import sun.misc.Unsafe;
038
039/**
040 * Static utility methods pertaining to {@code byte} primitives that interpret values as
041 * <i>unsigned</i> (that is, any negative value {@code b} is treated as the positive value {@code
042 * 256 + b}). The corresponding methods that treat the values as signed are found in {@link
043 * SignedBytes}, and the methods for which signedness is not an issue are in {@link Bytes}.
044 *
045 * <p>See the Guava User Guide article on <a
046 * href="https://github.com/google/guava/wiki/PrimitivesExplained">primitive utilities</a>.
047 *
048 * @author Kevin Bourrillion
049 * @author Martin Buchholz
050 * @author Hiroshi Yamauchi
051 * @author Louis Wasserman
052 * @since 1.0
053 */
054@J2ktIncompatible
055@GwtIncompatible
056public final class UnsignedBytes {
057  private UnsignedBytes() {}
058
059  /**
060   * The largest power of two that can be represented as an unsigned {@code byte}.
061   *
062   * @since 10.0
063   */
064  public static final byte MAX_POWER_OF_TWO = (byte) 0x80;
065
066  /**
067   * The largest value that fits into an unsigned byte.
068   *
069   * @since 13.0
070   */
071  public static final byte MAX_VALUE = (byte) 0xFF;
072
073  private static final int UNSIGNED_MASK = 0xFF;
074
075  /**
076   * Returns the value of the given byte as an integer, when treated as unsigned. That is, returns
077   * {@code value + 256} if {@code value} is negative; {@code value} itself otherwise.
078   *
079   * <p>Prefer {@link Byte#toUnsignedInt(byte)} instead.
080   *
081   * @since 6.0
082   */
083  @InlineMe(replacement = "Byte.toUnsignedInt(value)")
084  public static int toInt(byte value) {
085    return Byte.toUnsignedInt(value);
086  }
087
088  /**
089   * Returns the {@code byte} value that, when treated as unsigned, is equal to {@code value}, if
090   * possible.
091   *
092   * @param value a value between 0 and 255 inclusive
093   * @return the {@code byte} value that, when treated as unsigned, equals {@code value}
094   * @throws IllegalArgumentException if {@code value} is negative or greater than 255
095   */
096  @CanIgnoreReturnValue
097  public static byte checkedCast(long value) {
098    checkArgument(value >> Byte.SIZE == 0, "out of range: %s", value);
099    return (byte) value;
100  }
101
102  /**
103   * Returns the {@code byte} value that, when treated as unsigned, is nearest in value to {@code
104   * value}.
105   *
106   * @param value any {@code long} value
107   * @return {@code (byte) 255} if {@code value >= 255}, {@code (byte) 0} if {@code value <= 0}, and
108   *     {@code value} cast to {@code byte} otherwise
109   */
110  public static byte saturatedCast(long value) {
111    if (value > toUnsignedInt(MAX_VALUE)) {
112      return MAX_VALUE; // -1
113    }
114    if (value < 0) {
115      return (byte) 0;
116    }
117    return (byte) value;
118  }
119
120  /**
121   * Compares the two specified {@code byte} values, treating them as unsigned values between 0 and
122   * 255 inclusive. For example, {@code (byte) -127} is considered greater than {@code (byte) 127}
123   * because it is seen as having the value of positive {@code 129}.
124   *
125   * @param a the first {@code byte} to compare
126   * @param b the second {@code byte} to compare
127   * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
128   *     greater than {@code b}; or zero if they are equal
129   */
130  public static int compare(byte a, byte b) {
131    return toUnsignedInt(a) - toUnsignedInt(b);
132  }
133
134  /**
135   * Returns the least value present in {@code array}, treating values as unsigned.
136   *
137   * @param array a <i>nonempty</i> array of {@code byte} values
138   * @return the value present in {@code array} that is less than or equal to every other value in
139   *     the array according to {@link #compare}
140   * @throws IllegalArgumentException if {@code array} is empty
141   */
142  public static byte min(byte... array) {
143    checkArgument(array.length > 0);
144    int min = toUnsignedInt(array[0]);
145    for (int i = 1; i < array.length; i++) {
146      int next = toUnsignedInt(array[i]);
147      if (next < min) {
148        min = next;
149      }
150    }
151    return (byte) min;
152  }
153
154  /**
155   * Returns the greatest value present in {@code array}, treating values as unsigned.
156   *
157   * @param array a <i>nonempty</i> array of {@code byte} values
158   * @return the value present in {@code array} that is greater than or equal to every other value
159   *     in the array according to {@link #compare}
160   * @throws IllegalArgumentException if {@code array} is empty
161   */
162  public static byte max(byte... array) {
163    checkArgument(array.length > 0);
164    int max = toUnsignedInt(array[0]);
165    for (int i = 1; i < array.length; i++) {
166      int next = toUnsignedInt(array[i]);
167      if (next > max) {
168        max = next;
169      }
170    }
171    return (byte) max;
172  }
173
174  /**
175   * Returns a string representation of x, where x is treated as unsigned.
176   *
177   * @since 13.0
178   */
179  public static String toString(byte x) {
180    return toString(x, 10);
181  }
182
183  /**
184   * Returns a string representation of {@code x} for the given radix, where {@code x} is treated as
185   * unsigned.
186   *
187   * @param x the value to convert to a string.
188   * @param radix the radix to use while working with {@code x}
189   * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX}
190   *     and {@link Character#MAX_RADIX}.
191   * @since 13.0
192   */
193  public static String toString(byte x, int radix) {
194    checkArgument(
195        radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX,
196        "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX",
197        radix);
198    // Benchmarks indicate this is probably not worth optimizing.
199    return Integer.toString(toUnsignedInt(x), radix);
200  }
201
202  /**
203   * Returns the unsigned {@code byte} value represented by the given decimal string.
204   *
205   * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte}
206   *     value
207   * @throws NullPointerException if {@code string} is null (in contrast to {@link
208   *     Byte#parseByte(String)})
209   * @since 13.0
210   */
211  @CanIgnoreReturnValue
212  public static byte parseUnsignedByte(String string) {
213    return parseUnsignedByte(string, 10);
214  }
215
216  /**
217   * Returns the unsigned {@code byte} value represented by a string with the given radix.
218   *
219   * @param string the string containing the unsigned {@code byte} representation to be parsed.
220   * @param radix the radix to use while parsing {@code string}
221   * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte} with
222   *     the given radix, or if {@code radix} is not between {@link Character#MIN_RADIX} and {@link
223   *     Character#MAX_RADIX}.
224   * @throws NullPointerException if {@code string} is null (in contrast to {@link
225   *     Byte#parseByte(String)})
226   * @since 13.0
227   */
228  @CanIgnoreReturnValue
229  public static byte parseUnsignedByte(String string, int radix) {
230    int parse = parseInt(checkNotNull(string), radix);
231    // We need to throw a NumberFormatException, so we have to duplicate checkedCast. =(
232    if (parse >> Byte.SIZE == 0) {
233      return (byte) parse;
234    } else {
235      throw new NumberFormatException("out of range: " + parse);
236    }
237  }
238
239  /**
240   * Returns a string containing the supplied {@code byte} values separated by {@code separator}.
241   * For example, {@code join(":", (byte) 1, (byte) 2, (byte) 255)} returns the string {@code
242   * "1:2:255"}.
243   *
244   * @param separator the text that should appear between consecutive values in the resulting string
245   *     (but not at the start or end)
246   * @param array an array of {@code byte} values, possibly empty
247   */
248  public static String join(String separator, byte... array) {
249    checkNotNull(separator);
250    if (array.length == 0) {
251      return "";
252    }
253
254    // For pre-sizing a builder, just get the right order of magnitude
255    StringBuilder builder = new StringBuilder(array.length * (3 + separator.length()));
256    builder.append(toUnsignedInt(array[0]));
257    for (int i = 1; i < array.length; i++) {
258      builder.append(separator).append(toString(array[i]));
259    }
260    return builder.toString();
261  }
262
263  /**
264   * Returns a comparator that compares two {@code byte} arrays <a
265   * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it
266   * compares, using {@link #compare(byte, byte)}), the first pair of values that follow any common
267   * prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For
268   * example, {@code [] < [0x01] < [0x01, 0x7F] < [0x01, 0x80] < [0x02]}. Values are treated as
269   * unsigned.
270   *
271   * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays
272   * support only identity equality), but it is consistent with {@link
273   * java.util.Arrays#equals(byte[], byte[])}.
274   *
275   * <p><b>Java 9+ users:</b> Use {@link Arrays#compareUnsigned(byte[], byte[])
276   * Arrays::compareUnsigned}.
277   *
278   * @since 2.0
279   */
280  public static Comparator<byte[]> lexicographicalComparator() {
281    return LexicographicalComparatorHolder.BEST_COMPARATOR;
282  }
283
284  @VisibleForTesting
285  static Comparator<byte[]> lexicographicalComparatorJavaImpl() {
286    return LexicographicalComparatorHolder.PureJavaComparator.INSTANCE;
287  }
288
289  /** Provides a lexicographical comparator implementation selected based on the current runtime. */
290  @VisibleForTesting
291  static final class LexicographicalComparatorHolder {
292    /**
293     * Interface implemented by {@link UnsafeComparator}, extracted so that we can refer to it
294     * without referring to {@link UnsafeComparator} itself.
295     */
296    interface LexicographicalComparator extends Comparator<byte[]> {
297      /** Returns whether this implementation is available for use on the current platform. */
298      boolean isFunctional();
299    }
300
301    static final String UNSAFE_COMPARATOR_NAME =
302        LexicographicalComparatorHolder.class.getName() + "$UnsafeComparator";
303
304    static final Comparator<byte[]> BEST_COMPARATOR = getBestComparator();
305
306    @SuppressWarnings({
307      // b/345822163
308      "SunApi",
309      "deprecation",
310      "removal",
311      // The `deprecation` suppression is for Unsafe APIs that aren't deprecated under some versions
312      "UnnecessaryJavacSuppressWarnings"
313    })
314    @VisibleForTesting
315    enum UnsafeComparator implements LexicographicalComparator {
316      INSTANCE;
317
318      static final boolean BIG_ENDIAN = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN);
319
320      /*
321       * The following static final fields exist for performance reasons.
322       *
323       * In UnsignedBytesBenchmark, accessing the following objects via static final fields is the
324       * fastest (more than twice as fast as the Java implementation, vs ~1.5x with non-final static
325       * fields, on x86_32) under the Hotspot server compiler. The reason is obviously that the
326       * non-final fields need to be reloaded inside the loop.
327       *
328       * And, no, defining (final or not) local variables out of the loop still isn't as good
329       * because the null check on the theUnsafe object remains inside the loop and
330       * BYTE_ARRAY_BASE_OFFSET doesn't get constant-folded.
331       *
332       * The compiler can treat static final fields as compile-time constants and can constant-fold
333       * them while (final or not) local variables are run time values.
334       */
335
336      /**
337       * Value stored in {@link #BYTE_ARRAY_BASE_OFFSET} to indicate that the current runtime does
338       * not support the {@link Unsafe} comparator.
339       */
340      static final int OFFSET_UNSAFE_APPROACH_IS_UNAVAILABLE = -1;
341
342      static final @Nullable Unsafe theUnsafe = getUnsafe();
343
344      /**
345       * The offset to the first element in a byte array, or {@link
346       * #OFFSET_UNSAFE_APPROACH_IS_UNAVAILABLE}.
347       */
348      static final int BYTE_ARRAY_BASE_OFFSET = getByteArrayBaseOffset();
349
350      private static int getByteArrayBaseOffset() {
351        if (theUnsafe == null) {
352          return OFFSET_UNSAFE_APPROACH_IS_UNAVAILABLE;
353        }
354
355        try {
356          int offset = theUnsafe.arrayBaseOffset(byte[].class);
357          int scale = theUnsafe.arrayIndexScale(byte[].class);
358
359          // Use Unsafe only if we're in a 64-bit JVM with an 8-byte aligned field offset.
360          if (Objects.equals(System.getProperty("sun.arch.data.model"), "64")
361              && (offset % 8) == 0
362              // sanity check - this should never fail
363              && scale == 1) {
364            return offset;
365          }
366
367          return OFFSET_UNSAFE_APPROACH_IS_UNAVAILABLE;
368        } catch (UnsupportedOperationException e) {
369          return OFFSET_UNSAFE_APPROACH_IS_UNAVAILABLE;
370        }
371      }
372
373      private static @Nullable Unsafe getUnsafe() {
374        try {
375          return Unsafe.getUnsafe();
376        } catch (SecurityException e) {
377          // that's okay; try reflection instead
378        }
379        try {
380          PrivilegedExceptionAction<Unsafe> action =
381              () -> {
382                Class<Unsafe> k = Unsafe.class;
383                for (Field f : k.getDeclaredFields()) {
384                  f.setAccessible(true);
385                  Object x = f.get(null);
386                  if (k.isInstance(x)) {
387                    return k.cast(x);
388                  }
389                }
390                return null;
391              };
392          try {
393            return (Unsafe)
394                Class.forName("java.security.AccessController")
395                    .getMethod("doPrivileged", PrivilegedExceptionAction.class)
396                    .invoke(null, action);
397          } catch (Exception e) {
398            return action.run();
399          }
400        } catch (Exception e) {
401          return null;
402        }
403      }
404
405      @Override
406      public boolean isFunctional() {
407        return BYTE_ARRAY_BASE_OFFSET != OFFSET_UNSAFE_APPROACH_IS_UNAVAILABLE;
408      }
409
410      @Override
411      public int compare(byte[] left, byte[] right) {
412        // If theUnsafe weren't available, we wouldn't have selected this Comparator implementation.
413        Unsafe theUnsafe = requireNonNull(UnsafeComparator.theUnsafe);
414
415        int stride = 8;
416        int minLength = Math.min(left.length, right.length);
417        int strideLimit = minLength & ~(stride - 1);
418        int i;
419
420        /*
421         * Compare 8 bytes at a time. Benchmarking on x86 shows a stride of 8 bytes is no slower
422         * than 4 bytes even on 32-bit. On the other hand, it is substantially faster on 64-bit.
423         */
424        for (i = 0; i < strideLimit; i += stride) {
425          long lw = theUnsafe.getLong(left, BYTE_ARRAY_BASE_OFFSET + (long) i);
426          long rw = theUnsafe.getLong(right, BYTE_ARRAY_BASE_OFFSET + (long) i);
427          if (lw != rw) {
428            if (BIG_ENDIAN) {
429              return Long.compareUnsigned(lw, rw);
430            }
431
432            /*
433             * We want to compare only the first index where left[index] != right[index]. This
434             * corresponds to the least significant nonzero byte in lw ^ rw, since lw and rw are
435             * little-endian. Long.numberOfTrailingZeros(diff) tells us the least significant
436             * nonzero bit, and zeroing out the first three bits of L.nTZ gives us the shift to get
437             * that least significant nonzero byte.
438             */
439            int n = Long.numberOfTrailingZeros(lw ^ rw) & ~0x7;
440            return ((int) ((lw >>> n) & UNSIGNED_MASK)) - ((int) ((rw >>> n) & UNSIGNED_MASK));
441          }
442        }
443
444        // The epilogue to cover the last (minLength % stride) elements.
445        for (; i < minLength; i++) {
446          int result = UnsignedBytes.compare(left[i], right[i]);
447          if (result != 0) {
448            return result;
449          }
450        }
451        return left.length - right.length;
452      }
453
454      @Override
455      public String toString() {
456        return "UnsignedBytes.lexicographicalComparator() (sun.misc.Unsafe version)";
457      }
458    }
459
460    enum PureJavaComparator implements Comparator<byte[]> {
461      INSTANCE;
462
463      @Override
464      public int compare(byte[] left, byte[] right) {
465        int minLength = Math.min(left.length, right.length);
466        for (int i = 0; i < minLength; i++) {
467          int result = UnsignedBytes.compare(left[i], right[i]);
468          if (result != 0) {
469            return result;
470          }
471        }
472        return left.length - right.length;
473      }
474
475      @Override
476      public String toString() {
477        return "UnsignedBytes.lexicographicalComparator() (pure Java version)";
478      }
479    }
480
481    /** Returns the best comparator supported by the current runtime. */
482    static Comparator<byte[]> getBestComparator() {
483      Comparator<byte[]> arraysCompareUnsignedComparator =
484          ArraysCompareUnsignedComparatorMaker.INSTANCE.tryMakeArraysCompareUnsignedComparator();
485      if (arraysCompareUnsignedComparator != null) {
486        return arraysCompareUnsignedComparator;
487      }
488
489      try {
490        Class<? extends LexicographicalComparator> unsafeImpl =
491            Class.forName(UNSAFE_COMPARATOR_NAME).asSubclass(LexicographicalComparator.class);
492        // requireNonNull is safe because the class is an enum.
493        LexicographicalComparator unsafeComparator =
494            requireNonNull(unsafeImpl.getEnumConstants())[0];
495        return unsafeComparator.isFunctional()
496            ? unsafeComparator
497            : lexicographicalComparatorJavaImpl();
498      } catch (Throwable t) { // ensure we really catch *everything*
499        /*
500         * Now that UnsafeComparator is implemented to initialize successfully even when we know we
501         * can't use it, this `catch` block might now be necessary only:
502         *
503         * - in the Android flavor or anywhere else that users might be applying an optimizer that
504         * might strip UnsafeComparator entirely. (TODO(cpovirk): Are we confident that optimizers
505         * aren't stripping UnsafeComparator today? Should we have Proguard configuration for it?)
506         *
507         * - if Unsafe is removed entirely from JDKs (or already absent in some unusual environment
508         * today). TODO(b/392974826): Check for the existence of Unsafe and its methods
509         * reflectively before attempting to access UnsafeComparator. Or, better yet, allow
510         * UnsafeComparator to still initialize correctly even if Unsafe is unavailable. This would
511         * protect against users that automatically preinitialize internal classes that they've seen
512         * initialized in their apps in the past. To do that, we may need to move the references to
513         * Unsafe to another class and then ensure that the preinitialization logic doesn't start
514         * picking up the new class as part of loading UnsafeComparator!
515         */
516        return lexicographicalComparatorJavaImpl();
517      }
518    }
519
520    private LexicographicalComparatorHolder() {}
521  }
522
523  private enum ArraysCompareUnsignedComparatorMaker {
524    INSTANCE {
525      /** Implementation used by non-J2ObjC environments. */
526      // We use Arrays.compareUnsigned only after confirming that it's available at runtime.
527      @SuppressWarnings("Java8ApiChecker")
528      @IgnoreJRERequirement
529      @Override
530      @J2ObjCIncompatible
531      @Nullable Comparator<byte[]> tryMakeArraysCompareUnsignedComparator() {
532        try {
533          // Compare AbstractFuture.VarHandleAtomicHelperMaker.
534          Arrays.class.getMethod("compareUnsigned", byte[].class, byte[].class);
535        } catch (NoSuchMethodException beforeJava9) {
536          return null;
537        }
538        return ArraysCompareUnsignedComparator.INSTANCE;
539      }
540
541      // TODO(cpovirk): Implement toString, as the other implementations do?
542    };
543
544    /** Implementation used by J2ObjC environments, overridden for other environments. */
545    @Nullable Comparator<byte[]> tryMakeArraysCompareUnsignedComparator() {
546      return null;
547    }
548  }
549
550  @J2ObjCIncompatible
551  enum ArraysCompareUnsignedComparator implements Comparator<byte[]> {
552    INSTANCE;
553
554    @Override
555    // We use the class only after confirming that Arrays.compareUnsigned is available at runtime.
556    @SuppressWarnings("Java8ApiChecker")
557    @IgnoreJRERequirement
558    public int compare(byte[] left, byte[] right) {
559      return Arrays.compareUnsigned(left, right);
560    }
561  }
562
563  private static byte flip(byte b) {
564    return (byte) (b ^ 0x80);
565  }
566
567  /**
568   * Sorts the array, treating its elements as unsigned bytes.
569   *
570   * @since 23.1
571   */
572  public static void sort(byte[] array) {
573    checkNotNull(array);
574    sort(array, 0, array.length);
575  }
576
577  /**
578   * Sorts the array between {@code fromIndex} inclusive and {@code toIndex} exclusive, treating its
579   * elements as unsigned bytes.
580   *
581   * @since 23.1
582   */
583  public static void sort(byte[] array, int fromIndex, int toIndex) {
584    checkNotNull(array);
585    checkPositionIndexes(fromIndex, toIndex, array.length);
586    for (int i = fromIndex; i < toIndex; i++) {
587      array[i] = flip(array[i]);
588    }
589    Arrays.sort(array, fromIndex, toIndex);
590    for (int i = fromIndex; i < toIndex; i++) {
591      array[i] = flip(array[i]);
592    }
593  }
594
595  /**
596   * Sorts the elements of {@code array} in descending order, interpreting them as unsigned 8-bit
597   * integers.
598   *
599   * @since 23.1
600   */
601  public static void sortDescending(byte[] array) {
602    checkNotNull(array);
603    sortDescending(array, 0, array.length);
604  }
605
606  /**
607   * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex}
608   * exclusive in descending order, interpreting them as unsigned 8-bit integers.
609   *
610   * @since 23.1
611   */
612  public static void sortDescending(byte[] array, int fromIndex, int toIndex) {
613    checkNotNull(array);
614    checkPositionIndexes(fromIndex, toIndex, array.length);
615    for (int i = fromIndex; i < toIndex; i++) {
616      array[i] ^= Byte.MAX_VALUE;
617    }
618    Arrays.sort(array, fromIndex, toIndex);
619    for (int i = fromIndex; i < toIndex; i++) {
620      array[i] ^= Byte.MAX_VALUE;
621    }
622  }
623}