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