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    
017    package com.google.common.primitives;
018    
019    import static com.google.common.base.Preconditions.checkArgument;
020    import static com.google.common.base.Preconditions.checkNotNull;
021    
022    import com.google.common.annotations.Beta;
023    import com.google.common.annotations.VisibleForTesting;
024    
025    import sun.misc.Unsafe;
026    
027    import java.lang.reflect.Field;
028    import java.nio.ByteOrder;
029    import java.security.AccessController;
030    import java.security.PrivilegedAction;
031    import java.util.Comparator;
032    
033    /**
034     * Static utility methods pertaining to {@code byte} primitives that interpret
035     * values as <i>unsigned</i> (that is, any negative value {@code b} is treated
036     * as the positive value {@code 256 + b}). The corresponding methods that treat
037     * the values as signed are found in {@link SignedBytes}, and the methods for
038     * which signedness is not an issue are in {@link Bytes}.
039     *
040     * <p>See the Guava User Guide article on <a href=
041     * "http://code.google.com/p/guava-libraries/wiki/PrimitivesExplained">
042     * 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    public final class UnsignedBytes {
051      private UnsignedBytes() {}
052    
053      /**
054       * The largest power of two that can be represented as an unsigned {@code
055       * byte}.
056       *
057       * @since 10.0
058       */
059      public static final byte MAX_POWER_OF_TWO = (byte) 0x80;
060    
061      /**
062       * The largest value that fits into an unsigned byte.
063       *
064       * @since 13.0
065       */
066      public static final byte MAX_VALUE = (byte) 0xFF;
067    
068      private static final int UNSIGNED_MASK = 0xFF;
069    
070      /**
071       * Returns the value of the given byte as an integer, when treated as
072       * unsigned. That is, returns {@code value + 256} if {@code value} is
073       * negative; {@code value} itself otherwise.
074       *
075       * @since 6.0
076       */
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        checkArgument(value >> Byte.SIZE == 0, "out of range: %s", value);
093        return (byte) value;
094      }
095    
096      /**
097       * Returns the {@code byte} value that, when treated as unsigned, is nearest
098       * in value to {@code value}.
099       *
100       * @param value any {@code long} value
101       * @return {@code (byte) 255} if {@code value >= 255}, {@code (byte) 0} if
102       *     {@code value <= 0}, and {@code value} cast to {@code byte} otherwise
103       */
104      public static byte saturatedCast(long value) {
105        if (value > toInt(MAX_VALUE)) {
106          return MAX_VALUE; // -1
107        }
108        if (value < 0) {
109          return (byte) 0;
110        }
111        return (byte) value;
112      }
113    
114      /**
115       * Compares the two specified {@code byte} values, treating them as unsigned
116       * values between 0 and 255 inclusive. For example, {@code (byte) -127} is
117       * considered greater than {@code (byte) 127} because it is seen as having
118       * 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
123       *     value if {@code a} is 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}.
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
134       *     every other value in the array
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}.
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
154       *     to every other value in the array
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      @Beta
175      public static String toString(byte x) {
176        return toString(x, 10);
177      }
178    
179      /**
180       * Returns a string representation of {@code x} for the given radix, where {@code x} is treated
181       * as unsigned.
182       *
183       * @param x the value to convert to a string.
184       * @param radix the radix to use while working with {@code x}
185       * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX}
186       *         and {@link Character#MAX_RADIX}.
187       * @since 13.0
188       */
189      @Beta
190      public static String toString(byte x, int radix) {
191        checkArgument(radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX,
192            "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX", 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 long}
201       *         value
202       * @since 13.0
203       */
204      @Beta
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}
215       *         with the given radix, or if {@code radix} is not between {@link Character#MIN_RADIX}
216       *         and {@link Character#MAX_RADIX}.
217       * @since 13.0
218       */
219      @Beta
220      public static byte parseUnsignedByte(String string, int radix) {
221        int parse = Integer.parseInt(checkNotNull(string), radix);
222        // We need to throw a NumberFormatException, so we have to duplicate checkedCast. =(
223        if (parse >> Byte.SIZE == 0) {
224          return (byte) parse;
225        } else {
226          throw new NumberFormatException("out of range: " + parse);
227        }
228      }
229    
230      /**
231       * Returns a string containing the supplied {@code byte} values separated by
232       * {@code separator}. For example, {@code join(":", (byte) 1, (byte) 2,
233       * (byte) 255)} returns the string {@code "1:2:255"}.
234       *
235       * @param separator the text that should appear between consecutive values in
236       *     the resulting string (but not at the start or end)
237       * @param array an array of {@code byte} values, possibly empty
238       */
239      public static String join(String separator, byte... array) {
240        checkNotNull(separator);
241        if (array.length == 0) {
242          return "";
243        }
244    
245        // For pre-sizing a builder, just get the right order of magnitude
246        StringBuilder builder = new StringBuilder(array.length * (3 + separator.length()));
247        builder.append(toInt(array[0]));
248        for (int i = 1; i < array.length; i++) {
249          builder.append(separator).append(toString(array[i]));
250        }
251        return builder.toString();
252      }
253    
254      /**
255       * Returns a comparator that compares two {@code byte} arrays
256       * lexicographically. That is, it compares, using {@link
257       * #compare(byte, byte)}), the first pair of values that follow any common
258       * prefix, or when one array is a prefix of the other, treats the shorter
259       * array as the lesser. For example, {@code [] < [0x01] < [0x01, 0x7F] <
260       * [0x01, 0x80] < [0x02]}. Values are treated as unsigned.
261       *
262       * <p>The returned comparator is inconsistent with {@link
263       * Object#equals(Object)} (since arrays support only identity equality), but
264       * it is consistent with {@link java.util.Arrays#equals(byte[], byte[])}.
265       *
266       * @see <a href="http://en.wikipedia.org/wiki/Lexicographical_order">
267       *     Lexicographical order article at Wikipedia</a>
268       * @since 2.0
269       */
270      public static Comparator<byte[]> lexicographicalComparator() {
271        return LexicographicalComparatorHolder.BEST_COMPARATOR;
272      }
273    
274      @VisibleForTesting
275      static Comparator<byte[]> lexicographicalComparatorJavaImpl() {
276        return LexicographicalComparatorHolder.PureJavaComparator.INSTANCE;
277      }
278    
279      /**
280       * Provides a lexicographical comparator implementation; either a Java
281       * implementation or a faster implementation based on {@link Unsafe}.
282       *
283       * <p>Uses reflection to gracefully fall back to the Java implementation if
284       * {@code Unsafe} isn't available.
285       */
286      @VisibleForTesting
287      static class LexicographicalComparatorHolder {
288        static final String UNSAFE_COMPARATOR_NAME =
289            LexicographicalComparatorHolder.class.getName() + "$UnsafeComparator";
290    
291        static final Comparator<byte[]> BEST_COMPARATOR = getBestComparator();
292    
293        @VisibleForTesting
294        enum UnsafeComparator implements Comparator<byte[]> {
295          INSTANCE;
296    
297          static final boolean littleEndian =
298              ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN);
299    
300          /*
301           * The following static final fields exist for performance reasons.
302           *
303           * In UnsignedBytesBenchmark, accessing the following objects via static
304           * final fields is the fastest (more than twice as fast as the Java
305           * implementation, vs ~1.5x with non-final static fields, on x86_32)
306           * under the Hotspot server compiler. The reason is obviously that the
307           * non-final fields need to be reloaded inside the loop.
308           *
309           * And, no, defining (final or not) local variables out of the loop still
310           * isn't as good because the null check on the theUnsafe object remains
311           * inside the loop and BYTE_ARRAY_BASE_OFFSET doesn't get
312           * constant-folded.
313           *
314           * The compiler can treat static final fields as compile-time constants
315           * and can constant-fold them while (final or not) local variables are
316           * run time values.
317           */
318    
319          static final Unsafe theUnsafe;
320    
321          /** The offset to the first element in a byte array. */
322          static final int BYTE_ARRAY_BASE_OFFSET;
323    
324          static {
325            theUnsafe = (Unsafe) AccessController.doPrivileged(
326                new PrivilegedAction<Object>() {
327                  @Override
328                  public Object run() {
329                    try {
330                      Field f = Unsafe.class.getDeclaredField("theUnsafe");
331                      f.setAccessible(true);
332                      return f.get(null);
333                    } catch (NoSuchFieldException e) {
334                      // It doesn't matter what we throw;
335                      // it's swallowed in getBestComparator().
336                      throw new Error();
337                    } catch (IllegalAccessException e) {
338                      throw new Error();
339                    }
340                  }
341                });
342    
343            BYTE_ARRAY_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
344    
345            // sanity check - this should never fail
346            if (theUnsafe.arrayIndexScale(byte[].class) != 1) {
347              throw new AssertionError();
348            }
349          }
350    
351          @Override public int compare(byte[] left, byte[] right) {
352            int minLength = Math.min(left.length, right.length);
353            int minWords = minLength / Longs.BYTES;
354    
355            /*
356             * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
357             * time is no slower than comparing 4 bytes at a time even on 32-bit.
358             * On the other hand, it is substantially faster on 64-bit.
359             */
360            for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) {
361              long lw = theUnsafe.getLong(left, BYTE_ARRAY_BASE_OFFSET + (long) i);
362              long rw = theUnsafe.getLong(right, BYTE_ARRAY_BASE_OFFSET + (long) i);
363              long diff = lw ^ rw;
364    
365              if (diff != 0) {
366                if (!littleEndian) {
367                  return UnsignedLongs.compare(lw, rw);
368                }
369    
370                // Use binary search
371                int n = 0;
372                int y;
373                int x = (int) diff;
374                if (x == 0) {
375                  x = (int) (diff >>> 32);
376                  n = 32;
377                }
378    
379                y = x << 16;
380                if (y == 0) {
381                  n += 16;
382                } else {
383                  x = y;
384                }
385    
386                y = x << 8;
387                if (y == 0) {
388                  n += 8;
389                }
390                return (int) (((lw >>> n) & UNSIGNED_MASK) - ((rw >>> n) & UNSIGNED_MASK));
391              }
392            }
393    
394            // The epilogue to cover the last (minLength % 8) elements.
395            for (int i = minWords * Longs.BYTES; i < minLength; i++) {
396              int result = UnsignedBytes.compare(left[i], right[i]);
397              if (result != 0) {
398                return result;
399              }
400            }
401            return left.length - right.length;
402          }
403        }
404    
405        enum PureJavaComparator implements Comparator<byte[]> {
406          INSTANCE;
407    
408          @Override public int compare(byte[] left, byte[] right) {
409            int minLength = Math.min(left.length, right.length);
410            for (int i = 0; i < minLength; i++) {
411              int result = UnsignedBytes.compare(left[i], right[i]);
412              if (result != 0) {
413                return result;
414              }
415            }
416            return left.length - right.length;
417          }
418        }
419    
420        /**
421         * Returns the Unsafe-using Comparator, or falls back to the pure-Java
422         * implementation if unable to do so.
423         */
424        static Comparator<byte[]> getBestComparator() {
425          try {
426            Class<?> theClass = Class.forName(UNSAFE_COMPARATOR_NAME);
427    
428            // yes, UnsafeComparator does implement Comparator<byte[]>
429            @SuppressWarnings("unchecked")
430            Comparator<byte[]> comparator =
431                (Comparator<byte[]>) theClass.getEnumConstants()[0];
432            return comparator;
433          } catch (Throwable t) { // ensure we really catch *everything*
434            return lexicographicalComparatorJavaImpl();
435          }
436        }
437      }
438    }