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