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