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