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