View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import static com.google.common.base.Preconditions.checkArgument;
21  import static com.google.common.base.Preconditions.checkNotNull;
22  import static com.google.common.base.Preconditions.checkPositionIndex;
23  
24  import java.io.DataInput;
25  import java.io.DataOutput;
26  import java.io.IOException;
27  import java.lang.reflect.Field;
28  import java.math.BigDecimal;
29  import java.math.BigInteger;
30  import java.nio.ByteBuffer;
31  import java.nio.ByteOrder;
32  import java.nio.charset.Charset;
33  import java.security.AccessController;
34  import java.security.PrivilegedAction;
35  import java.security.SecureRandom;
36  import java.util.Arrays;
37  import java.util.Collection;
38  import java.util.Comparator;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  import org.apache.hadoop.hbase.classification.InterfaceAudience;
45  import org.apache.hadoop.hbase.classification.InterfaceStability;
46  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
47  import org.apache.hadoop.io.RawComparator;
48  import org.apache.hadoop.io.WritableComparator;
49  import org.apache.hadoop.io.WritableUtils;
50  
51  import sun.misc.Unsafe;
52  
53  import com.google.common.annotations.VisibleForTesting;
54  import com.google.common.collect.Lists;
55  import org.apache.hadoop.hbase.util.Bytes.LexicographicalComparerHolder.UnsafeComparer;
56  
57  /**
58   * Utility class that handles byte arrays, conversions to/from other types,
59   * comparisons, hash code generation, manufacturing keys for HashMaps or
60   * HashSets, etc.
61   */
62  @InterfaceAudience.Public
63  @InterfaceStability.Stable
64  public class Bytes {
65    //HConstants.UTF8_ENCODING should be updated if this changed
66    /** When we encode strings, we always specify UTF8 encoding */
67    private static final String UTF8_ENCODING = "UTF-8";
68  
69    //HConstants.UTF8_CHARSET should be updated if this changed
70    /** When we encode strings, we always specify UTF8 encoding */
71    private static final Charset UTF8_CHARSET = Charset.forName(UTF8_ENCODING);
72  
73    //HConstants.EMPTY_BYTE_ARRAY should be updated if this changed
74    private static final byte [] EMPTY_BYTE_ARRAY = new byte [0];
75  
76    private static final Log LOG = LogFactory.getLog(Bytes.class);
77  
78    /**
79     * Size of boolean in bytes
80     */
81    public static final int SIZEOF_BOOLEAN = Byte.SIZE / Byte.SIZE;
82  
83    /**
84     * Size of byte in bytes
85     */
86    public static final int SIZEOF_BYTE = SIZEOF_BOOLEAN;
87  
88    /**
89     * Size of char in bytes
90     */
91    public static final int SIZEOF_CHAR = Character.SIZE / Byte.SIZE;
92  
93    /**
94     * Size of double in bytes
95     */
96    public static final int SIZEOF_DOUBLE = Double.SIZE / Byte.SIZE;
97  
98    /**
99     * Size of float in bytes
100    */
101   public static final int SIZEOF_FLOAT = Float.SIZE / Byte.SIZE;
102 
103   /**
104    * Size of int in bytes
105    */
106   public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE;
107 
108   /**
109    * Size of long in bytes
110    */
111   public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE;
112 
113   /**
114    * Size of short in bytes
115    */
116   public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE;
117 
118 
119   /**
120    * Estimate of size cost to pay beyond payload in jvm for instance of byte [].
121    * Estimate based on study of jhat and jprofiler numbers.
122    */
123   // JHat says BU is 56 bytes.
124   // SizeOf which uses java.lang.instrument says 24 bytes. (3 longs?)
125   public static final int ESTIMATED_HEAP_TAX = 16;
126 
127   
128   /**
129    * Returns length of the byte array, returning 0 if the array is null.
130    * Useful for calculating sizes.
131    * @param b byte array, which can be null
132    * @return 0 if b is null, otherwise returns length
133    */
134   final public static int len(byte[] b) {
135     return b == null ? 0 : b.length;
136   }
137 
138   /**
139    * Byte array comparator class.
140    */
141   @InterfaceAudience.Public
142   @InterfaceStability.Stable
143   public static class ByteArrayComparator implements RawComparator<byte []> {
144     /**
145      * Constructor
146      */
147     public ByteArrayComparator() {
148       super();
149     }
150     @Override
151     public int compare(byte [] left, byte [] right) {
152       return compareTo(left, right);
153     }
154     @Override
155     public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) {
156       return LexicographicalComparerHolder.BEST_COMPARER.
157         compareTo(b1, s1, l1, b2, s2, l2);
158     }
159   }
160 
161   /**
162    * A {@link ByteArrayComparator} that treats the empty array as the largest value.
163    * This is useful for comparing row end keys for regions.
164    */
165   // TODO: unfortunately, HBase uses byte[0] as both start and end keys for region
166   // boundaries. Thus semantically, we should treat empty byte array as the smallest value
167   // while comparing row keys, start keys etc; but as the largest value for comparing
168   // region boundaries for endKeys.
169   @InterfaceAudience.Public
170   @InterfaceStability.Stable
171   public static class RowEndKeyComparator extends ByteArrayComparator {
172     @Override
173     public int compare(byte[] left, byte[] right) {
174       return compare(left, 0, left.length, right, 0, right.length);
175     }
176     @Override
177     public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
178       if (b1 == b2 && s1 == s2 && l1 == l2) {
179         return 0;
180       }
181       if (l1 == 0) {
182         return l2; //0 or positive
183       }
184       if (l2 == 0) {
185         return -1;
186       }
187       return super.compare(b1, s1, l1, b2, s2, l2);
188     }
189   }
190 
191   /**
192    * Pass this to TreeMaps where byte [] are keys.
193    */
194   public final static Comparator<byte []> BYTES_COMPARATOR = new ByteArrayComparator();
195 
196   /**
197    * Use comparing byte arrays, byte-by-byte
198    */
199   public final static RawComparator<byte []> BYTES_RAWCOMPARATOR = new ByteArrayComparator();
200 
201   /**
202    * Read byte-array written with a WritableableUtils.vint prefix.
203    * @param in Input to read from.
204    * @return byte array read off <code>in</code>
205    * @throws IOException e
206    */
207   public static byte [] readByteArray(final DataInput in)
208   throws IOException {
209     int len = WritableUtils.readVInt(in);
210     if (len < 0) {
211       throw new NegativeArraySizeException(Integer.toString(len));
212     }
213     byte [] result = new byte[len];
214     in.readFully(result, 0, len);
215     return result;
216   }
217 
218   /**
219    * Read byte-array written with a WritableableUtils.vint prefix.
220    * IOException is converted to a RuntimeException.
221    * @param in Input to read from.
222    * @return byte array read off <code>in</code>
223    */
224   public static byte [] readByteArrayThrowsRuntime(final DataInput in) {
225     try {
226       return readByteArray(in);
227     } catch (Exception e) {
228       throw new RuntimeException(e);
229     }
230   }
231 
232   /**
233    * Write byte-array with a WritableableUtils.vint prefix.
234    * @param out output stream to be written to
235    * @param b array to write
236    * @throws IOException e
237    */
238   public static void writeByteArray(final DataOutput out, final byte [] b)
239   throws IOException {
240     if(b == null) {
241       WritableUtils.writeVInt(out, 0);
242     } else {
243       writeByteArray(out, b, 0, b.length);
244     }
245   }
246 
247   /**
248    * Write byte-array to out with a vint length prefix.
249    * @param out output stream
250    * @param b array
251    * @param offset offset into array
252    * @param length length past offset
253    * @throws IOException e
254    */
255   public static void writeByteArray(final DataOutput out, final byte [] b,
256       final int offset, final int length)
257   throws IOException {
258     WritableUtils.writeVInt(out, length);
259     out.write(b, offset, length);
260   }
261 
262   /**
263    * Write byte-array from src to tgt with a vint length prefix.
264    * @param tgt target array
265    * @param tgtOffset offset into target array
266    * @param src source array
267    * @param srcOffset source offset
268    * @param srcLength source length
269    * @return New offset in src array.
270    */
271   public static int writeByteArray(final byte [] tgt, final int tgtOffset,
272       final byte [] src, final int srcOffset, final int srcLength) {
273     byte [] vint = vintToBytes(srcLength);
274     System.arraycopy(vint, 0, tgt, tgtOffset, vint.length);
275     int offset = tgtOffset + vint.length;
276     System.arraycopy(src, srcOffset, tgt, offset, srcLength);
277     return offset + srcLength;
278   }
279 
280   /**
281    * Put bytes at the specified byte array position.
282    * @param tgtBytes the byte array
283    * @param tgtOffset position in the array
284    * @param srcBytes array to write out
285    * @param srcOffset source offset
286    * @param srcLength source length
287    * @return incremented offset
288    */
289   public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes,
290       int srcOffset, int srcLength) {
291     System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength);
292     return tgtOffset + srcLength;
293   }
294 
295   /**
296    * Write a single byte out to the specified byte array position.
297    * @param bytes the byte array
298    * @param offset position in the array
299    * @param b byte to write out
300    * @return incremented offset
301    */
302   public static int putByte(byte[] bytes, int offset, byte b) {
303     bytes[offset] = b;
304     return offset + 1;
305   }
306 
307   /**
308    * Add the whole content of the ByteBuffer to the bytes arrays. The ByteBuffer is modified.
309    * @param bytes the byte array
310    * @param offset position in the array
311    * @param buf ByteBuffer to write out
312    * @return incremented offset
313    */
314   public static int putByteBuffer(byte[] bytes, int offset, ByteBuffer buf) {
315     int len = buf.remaining();
316     buf.get(bytes, offset, len);
317     return offset + len;
318   }
319 
320   /**
321    * Returns a new byte array, copied from the given {@code buf},
322    * from the index 0 (inclusive) to the limit (exclusive),
323    * regardless of the current position.
324    * The position and the other index parameters are not changed.
325    *
326    * @param buf a byte buffer
327    * @return the byte array
328    * @see #getBytes(ByteBuffer)
329    */
330   public static byte[] toBytes(ByteBuffer buf) {
331     ByteBuffer dup = buf.duplicate();
332     dup.position(0);
333     return readBytes(dup);
334   }
335 
336   private static byte[] readBytes(ByteBuffer buf) {
337     byte [] result = new byte[buf.remaining()];
338     buf.get(result);
339     return result;
340   }
341 
342   /**
343    * @param b Presumed UTF-8 encoded byte array.
344    * @return String made from <code>b</code>
345    */
346   public static String toString(final byte [] b) {
347     if (b == null) {
348       return null;
349     }
350     return toString(b, 0, b.length);
351   }
352 
353   /**
354    * Joins two byte arrays together using a separator.
355    * @param b1 The first byte array.
356    * @param sep The separator to use.
357    * @param b2 The second byte array.
358    */
359   public static String toString(final byte [] b1,
360                                 String sep,
361                                 final byte [] b2) {
362     return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length);
363   }
364 
365   /**
366    * This method will convert utf8 encoded bytes into a string. If
367    * the given byte array is null, this method will return null.
368    *
369    * @param b Presumed UTF-8 encoded byte array.
370    * @param off offset into array
371    * @param len length of utf-8 sequence
372    * @return String made from <code>b</code> or null
373    */
374   public static String toString(final byte [] b, int off, int len) {
375     if (b == null) {
376       return null;
377     }
378     if (len == 0) {
379       return "";
380     }
381     return new String(b, off, len, UTF8_CHARSET);
382   }
383 
384   /**
385    * Write a printable representation of a byte array.
386    *
387    * @param b byte array
388    * @return string
389    * @see #toStringBinary(byte[], int, int)
390    */
391   public static String toStringBinary(final byte [] b) {
392     if (b == null)
393       return "null";
394     return toStringBinary(b, 0, b.length);
395   }
396 
397   /**
398    * Converts the given byte buffer to a printable representation,
399    * from the index 0 (inclusive) to the limit (exclusive),
400    * regardless of the current position.
401    * The position and the other index parameters are not changed.
402    *
403    * @param buf a byte buffer
404    * @return a string representation of the buffer's binary contents
405    * @see #toBytes(ByteBuffer)
406    * @see #getBytes(ByteBuffer)
407    */
408   public static String toStringBinary(ByteBuffer buf) {
409     if (buf == null)
410       return "null";
411     if (buf.hasArray()) {
412       return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit());
413     }
414     return toStringBinary(toBytes(buf));
415   }
416 
417   /**
418    * Write a printable representation of a byte array. Non-printable
419    * characters are hex escaped in the format \\x%02X, eg:
420    * \x00 \x05 etc
421    *
422    * @param b array to write out
423    * @param off offset to start at
424    * @param len length to write
425    * @return string output
426    */
427   public static String toStringBinary(final byte [] b, int off, int len) {
428     StringBuilder result = new StringBuilder();
429     // Just in case we are passed a 'len' that is > buffer length...
430     if (off >= b.length) return result.toString();
431     if (off + len > b.length) len = b.length - off;
432     for (int i = off; i < off + len ; ++i ) {
433       int ch = b[i] & 0xFF;
434       if ( (ch >= '0' && ch <= '9')
435           || (ch >= 'A' && ch <= 'Z')
436           || (ch >= 'a' && ch <= 'z')
437           || " `~!@#$%^&*()-_=+[]{}|;:'\",.<>/?".indexOf(ch) >= 0 ) {
438         result.append((char)ch);
439       } else {
440         result.append(String.format("\\x%02X", ch));
441       }
442     }
443     return result.toString();
444   }
445 
446   private static boolean isHexDigit(char c) {
447     return
448         (c >= 'A' && c <= 'F') ||
449         (c >= '0' && c <= '9');
450   }
451 
452   /**
453    * Takes a ASCII digit in the range A-F0-9 and returns
454    * the corresponding integer/ordinal value.
455    * @param ch  The hex digit.
456    * @return The converted hex value as a byte.
457    */
458   public static byte toBinaryFromHex(byte ch) {
459     if ( ch >= 'A' && ch <= 'F' )
460       return (byte) ((byte)10 + (byte) (ch - 'A'));
461     // else
462     return (byte) (ch - '0');
463   }
464 
465   public static byte [] toBytesBinary(String in) {
466     // this may be bigger than we need, but let's be safe.
467     byte [] b = new byte[in.length()];
468     int size = 0;
469     for (int i = 0; i < in.length(); ++i) {
470       char ch = in.charAt(i);
471       if (ch == '\\' && in.length() > i+1 && in.charAt(i+1) == 'x') {
472         // ok, take next 2 hex digits.
473         char hd1 = in.charAt(i+2);
474         char hd2 = in.charAt(i+3);
475 
476         // they need to be A-F0-9:
477         if (!isHexDigit(hd1) ||
478             !isHexDigit(hd2)) {
479           // bogus escape code, ignore:
480           continue;
481         }
482         // turn hex ASCII digit -> number
483         byte d = (byte) ((toBinaryFromHex((byte)hd1) << 4) + toBinaryFromHex((byte)hd2));
484 
485         b[size++] = d;
486         i += 3; // skip 3
487       } else {
488         b[size++] = (byte) ch;
489       }
490     }
491     // resize:
492     byte [] b2 = new byte[size];
493     System.arraycopy(b, 0, b2, 0, size);
494     return b2;
495   }
496 
497   /**
498    * Converts a string to a UTF-8 byte array.
499    * @param s string
500    * @return the byte array
501    */
502   public static byte[] toBytes(String s) {
503     return s.getBytes(UTF8_CHARSET);
504   }
505 
506   /**
507    * Convert a boolean to a byte array. True becomes -1
508    * and false becomes 0.
509    *
510    * @param b value
511    * @return <code>b</code> encoded in a byte array.
512    */
513   public static byte [] toBytes(final boolean b) {
514     return new byte[] { b ? (byte) -1 : (byte) 0 };
515   }
516 
517   /**
518    * Reverses {@link #toBytes(boolean)}
519    * @param b array
520    * @return True or false.
521    */
522   public static boolean toBoolean(final byte [] b) {
523     if (b.length != 1) {
524       throw new IllegalArgumentException("Array has wrong size: " + b.length);
525     }
526     return b[0] != (byte) 0;
527   }
528 
529   /**
530    * Convert a long value to a byte array using big-endian.
531    *
532    * @param val value to convert
533    * @return the byte array
534    */
535   public static byte[] toBytes(long val) {
536     byte [] b = new byte[8];
537     for (int i = 7; i > 0; i--) {
538       b[i] = (byte) val;
539       val >>>= 8;
540     }
541     b[0] = (byte) val;
542     return b;
543   }
544 
545   /**
546    * Converts a byte array to a long value. Reverses
547    * {@link #toBytes(long)}
548    * @param bytes array
549    * @return the long value
550    */
551   public static long toLong(byte[] bytes) {
552     return toLong(bytes, 0, SIZEOF_LONG);
553   }
554 
555   /**
556    * Converts a byte array to a long value. Assumes there will be
557    * {@link #SIZEOF_LONG} bytes available.
558    *
559    * @param bytes bytes
560    * @param offset offset
561    * @return the long value
562    */
563   public static long toLong(byte[] bytes, int offset) {
564     return toLong(bytes, offset, SIZEOF_LONG);
565   }
566 
567   /**
568    * Converts a byte array to a long value.
569    *
570    * @param bytes array of bytes
571    * @param offset offset into array
572    * @param length length of data (must be {@link #SIZEOF_LONG})
573    * @return the long value
574    * @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or
575    * if there's not enough room in the array at the offset indicated.
576    */
577   public static long toLong(byte[] bytes, int offset, final int length) {
578     if (length != SIZEOF_LONG || offset + length > bytes.length) {
579       throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG);
580     }
581     if (UnsafeComparer.isAvailable()) {
582       return toLongUnsafe(bytes, offset);
583     } else {
584       long l = 0;
585       for(int i = offset; i < offset + length; i++) {
586         l <<= 8;
587         l ^= bytes[i] & 0xFF;
588       }
589       return l;
590     }
591   }
592 
593   private static IllegalArgumentException
594     explainWrongLengthOrOffset(final byte[] bytes,
595                                final int offset,
596                                final int length,
597                                final int expectedLength) {
598     String reason;
599     if (length != expectedLength) {
600       reason = "Wrong length: " + length + ", expected " + expectedLength;
601     } else {
602      reason = "offset (" + offset + ") + length (" + length + ") exceed the"
603         + " capacity of the array: " + bytes.length;
604     }
605     return new IllegalArgumentException(reason);
606   }
607 
608   /**
609    * Put a long value out to the specified byte array position.
610    * @param bytes the byte array
611    * @param offset position in the array
612    * @param val long to write out
613    * @return incremented offset
614    * @throws IllegalArgumentException if the byte array given doesn't have
615    * enough room at the offset specified.
616    */
617   public static int putLong(byte[] bytes, int offset, long val) {
618     if (bytes.length - offset < SIZEOF_LONG) {
619       throw new IllegalArgumentException("Not enough room to put a long at"
620           + " offset " + offset + " in a " + bytes.length + " byte array");
621     }
622     if (UnsafeComparer.isAvailable()) {
623       return putLongUnsafe(bytes, offset, val);
624     } else {
625       for(int i = offset + 7; i > offset; i--) {
626         bytes[i] = (byte) val;
627         val >>>= 8;
628       }
629       bytes[offset] = (byte) val;
630       return offset + SIZEOF_LONG;
631     }
632   }
633 
634   /**
635    * Put a long value out to the specified byte array position (Unsafe).
636    * @param bytes the byte array
637    * @param offset position in the array
638    * @param val long to write out
639    * @return incremented offset
640    */
641   public static int putLongUnsafe(byte[] bytes, int offset, long val)
642   {
643     if (UnsafeComparer.littleEndian) {
644       val = Long.reverseBytes(val);
645     }
646     UnsafeComparer.theUnsafe.putLong(bytes, (long) offset +
647       UnsafeComparer.BYTE_ARRAY_BASE_OFFSET , val);
648     return offset + SIZEOF_LONG;
649   }
650 
651   /**
652    * Presumes float encoded as IEEE 754 floating-point "single format"
653    * @param bytes byte array
654    * @return Float made from passed byte array.
655    */
656   public static float toFloat(byte [] bytes) {
657     return toFloat(bytes, 0);
658   }
659 
660   /**
661    * Presumes float encoded as IEEE 754 floating-point "single format"
662    * @param bytes array to convert
663    * @param offset offset into array
664    * @return Float made from passed byte array.
665    */
666   public static float toFloat(byte [] bytes, int offset) {
667     return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT));
668   }
669 
670   /**
671    * @param bytes byte array
672    * @param offset offset to write to
673    * @param f float value
674    * @return New offset in <code>bytes</code>
675    */
676   public static int putFloat(byte [] bytes, int offset, float f) {
677     return putInt(bytes, offset, Float.floatToRawIntBits(f));
678   }
679 
680   /**
681    * @param f float value
682    * @return the float represented as byte []
683    */
684   public static byte [] toBytes(final float f) {
685     // Encode it as int
686     return Bytes.toBytes(Float.floatToRawIntBits(f));
687   }
688 
689   /**
690    * @param bytes byte array
691    * @return Return double made from passed bytes.
692    */
693   public static double toDouble(final byte [] bytes) {
694     return toDouble(bytes, 0);
695   }
696 
697   /**
698    * @param bytes byte array
699    * @param offset offset where double is
700    * @return Return double made from passed bytes.
701    */
702   public static double toDouble(final byte [] bytes, final int offset) {
703     return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG));
704   }
705 
706   /**
707    * @param bytes byte array
708    * @param offset offset to write to
709    * @param d value
710    * @return New offset into array <code>bytes</code>
711    */
712   public static int putDouble(byte [] bytes, int offset, double d) {
713     return putLong(bytes, offset, Double.doubleToLongBits(d));
714   }
715 
716   /**
717    * Serialize a double as the IEEE 754 double format output. The resultant
718    * array will be 8 bytes long.
719    *
720    * @param d value
721    * @return the double represented as byte []
722    */
723   public static byte [] toBytes(final double d) {
724     // Encode it as a long
725     return Bytes.toBytes(Double.doubleToRawLongBits(d));
726   }
727 
728   /**
729    * Convert an int value to a byte array.  Big-endian.  Same as what DataOutputStream.writeInt
730    * does.
731    *
732    * @param val value
733    * @return the byte array
734    */
735   public static byte[] toBytes(int val) {
736     byte [] b = new byte[4];
737     for(int i = 3; i > 0; i--) {
738       b[i] = (byte) val;
739       val >>>= 8;
740     }
741     b[0] = (byte) val;
742     return b;
743   }
744 
745   /**
746    * Converts a byte array to an int value
747    * @param bytes byte array
748    * @return the int value
749    */
750   public static int toInt(byte[] bytes) {
751     return toInt(bytes, 0, SIZEOF_INT);
752   }
753 
754   /**
755    * Converts a byte array to an int value
756    * @param bytes byte array
757    * @param offset offset into array
758    * @return the int value
759    */
760   public static int toInt(byte[] bytes, int offset) {
761     return toInt(bytes, offset, SIZEOF_INT);
762   }
763 
764   /**
765    * Converts a byte array to an int value
766    * @param bytes byte array
767    * @param offset offset into array
768    * @param length length of int (has to be {@link #SIZEOF_INT})
769    * @return the int value
770    * @throws IllegalArgumentException if length is not {@link #SIZEOF_INT} or
771    * if there's not enough room in the array at the offset indicated.
772    */
773   public static int toInt(byte[] bytes, int offset, final int length) {
774     if (length != SIZEOF_INT || offset + length > bytes.length) {
775       throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT);
776     }
777     if (UnsafeComparer.isAvailable()) {
778       return toIntUnsafe(bytes, offset);
779     } else {
780       int n = 0;
781       for(int i = offset; i < (offset + length); i++) {
782         n <<= 8;
783         n ^= bytes[i] & 0xFF;
784       }
785       return n;
786     }
787   }
788 
789   /**
790    * Converts a byte array to an int value (Unsafe version)
791    * @param bytes byte array
792    * @param offset offset into array
793    * @return the int value
794    */
795   public static int toIntUnsafe(byte[] bytes, int offset) {
796     if (UnsafeComparer.littleEndian) {
797       return Integer.reverseBytes(UnsafeComparer.theUnsafe.getInt(bytes,
798         (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
799     } else {
800       return UnsafeComparer.theUnsafe.getInt(bytes,
801         (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
802     }
803   }
804 
805   /**
806    * Converts a byte array to an short value (Unsafe version)
807    * @param bytes byte array
808    * @param offset offset into array
809    * @return the short value
810    */
811   public static short toShortUnsafe(byte[] bytes, int offset) {
812     if (UnsafeComparer.littleEndian) {
813       return Short.reverseBytes(UnsafeComparer.theUnsafe.getShort(bytes,
814         (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
815     } else {
816       return UnsafeComparer.theUnsafe.getShort(bytes,
817         (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
818     }
819   }
820 
821   /**
822    * Converts a byte array to an long value (Unsafe version)
823    * @param bytes byte array
824    * @param offset offset into array
825    * @return the long value
826    */
827   public static long toLongUnsafe(byte[] bytes, int offset) {
828     if (UnsafeComparer.littleEndian) {
829       return Long.reverseBytes(UnsafeComparer.theUnsafe.getLong(bytes,
830         (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
831     } else {
832       return UnsafeComparer.theUnsafe.getLong(bytes,
833         (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
834     }
835   }
836 
837   /**
838    * Converts a byte array to an int value
839    * @param bytes byte array
840    * @param offset offset into array
841    * @param length how many bytes should be considered for creating int
842    * @return the int value
843    * @throws IllegalArgumentException if there's not enough room in the array at the offset
844    * indicated.
845    */
846   public static int readAsInt(byte[] bytes, int offset, final int length) {
847     if (offset + length > bytes.length) {
848       throw new IllegalArgumentException("offset (" + offset + ") + length (" + length
849           + ") exceed the" + " capacity of the array: " + bytes.length);
850     }
851     int n = 0;
852     for(int i = offset; i < (offset + length); i++) {
853       n <<= 8;
854       n ^= bytes[i] & 0xFF;
855     }
856     return n;
857   }
858 
859   /**
860    * Put an int value out to the specified byte array position.
861    * @param bytes the byte array
862    * @param offset position in the array
863    * @param val int to write out
864    * @return incremented offset
865    * @throws IllegalArgumentException if the byte array given doesn't have
866    * enough room at the offset specified.
867    */
868   public static int putInt(byte[] bytes, int offset, int val) {
869     if (bytes.length - offset < SIZEOF_INT) {
870       throw new IllegalArgumentException("Not enough room to put an int at"
871           + " offset " + offset + " in a " + bytes.length + " byte array");
872     }
873     if (UnsafeComparer.isAvailable()) {
874       return putIntUnsafe(bytes, offset, val);
875     } else {
876       for(int i= offset + 3; i > offset; i--) {
877         bytes[i] = (byte) val;
878         val >>>= 8;
879       }
880       bytes[offset] = (byte) val;
881       return offset + SIZEOF_INT;
882     }
883   }
884 
885   /**
886    * Put an int value out to the specified byte array position (Unsafe).
887    * @param bytes the byte array
888    * @param offset position in the array
889    * @param val int to write out
890    * @return incremented offset
891    */
892   public static int putIntUnsafe(byte[] bytes, int offset, int val)
893   {
894     if (UnsafeComparer.littleEndian) {
895       val = Integer.reverseBytes(val);
896     }
897     UnsafeComparer.theUnsafe.putInt(bytes, (long) offset +
898       UnsafeComparer.BYTE_ARRAY_BASE_OFFSET , val);
899     return offset + SIZEOF_INT;
900   }
901 
902   /**
903    * Convert a short value to a byte array of {@link #SIZEOF_SHORT} bytes long.
904    * @param val value
905    * @return the byte array
906    */
907   public static byte[] toBytes(short val) {
908     byte[] b = new byte[SIZEOF_SHORT];
909     b[1] = (byte) val;
910     val >>= 8;
911     b[0] = (byte) val;
912     return b;
913   }
914 
915   /**
916    * Converts a byte array to a short value
917    * @param bytes byte array
918    * @return the short value
919    */
920   public static short toShort(byte[] bytes) {
921     return toShort(bytes, 0, SIZEOF_SHORT);
922   }
923 
924   /**
925    * Converts a byte array to a short value
926    * @param bytes byte array
927    * @param offset offset into array
928    * @return the short value
929    */
930   public static short toShort(byte[] bytes, int offset) {
931     return toShort(bytes, offset, SIZEOF_SHORT);
932   }
933 
934   /**
935    * Converts a byte array to a short value
936    * @param bytes byte array
937    * @param offset offset into array
938    * @param length length, has to be {@link #SIZEOF_SHORT}
939    * @return the short value
940    * @throws IllegalArgumentException if length is not {@link #SIZEOF_SHORT}
941    * or if there's not enough room in the array at the offset indicated.
942    */
943   public static short toShort(byte[] bytes, int offset, final int length) {
944     if (length != SIZEOF_SHORT || offset + length > bytes.length) {
945       throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT);
946     }
947     if (UnsafeComparer.isAvailable()) {
948       return toShortUnsafe(bytes, offset);
949     } else {
950       short n = 0;
951       n ^= bytes[offset] & 0xFF;
952       n <<= 8;
953       n ^= bytes[offset+1] & 0xFF;
954       return n;
955    }
956   }
957 
958   /**
959    * Returns a new byte array, copied from the given {@code buf},
960    * from the position (inclusive) to the limit (exclusive).
961    * The position and the other index parameters are not changed.
962    *
963    * @param buf a byte buffer
964    * @return the byte array
965    * @see #toBytes(ByteBuffer)
966    */
967   public static byte[] getBytes(ByteBuffer buf) {
968     return readBytes(buf.duplicate());
969   }
970 
971   /**
972    * Put a short value out to the specified byte array position.
973    * @param bytes the byte array
974    * @param offset position in the array
975    * @param val short to write out
976    * @return incremented offset
977    * @throws IllegalArgumentException if the byte array given doesn't have
978    * enough room at the offset specified.
979    */
980   public static int putShort(byte[] bytes, int offset, short val) {
981     if (bytes.length - offset < SIZEOF_SHORT) {
982       throw new IllegalArgumentException("Not enough room to put a short at"
983           + " offset " + offset + " in a " + bytes.length + " byte array");
984     }
985     if (UnsafeComparer.isAvailable()) {
986       return putShortUnsafe(bytes, offset, val);
987     } else {
988       bytes[offset+1] = (byte) val;
989       val >>= 8;
990       bytes[offset] = (byte) val;
991       return offset + SIZEOF_SHORT;
992     }
993   }
994 
995   /**
996    * Put a short value out to the specified byte array position (Unsafe).
997    * @param bytes the byte array
998    * @param offset position in the array
999    * @param val short to write out
1000    * @return incremented offset
1001    */
1002   public static int putShortUnsafe(byte[] bytes, int offset, short val)
1003   {
1004     if (UnsafeComparer.littleEndian) {
1005       val = Short.reverseBytes(val);
1006     }
1007     UnsafeComparer.theUnsafe.putShort(bytes, (long) offset +
1008       UnsafeComparer.BYTE_ARRAY_BASE_OFFSET , val);
1009     return offset + SIZEOF_SHORT;
1010   }
1011 
1012   /**
1013    * Put an int value as short out to the specified byte array position. Only the lower 2 bytes of
1014    * the short will be put into the array. The caller of the API need to make sure they will not
1015    * loose the value by doing so. This is useful to store an unsigned short which is represented as
1016    * int in other parts.
1017    * @param bytes the byte array
1018    * @param offset position in the array
1019    * @param val value to write out
1020    * @return incremented offset
1021    * @throws IllegalArgumentException if the byte array given doesn't have
1022    * enough room at the offset specified.
1023    */
1024   public static int putAsShort(byte[] bytes, int offset, int val) {
1025     if (bytes.length - offset < SIZEOF_SHORT) {
1026       throw new IllegalArgumentException("Not enough room to put a short at"
1027           + " offset " + offset + " in a " + bytes.length + " byte array");
1028     }
1029     bytes[offset+1] = (byte) val;
1030     val >>= 8;
1031     bytes[offset] = (byte) val;
1032     return offset + SIZEOF_SHORT;
1033   }
1034 
1035   /**
1036    * Convert a BigDecimal value to a byte array
1037    *
1038    * @param val
1039    * @return the byte array
1040    */
1041   public static byte[] toBytes(BigDecimal val) {
1042     byte[] valueBytes = val.unscaledValue().toByteArray();
1043     byte[] result = new byte[valueBytes.length + SIZEOF_INT];
1044     int offset = putInt(result, 0, val.scale());
1045     putBytes(result, offset, valueBytes, 0, valueBytes.length);
1046     return result;
1047   }
1048 
1049 
1050   /**
1051    * Converts a byte array to a BigDecimal
1052    *
1053    * @param bytes
1054    * @return the char value
1055    */
1056   public static BigDecimal toBigDecimal(byte[] bytes) {
1057     return toBigDecimal(bytes, 0, bytes.length);
1058   }
1059 
1060   /**
1061    * Converts a byte array to a BigDecimal value
1062    *
1063    * @param bytes
1064    * @param offset
1065    * @param length
1066    * @return the char value
1067    */
1068   public static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length) {
1069     if (bytes == null || length < SIZEOF_INT + 1 ||
1070       (offset + length > bytes.length)) {
1071       return null;
1072     }
1073 
1074     int scale = toInt(bytes, offset);
1075     byte[] tcBytes = new byte[length - SIZEOF_INT];
1076     System.arraycopy(bytes, offset + SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
1077     return new BigDecimal(new BigInteger(tcBytes), scale);
1078   }
1079 
1080   /**
1081    * Put a BigDecimal value out to the specified byte array position.
1082    *
1083    * @param bytes  the byte array
1084    * @param offset position in the array
1085    * @param val    BigDecimal to write out
1086    * @return incremented offset
1087    */
1088   public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) {
1089     if (bytes == null) {
1090       return offset;
1091     }
1092 
1093     byte[] valueBytes = val.unscaledValue().toByteArray();
1094     byte[] result = new byte[valueBytes.length + SIZEOF_INT];
1095     offset = putInt(result, offset, val.scale());
1096     return putBytes(result, offset, valueBytes, 0, valueBytes.length);
1097   }
1098 
1099   /**
1100    * @param vint Integer to make a vint of.
1101    * @return Vint as bytes array.
1102    */
1103   public static byte [] vintToBytes(final long vint) {
1104     long i = vint;
1105     int size = WritableUtils.getVIntSize(i);
1106     byte [] result = new byte[size];
1107     int offset = 0;
1108     if (i >= -112 && i <= 127) {
1109       result[offset] = (byte) i;
1110       return result;
1111     }
1112 
1113     int len = -112;
1114     if (i < 0) {
1115       i ^= -1L; // take one's complement'
1116       len = -120;
1117     }
1118 
1119     long tmp = i;
1120     while (tmp != 0) {
1121       tmp = tmp >> 8;
1122       len--;
1123     }
1124 
1125     result[offset++] = (byte) len;
1126 
1127     len = (len < -120) ? -(len + 120) : -(len + 112);
1128 
1129     for (int idx = len; idx != 0; idx--) {
1130       int shiftbits = (idx - 1) * 8;
1131       long mask = 0xFFL << shiftbits;
1132       result[offset++] = (byte)((i & mask) >> shiftbits);
1133     }
1134     return result;
1135   }
1136 
1137   /**
1138    * @param buffer buffer to convert
1139    * @return vint bytes as an integer.
1140    */
1141   public static long bytesToVint(final byte [] buffer) {
1142     int offset = 0;
1143     byte firstByte = buffer[offset++];
1144     int len = WritableUtils.decodeVIntSize(firstByte);
1145     if (len == 1) {
1146       return firstByte;
1147     }
1148     long i = 0;
1149     for (int idx = 0; idx < len-1; idx++) {
1150       byte b = buffer[offset++];
1151       i = i << 8;
1152       i = i | (b & 0xFF);
1153     }
1154     return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
1155   }
1156 
1157   /**
1158    * Reads a zero-compressed encoded long from input buffer and returns it.
1159    * @param buffer Binary array
1160    * @param offset Offset into array at which vint begins.
1161    * @throws java.io.IOException e
1162    * @return deserialized long from buffer.
1163    * @deprecated Use {@link #readAsVLong()} instead.
1164    */
1165   @Deprecated
1166   public static long readVLong(final byte [] buffer, final int offset)
1167   throws IOException {
1168     return readAsVLong(buffer, offset);
1169   }
1170 
1171   /**
1172    * Reads a zero-compressed encoded long from input buffer and returns it.
1173    * @param buffer Binary array
1174    * @param offset Offset into array at which vint begins.
1175    * @return deserialized long from buffer.
1176    */
1177   public static long readAsVLong(final byte [] buffer, final int offset) {
1178     byte firstByte = buffer[offset];
1179     int len = WritableUtils.decodeVIntSize(firstByte);
1180     if (len == 1) {
1181       return firstByte;
1182     }
1183     long i = 0;
1184     for (int idx = 0; idx < len-1; idx++) {
1185       byte b = buffer[offset + 1 + idx];
1186       i = i << 8;
1187       i = i | (b & 0xFF);
1188     }
1189     return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
1190   }
1191 
1192   /**
1193    * @param left left operand
1194    * @param right right operand
1195    * @return 0 if equal, < 0 if left is less than right, etc.
1196    */
1197   public static int compareTo(final byte [] left, final byte [] right) {
1198     return LexicographicalComparerHolder.BEST_COMPARER.
1199       compareTo(left, 0, left.length, right, 0, right.length);
1200   }
1201 
1202   /**
1203    * Lexicographically compare two arrays.
1204    *
1205    * @param buffer1 left operand
1206    * @param buffer2 right operand
1207    * @param offset1 Where to start comparing in the left buffer
1208    * @param offset2 Where to start comparing in the right buffer
1209    * @param length1 How much to compare from the left buffer
1210    * @param length2 How much to compare from the right buffer
1211    * @return 0 if equal, < 0 if left is less than right, etc.
1212    */
1213   public static int compareTo(byte[] buffer1, int offset1, int length1,
1214       byte[] buffer2, int offset2, int length2) {
1215     return LexicographicalComparerHolder.BEST_COMPARER.
1216       compareTo(buffer1, offset1, length1, buffer2, offset2, length2);
1217   }
1218 
1219   interface Comparer<T> {
1220     int compareTo(
1221       T buffer1, int offset1, int length1, T buffer2, int offset2, int length2
1222     );
1223   }
1224 
1225   @VisibleForTesting
1226   static Comparer<byte[]> lexicographicalComparerJavaImpl() {
1227     return LexicographicalComparerHolder.PureJavaComparer.INSTANCE;
1228   }
1229 
1230   /**
1231    * Provides a lexicographical comparer implementation; either a Java
1232    * implementation or a faster implementation based on {@link Unsafe}.
1233    *
1234    * <p>Uses reflection to gracefully fall back to the Java implementation if
1235    * {@code Unsafe} isn't available.
1236    */
1237   @VisibleForTesting
1238   static class LexicographicalComparerHolder {
1239     static final String UNSAFE_COMPARER_NAME =
1240         LexicographicalComparerHolder.class.getName() + "$UnsafeComparer";
1241 
1242     static final Comparer<byte[]> BEST_COMPARER = getBestComparer();
1243     /**
1244      * Returns the Unsafe-using Comparer, or falls back to the pure-Java
1245      * implementation if unable to do so.
1246      */
1247     static Comparer<byte[]> getBestComparer() {
1248       try {
1249         Class<?> theClass = Class.forName(UNSAFE_COMPARER_NAME);
1250 
1251         // yes, UnsafeComparer does implement Comparer<byte[]>
1252         @SuppressWarnings("unchecked")
1253         Comparer<byte[]> comparer =
1254           (Comparer<byte[]>) theClass.getEnumConstants()[0];
1255         return comparer;
1256       } catch (Throwable t) { // ensure we really catch *everything*
1257         return lexicographicalComparerJavaImpl();
1258       }
1259     }
1260 
1261     enum PureJavaComparer implements Comparer<byte[]> {
1262       INSTANCE;
1263 
1264       @Override
1265       public int compareTo(byte[] buffer1, int offset1, int length1,
1266           byte[] buffer2, int offset2, int length2) {
1267         // Short circuit equal case
1268         if (buffer1 == buffer2 &&
1269             offset1 == offset2 &&
1270             length1 == length2) {
1271           return 0;
1272         }
1273         // Bring WritableComparator code local
1274         int end1 = offset1 + length1;
1275         int end2 = offset2 + length2;
1276         for (int i = offset1, j = offset2; i < end1 && j < end2; i++, j++) {
1277           int a = (buffer1[i] & 0xff);
1278           int b = (buffer2[j] & 0xff);
1279           if (a != b) {
1280             return a - b;
1281           }
1282         }
1283         return length1 - length2;
1284       }
1285     }
1286 
1287     @VisibleForTesting
1288     enum UnsafeComparer implements Comparer<byte[]> {
1289       INSTANCE;
1290 
1291       static final Unsafe theUnsafe;
1292 
1293       /** The offset to the first element in a byte array. */
1294       static final int BYTE_ARRAY_BASE_OFFSET;
1295 
1296       static {
1297         theUnsafe = (Unsafe) AccessController.doPrivileged(
1298             new PrivilegedAction<Object>() {
1299               @Override
1300               public Object run() {
1301                 try {
1302                   Field f = Unsafe.class.getDeclaredField("theUnsafe");
1303                   f.setAccessible(true);
1304                   return f.get(null);
1305                 } catch (NoSuchFieldException e) {
1306                   // It doesn't matter what we throw;
1307                   // it's swallowed in getBestComparer().
1308                   throw new Error();
1309                 } catch (IllegalAccessException e) {
1310                   throw new Error();
1311                 }
1312               }
1313             });
1314 
1315         BYTE_ARRAY_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
1316 
1317         // sanity check - this should never fail
1318         if (theUnsafe.arrayIndexScale(byte[].class) != 1) {
1319           throw new AssertionError();
1320         }
1321       }
1322 
1323       static final boolean littleEndian =
1324         ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN);
1325 
1326       /**
1327        * Returns true if x1 is less than x2, when both values are treated as
1328        * unsigned long.
1329        * Both values are passed as is read by Unsafe. When platform is Little Endian, have to
1330        * convert to corresponding Big Endian value and then do compare. We do all writes in
1331        * Big Endian format.
1332        */
1333       static boolean lessThanUnsignedLong(long x1, long x2) {
1334         if (littleEndian) {
1335           x1 = Long.reverseBytes(x1);
1336           x2 = Long.reverseBytes(x2);
1337         }
1338         return (x1 + Long.MIN_VALUE) < (x2 + Long.MIN_VALUE);
1339       }
1340 
1341       /**
1342        * Returns true if x1 is less than x2, when both values are treated as
1343        * unsigned int.
1344        * Both values are passed as is read by Unsafe. When platform is Little Endian, have to
1345        * convert to corresponding Big Endian value and then do compare. We do all writes in
1346        * Big Endian format.
1347        */
1348       static boolean lessThanUnsignedInt(int x1, int x2) {
1349         if (littleEndian) {
1350           x1 = Integer.reverseBytes(x1);
1351           x2 = Integer.reverseBytes(x2);
1352         }
1353         return (x1 & 0xffffffffL) < (x2 & 0xffffffffL);
1354       }
1355 
1356       /**
1357        * Returns true if x1 is less than x2, when both values are treated as
1358        * unsigned short.
1359        * Both values are passed as is read by Unsafe. When platform is Little Endian, have to
1360        * convert to corresponding Big Endian value and then do compare. We do all writes in
1361        * Big Endian format.
1362        */
1363       static boolean lessThanUnsignedShort(short x1, short x2) {
1364         if (littleEndian) {
1365           x1 = Short.reverseBytes(x1);
1366           x2 = Short.reverseBytes(x2);
1367         }
1368         return (x1 & 0xffff) < (x2 & 0xffff);
1369       }
1370 
1371       /**
1372        * Checks if Unsafe is available
1373        * @return true, if available, false - otherwise
1374        */
1375       public static boolean isAvailable()
1376       {
1377         return theUnsafe != null;
1378       }
1379 
1380       /**
1381        * Lexicographically compare two arrays.
1382        *
1383        * @param buffer1 left operand
1384        * @param buffer2 right operand
1385        * @param offset1 Where to start comparing in the left buffer
1386        * @param offset2 Where to start comparing in the right buffer
1387        * @param length1 How much to compare from the left buffer
1388        * @param length2 How much to compare from the right buffer
1389        * @return 0 if equal, < 0 if left is less than right, etc.
1390        */
1391       @Override
1392       public int compareTo(byte[] buffer1, int offset1, int length1,
1393           byte[] buffer2, int offset2, int length2) {
1394 
1395         // Short circuit equal case
1396         if (buffer1 == buffer2 &&
1397             offset1 == offset2 &&
1398             length1 == length2) {
1399           return 0;
1400         }
1401         final int minLength = Math.min(length1, length2);
1402         final int minWords = minLength / SIZEOF_LONG;
1403         final long offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
1404         final long offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET;
1405 
1406         /*
1407          * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
1408          * time is no slower than comparing 4 bytes at a time even on 32-bit.
1409          * On the other hand, it is substantially faster on 64-bit.
1410          */
1411         // This is the end offset of long parts.
1412         int j = minWords << 3; // Same as minWords * SIZEOF_LONG
1413         for (int i = 0; i < j; i += SIZEOF_LONG) {
1414           long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
1415           long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
1416           long diff = lw ^ rw;
1417           if (diff != 0) {
1418               return lessThanUnsignedLong(lw, rw) ? -1 : 1;
1419           }
1420         }
1421         int offset = j;
1422 
1423         if (minLength - offset >= SIZEOF_INT) {
1424           int il = theUnsafe.getInt(buffer1, offset1Adj + offset);
1425           int ir = theUnsafe.getInt(buffer2, offset2Adj + offset);
1426           if (il != ir) {
1427             return lessThanUnsignedInt(il, ir) ? -1: 1;
1428           }
1429           offset += SIZEOF_INT;
1430         }
1431         if (minLength - offset >= SIZEOF_SHORT) {
1432           short sl = theUnsafe.getShort(buffer1, offset1Adj + offset);
1433           short sr = theUnsafe.getShort(buffer2, offset2Adj + offset);
1434           if (sl != sr) {
1435             return lessThanUnsignedShort(sl, sr) ? -1: 1;
1436           }
1437           offset += SIZEOF_SHORT;
1438         }
1439         if (minLength - offset == 1) {
1440           int a = (buffer1[(int)(offset1 + offset)] & 0xff);
1441           int b = (buffer2[(int)(offset2 + offset)] & 0xff);
1442           if (a != b) {
1443             return a - b;
1444           }
1445         }
1446         return length1 - length2;
1447       }
1448     }
1449   }
1450 
1451   /**
1452    * @param left left operand
1453    * @param right right operand
1454    * @return True if equal
1455    */
1456   public static boolean equals(final byte [] left, final byte [] right) {
1457     // Could use Arrays.equals?
1458     //noinspection SimplifiableConditionalExpression
1459     if (left == right) return true;
1460     if (left == null || right == null) return false;
1461     if (left.length != right.length) return false;
1462     if (left.length == 0) return true;
1463 
1464     // Since we're often comparing adjacent sorted data,
1465     // it's usual to have equal arrays except for the very last byte
1466     // so check that first
1467     if (left[left.length - 1] != right[right.length - 1]) return false;
1468 
1469     return compareTo(left, right) == 0;
1470   }
1471 
1472   public static boolean equals(final byte[] left, int leftOffset, int leftLen,
1473                                final byte[] right, int rightOffset, int rightLen) {
1474     // short circuit case
1475     if (left == right &&
1476         leftOffset == rightOffset &&
1477         leftLen == rightLen) {
1478       return true;
1479     }
1480     // different lengths fast check
1481     if (leftLen != rightLen) {
1482       return false;
1483     }
1484     if (leftLen == 0) {
1485       return true;
1486     }
1487 
1488     // Since we're often comparing adjacent sorted data,
1489     // it's usual to have equal arrays except for the very last byte
1490     // so check that first
1491     if (left[leftOffset + leftLen - 1] != right[rightOffset + rightLen - 1]) return false;
1492 
1493     return LexicographicalComparerHolder.BEST_COMPARER.
1494       compareTo(left, leftOffset, leftLen, right, rightOffset, rightLen) == 0;
1495   }
1496 
1497 
1498   /**
1499    * @param a left operand
1500    * @param buf right operand
1501    * @return True if equal
1502    */
1503   public static boolean equals(byte[] a, ByteBuffer buf) {
1504     if (a == null) return buf == null;
1505     if (buf == null) return false;
1506     if (a.length != buf.remaining()) return false;
1507 
1508     // Thou shalt not modify the original byte buffer in what should be read only operations.
1509     ByteBuffer b = buf.duplicate();
1510     for (byte anA : a) {
1511       if (anA != b.get()) {
1512         return false;
1513       }
1514     }
1515     return true;
1516   }
1517 
1518 
1519   /**
1520    * Return true if the byte array on the right is a prefix of the byte
1521    * array on the left.
1522    */
1523   public static boolean startsWith(byte[] bytes, byte[] prefix) {
1524     return bytes != null && prefix != null &&
1525       bytes.length >= prefix.length &&
1526       LexicographicalComparerHolder.BEST_COMPARER.
1527         compareTo(bytes, 0, prefix.length, prefix, 0, prefix.length) == 0;
1528   }
1529 
1530   /**
1531    * @param b bytes to hash
1532    * @return Runs {@link WritableComparator#hashBytes(byte[], int)} on the
1533    * passed in array.  This method is what {@link org.apache.hadoop.io.Text} and
1534    * {@link ImmutableBytesWritable} use calculating hash code.
1535    */
1536   public static int hashCode(final byte [] b) {
1537     return hashCode(b, b.length);
1538   }
1539 
1540   /**
1541    * @param b value
1542    * @param length length of the value
1543    * @return Runs {@link WritableComparator#hashBytes(byte[], int)} on the
1544    * passed in array.  This method is what {@link org.apache.hadoop.io.Text} and
1545    * {@link ImmutableBytesWritable} use calculating hash code.
1546    */
1547   public static int hashCode(final byte [] b, final int length) {
1548     return WritableComparator.hashBytes(b, length);
1549   }
1550 
1551   /**
1552    * @param b bytes to hash
1553    * @return A hash of <code>b</code> as an Integer that can be used as key in
1554    * Maps.
1555    */
1556   public static Integer mapKey(final byte [] b) {
1557     return hashCode(b);
1558   }
1559 
1560   /**
1561    * @param b bytes to hash
1562    * @param length length to hash
1563    * @return A hash of <code>b</code> as an Integer that can be used as key in
1564    * Maps.
1565    */
1566   public static Integer mapKey(final byte [] b, final int length) {
1567     return hashCode(b, length);
1568   }
1569 
1570   /**
1571    * @param a lower half
1572    * @param b upper half
1573    * @return New array that has a in lower half and b in upper half.
1574    */
1575   public static byte [] add(final byte [] a, final byte [] b) {
1576     return add(a, b, EMPTY_BYTE_ARRAY);
1577   }
1578 
1579   /**
1580    * @param a first third
1581    * @param b second third
1582    * @param c third third
1583    * @return New array made from a, b and c
1584    */
1585   public static byte [] add(final byte [] a, final byte [] b, final byte [] c) {
1586     byte [] result = new byte[a.length + b.length + c.length];
1587     System.arraycopy(a, 0, result, 0, a.length);
1588     System.arraycopy(b, 0, result, a.length, b.length);
1589     System.arraycopy(c, 0, result, a.length + b.length, c.length);
1590     return result;
1591   }
1592 
1593   /**
1594    * @param arrays all the arrays to concatenate together.
1595    * @return New array made from the concatenation of the given arrays.
1596    */
1597   public static byte [] add(final byte [][] arrays) {
1598     int length = 0;
1599     for (int i = 0; i < arrays.length; i++) {
1600       length += arrays[i].length;
1601     }
1602     byte [] result = new byte[length];
1603     int index = 0;
1604     for (int i = 0; i < arrays.length; i++) {
1605       System.arraycopy(arrays[i], 0, result, index, arrays[i].length);
1606       index += arrays[i].length;
1607     }
1608     return result;
1609   }
1610 
1611   /**
1612    * @param a array
1613    * @param length amount of bytes to grab
1614    * @return First <code>length</code> bytes from <code>a</code>
1615    */
1616   public static byte [] head(final byte [] a, final int length) {
1617     if (a.length < length) {
1618       return null;
1619     }
1620     byte [] result = new byte[length];
1621     System.arraycopy(a, 0, result, 0, length);
1622     return result;
1623   }
1624 
1625   /**
1626    * @param a array
1627    * @param length amount of bytes to snarf
1628    * @return Last <code>length</code> bytes from <code>a</code>
1629    */
1630   public static byte [] tail(final byte [] a, final int length) {
1631     if (a.length < length) {
1632       return null;
1633     }
1634     byte [] result = new byte[length];
1635     System.arraycopy(a, a.length - length, result, 0, length);
1636     return result;
1637   }
1638 
1639   /**
1640    * @param a array
1641    * @param length new array size
1642    * @return Value in <code>a</code> plus <code>length</code> prepended 0 bytes
1643    */
1644   public static byte [] padHead(final byte [] a, final int length) {
1645     byte [] padding = new byte[length];
1646     for (int i = 0; i < length; i++) {
1647       padding[i] = 0;
1648     }
1649     return add(padding,a);
1650   }
1651 
1652   /**
1653    * @param a array
1654    * @param length new array size
1655    * @return Value in <code>a</code> plus <code>length</code> appended 0 bytes
1656    */
1657   public static byte [] padTail(final byte [] a, final int length) {
1658     byte [] padding = new byte[length];
1659     for (int i = 0; i < length; i++) {
1660       padding[i] = 0;
1661     }
1662     return add(a,padding);
1663   }
1664 
1665   /**
1666    * Split passed range.  Expensive operation relatively.  Uses BigInteger math.
1667    * Useful splitting ranges for MapReduce jobs.
1668    * @param a Beginning of range
1669    * @param b End of range
1670    * @param num Number of times to split range.  Pass 1 if you want to split
1671    * the range in two; i.e. one split.
1672    * @return Array of dividing values
1673    */
1674   public static byte [][] split(final byte [] a, final byte [] b, final int num) {
1675     return split(a, b, false, num);
1676   }
1677 
1678   /**
1679    * Split passed range.  Expensive operation relatively.  Uses BigInteger math.
1680    * Useful splitting ranges for MapReduce jobs.
1681    * @param a Beginning of range
1682    * @param b End of range
1683    * @param inclusive Whether the end of range is prefix-inclusive or is
1684    * considered an exclusive boundary.  Automatic splits are generally exclusive
1685    * and manual splits with an explicit range utilize an inclusive end of range.
1686    * @param num Number of times to split range.  Pass 1 if you want to split
1687    * the range in two; i.e. one split.
1688    * @return Array of dividing values
1689    */
1690   public static byte[][] split(final byte[] a, final byte[] b,
1691       boolean inclusive, final int num) {
1692     byte[][] ret = new byte[num + 2][];
1693     int i = 0;
1694     Iterable<byte[]> iter = iterateOnSplits(a, b, inclusive, num);
1695     if (iter == null)
1696       return null;
1697     for (byte[] elem : iter) {
1698       ret[i++] = elem;
1699     }
1700     return ret;
1701   }
1702 
1703   /**
1704    * Iterate over keys within the passed range, splitting at an [a,b) boundary.
1705    */
1706   public static Iterable<byte[]> iterateOnSplits(final byte[] a,
1707       final byte[] b, final int num)
1708   {
1709     return iterateOnSplits(a, b, false, num);
1710   }
1711 
1712   /**
1713    * Iterate over keys within the passed range.
1714    */
1715   public static Iterable<byte[]> iterateOnSplits(
1716       final byte[] a, final byte[]b, boolean inclusive, final int num)
1717   {
1718     byte [] aPadded;
1719     byte [] bPadded;
1720     if (a.length < b.length) {
1721       aPadded = padTail(a, b.length - a.length);
1722       bPadded = b;
1723     } else if (b.length < a.length) {
1724       aPadded = a;
1725       bPadded = padTail(b, a.length - b.length);
1726     } else {
1727       aPadded = a;
1728       bPadded = b;
1729     }
1730     if (compareTo(aPadded,bPadded) >= 0) {
1731       throw new IllegalArgumentException("b <= a");
1732     }
1733     if (num <= 0) {
1734       throw new IllegalArgumentException("num cannot be <= 0");
1735     }
1736     byte [] prependHeader = {1, 0};
1737     final BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
1738     final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
1739     BigInteger diffBI = stopBI.subtract(startBI);
1740     if (inclusive) {
1741       diffBI = diffBI.add(BigInteger.ONE);
1742     }
1743     final BigInteger splitsBI = BigInteger.valueOf(num + 1);
1744     //when diffBI < splitBI, use an additional byte to increase diffBI
1745     if(diffBI.compareTo(splitsBI) < 0) {
1746       byte[] aPaddedAdditional = new byte[aPadded.length+1];
1747       byte[] bPaddedAdditional = new byte[bPadded.length+1];
1748       for (int i = 0; i < aPadded.length; i++){
1749         aPaddedAdditional[i] = aPadded[i];
1750       }
1751       for (int j = 0; j < bPadded.length; j++){
1752         bPaddedAdditional[j] = bPadded[j];
1753       }
1754       aPaddedAdditional[aPadded.length] = 0;
1755       bPaddedAdditional[bPadded.length] = 0;
1756       return iterateOnSplits(aPaddedAdditional, bPaddedAdditional, inclusive,  num);
1757     }
1758     final BigInteger intervalBI;
1759     try {
1760       intervalBI = diffBI.divide(splitsBI);
1761     } catch(Exception e) {
1762       LOG.error("Exception caught during division", e);
1763       return null;
1764     }
1765 
1766     final Iterator<byte[]> iterator = new Iterator<byte[]>() {
1767       private int i = -1;
1768 
1769       @Override
1770       public boolean hasNext() {
1771         return i < num+1;
1772       }
1773 
1774       @Override
1775       public byte[] next() {
1776         i++;
1777         if (i == 0) return a;
1778         if (i == num + 1) return b;
1779 
1780         BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
1781         byte [] padded = curBI.toByteArray();
1782         if (padded[1] == 0)
1783           padded = tail(padded, padded.length - 2);
1784         else
1785           padded = tail(padded, padded.length - 1);
1786         return padded;
1787       }
1788 
1789       @Override
1790       public void remove() {
1791         throw new UnsupportedOperationException();
1792       }
1793 
1794     };
1795 
1796     return new Iterable<byte[]>() {
1797       @Override
1798       public Iterator<byte[]> iterator() {
1799         return iterator;
1800       }
1801     };
1802   }
1803 
1804   /**
1805    * @param bytes array to hash
1806    * @param offset offset to start from
1807    * @param length length to hash
1808    * */
1809   public static int hashCode(byte[] bytes, int offset, int length) {
1810     int hash = 1;
1811     for (int i = offset; i < offset + length; i++)
1812       hash = (31 * hash) + (int) bytes[i];
1813     return hash;
1814   }
1815 
1816   /**
1817    * @param t operands
1818    * @return Array of byte arrays made from passed array of Text
1819    */
1820   public static byte [][] toByteArrays(final String [] t) {
1821     byte [][] result = new byte[t.length][];
1822     for (int i = 0; i < t.length; i++) {
1823       result[i] = Bytes.toBytes(t[i]);
1824     }
1825     return result;
1826   }
1827 
1828   /**
1829    * @param t operands
1830    * @return Array of binary byte arrays made from passed array of binary strings
1831    */
1832   public static byte[][] toBinaryByteArrays(final String[] t) {
1833     byte[][] result = new byte[t.length][];
1834     for (int i = 0; i < t.length; i++) {
1835       result[i] = Bytes.toBytesBinary(t[i]);
1836     }
1837     return result;
1838   }
1839 
1840   /**
1841    * @param column operand
1842    * @return A byte array of a byte array where first and only entry is
1843    * <code>column</code>
1844    */
1845   public static byte [][] toByteArrays(final String column) {
1846     return toByteArrays(toBytes(column));
1847   }
1848 
1849   /**
1850    * @param column operand
1851    * @return A byte array of a byte array where first and only entry is
1852    * <code>column</code>
1853    */
1854   public static byte [][] toByteArrays(final byte [] column) {
1855     byte [][] result = new byte[1][];
1856     result[0] = column;
1857     return result;
1858   }
1859 
1860   /**
1861    * Binary search for keys in indexes.
1862    *
1863    * @param arr array of byte arrays to search for
1864    * @param key the key you want to find
1865    * @param offset the offset in the key you want to find
1866    * @param length the length of the key
1867    * @param comparator a comparator to compare.
1868    * @return zero-based index of the key, if the key is present in the array.
1869    *         Otherwise, a value -(i + 1) such that the key is between arr[i -
1870    *         1] and arr[i] non-inclusively, where i is in [0, i], if we define
1871    *         arr[-1] = -Inf and arr[N] = Inf for an N-element array. The above
1872    *         means that this function can return 2N + 1 different values
1873    *         ranging from -(N + 1) to N - 1.
1874    */
1875   public static int binarySearch(byte [][]arr, byte []key, int offset,
1876       int length, RawComparator<?> comparator) {
1877     int low = 0;
1878     int high = arr.length - 1;
1879 
1880     while (low <= high) {
1881       int mid = (low+high) >>> 1;
1882       // we have to compare in this order, because the comparator order
1883       // has special logic when the 'left side' is a special key.
1884       int cmp = comparator.compare(key, offset, length,
1885           arr[mid], 0, arr[mid].length);
1886       // key lives above the midpoint
1887       if (cmp > 0)
1888         low = mid + 1;
1889       // key lives below the midpoint
1890       else if (cmp < 0)
1891         high = mid - 1;
1892       // BAM. how often does this really happen?
1893       else
1894         return mid;
1895     }
1896     return - (low+1);
1897   }
1898 
1899   /**
1900    * Bytewise binary increment/deincrement of long contained in byte array
1901    * on given amount.
1902    *
1903    * @param value - array of bytes containing long (length <= SIZEOF_LONG)
1904    * @param amount value will be incremented on (deincremented if negative)
1905    * @return array of bytes containing incremented long (length == SIZEOF_LONG)
1906    */
1907   public static byte [] incrementBytes(byte[] value, long amount)
1908   {
1909     byte[] val = value;
1910     if (val.length < SIZEOF_LONG) {
1911       // Hopefully this doesn't happen too often.
1912       byte [] newvalue;
1913       if (val[0] < 0) {
1914         newvalue = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1};
1915       } else {
1916         newvalue = new byte[SIZEOF_LONG];
1917       }
1918       System.arraycopy(val, 0, newvalue, newvalue.length - val.length,
1919         val.length);
1920       val = newvalue;
1921     } else if (val.length > SIZEOF_LONG) {
1922       throw new IllegalArgumentException("Increment Bytes - value too big: " +
1923         val.length);
1924     }
1925     if(amount == 0) return val;
1926     if(val[0] < 0){
1927       return binaryIncrementNeg(val, amount);
1928     }
1929     return binaryIncrementPos(val, amount);
1930   }
1931 
1932   /* increment/deincrement for positive value */
1933   private static byte [] binaryIncrementPos(byte [] value, long amount) {
1934     long amo = amount;
1935     int sign = 1;
1936     if (amount < 0) {
1937       amo = -amount;
1938       sign = -1;
1939     }
1940     for(int i=0;i<value.length;i++) {
1941       int cur = ((int)amo % 256) * sign;
1942       amo = (amo >> 8);
1943       int val = value[value.length-i-1] & 0x0ff;
1944       int total = val + cur;
1945       if(total > 255) {
1946         amo += sign;
1947         total %= 256;
1948       } else if (total < 0) {
1949         amo -= sign;
1950       }
1951       value[value.length-i-1] = (byte)total;
1952       if (amo == 0) return value;
1953     }
1954     return value;
1955   }
1956 
1957   /* increment/deincrement for negative value */
1958   private static byte [] binaryIncrementNeg(byte [] value, long amount) {
1959     long amo = amount;
1960     int sign = 1;
1961     if (amount < 0) {
1962       amo = -amount;
1963       sign = -1;
1964     }
1965     for(int i=0;i<value.length;i++) {
1966       int cur = ((int)amo % 256) * sign;
1967       amo = (amo >> 8);
1968       int val = ((~value[value.length-i-1]) & 0x0ff) + 1;
1969       int total = cur - val;
1970       if(total >= 0) {
1971         amo += sign;
1972       } else if (total < -256) {
1973         amo -= sign;
1974         total %= 256;
1975       }
1976       value[value.length-i-1] = (byte)total;
1977       if (amo == 0) return value;
1978     }
1979     return value;
1980   }
1981 
1982   /**
1983    * Writes a string as a fixed-size field, padded with zeros.
1984    */
1985   public static void writeStringFixedSize(final DataOutput out, String s,
1986       int size) throws IOException {
1987     byte[] b = toBytes(s);
1988     if (b.length > size) {
1989       throw new IOException("Trying to write " + b.length + " bytes (" +
1990           toStringBinary(b) + ") into a field of length " + size);
1991     }
1992 
1993     out.writeBytes(s);
1994     for (int i = 0; i < size - s.length(); ++i)
1995       out.writeByte(0);
1996   }
1997 
1998   /**
1999    * Reads a fixed-size field and interprets it as a string padded with zeros.
2000    */
2001   public static String readStringFixedSize(final DataInput in, int size)
2002       throws IOException {
2003     byte[] b = new byte[size];
2004     in.readFully(b);
2005     int n = b.length;
2006     while (n > 0 && b[n - 1] == 0)
2007       --n;
2008 
2009     return toString(b, 0, n);
2010   }
2011 
2012   /**
2013    * Copy the byte array given in parameter and return an instance
2014    * of a new byte array with the same length and the same content.
2015    * @param bytes the byte array to duplicate
2016    * @return a copy of the given byte array
2017    */
2018   public static byte [] copy(byte [] bytes) {
2019     if (bytes == null) return null;
2020     byte [] result = new byte[bytes.length];
2021     System.arraycopy(bytes, 0, result, 0, bytes.length);
2022     return result;
2023   }
2024 
2025   /**
2026    * Copy the byte array given in parameter and return an instance
2027    * of a new byte array with the same length and the same content.
2028    * @param bytes the byte array to copy from
2029    * @return a copy of the given designated byte array
2030    * @param offset
2031    * @param length
2032    */
2033   public static byte [] copy(byte [] bytes, final int offset, final int length) {
2034     if (bytes == null) return null;
2035     byte [] result = new byte[length];
2036     System.arraycopy(bytes, offset, result, 0, length);
2037     return result;
2038   }
2039 
2040   /**
2041    * Search sorted array "a" for byte "key". I can't remember if I wrote this or copied it from
2042    * somewhere. (mcorgan)
2043    * @param a Array to search. Entries must be sorted and unique.
2044    * @param fromIndex First index inclusive of "a" to include in the search.
2045    * @param toIndex Last index exclusive of "a" to include in the search.
2046    * @param key The byte to search for.
2047    * @return The index of key if found. If not found, return -(index + 1), where negative indicates
2048    *         "not found" and the "index + 1" handles the "-0" case.
2049    */
2050   public static int unsignedBinarySearch(byte[] a, int fromIndex, int toIndex, byte key) {
2051     int unsignedKey = key & 0xff;
2052     int low = fromIndex;
2053     int high = toIndex - 1;
2054 
2055     while (low <= high) {
2056       int mid = (low + high) >>> 1;
2057       int midVal = a[mid] & 0xff;
2058 
2059       if (midVal < unsignedKey) {
2060         low = mid + 1;
2061       } else if (midVal > unsignedKey) {
2062         high = mid - 1;
2063       } else {
2064         return mid; // key found
2065       }
2066     }
2067     return -(low + 1); // key not found.
2068   }
2069 
2070   /**
2071    * Treat the byte[] as an unsigned series of bytes, most significant bits first.  Start by adding
2072    * 1 to the rightmost bit/byte and carry over all overflows to the more significant bits/bytes.
2073    *
2074    * @param input The byte[] to increment.
2075    * @return The incremented copy of "in".  May be same length or 1 byte longer.
2076    */
2077   public static byte[] unsignedCopyAndIncrement(final byte[] input) {
2078     byte[] copy = copy(input);
2079     if (copy == null) {
2080       throw new IllegalArgumentException("cannot increment null array");
2081     }
2082     for (int i = copy.length - 1; i >= 0; --i) {
2083       if (copy[i] == -1) {// -1 is all 1-bits, which is the unsigned maximum
2084         copy[i] = 0;
2085       } else {
2086         ++copy[i];
2087         return copy;
2088       }
2089     }
2090     // we maxed out the array
2091     byte[] out = new byte[copy.length + 1];
2092     out[0] = 1;
2093     System.arraycopy(copy, 0, out, 1, copy.length);
2094     return out;
2095   }
2096 
2097   public static boolean equals(List<byte[]> a, List<byte[]> b) {
2098     if (a == null) {
2099       if (b == null) {
2100         return true;
2101       }
2102       return false;
2103     }
2104     if (b == null) {
2105       return false;
2106     }
2107     if (a.size() != b.size()) {
2108       return false;
2109     }
2110     for (int i = 0; i < a.size(); ++i) {
2111       if (!Bytes.equals(a.get(i), b.get(i))) {
2112         return false;
2113       }
2114     }
2115     return true;
2116   }
2117 
2118   public static boolean isSorted(Collection<byte[]> arrays) {
2119     byte[] previous = new byte[0];
2120     for (byte[] array : IterableUtils.nullSafe(arrays)) {
2121       if (Bytes.compareTo(previous, array) > 0) {
2122         return false;
2123       }
2124       previous = array;
2125     }
2126     return true;
2127   }
2128 
2129   public static List<byte[]> getUtf8ByteArrays(List<String> strings) {
2130     List<byte[]> byteArrays = Lists.newArrayListWithCapacity(CollectionUtils.nullSafeSize(strings));
2131     for (String s : IterableUtils.nullSafe(strings)) {
2132       byteArrays.add(Bytes.toBytes(s));
2133     }
2134     return byteArrays;
2135   }
2136 
2137   /**
2138    * Returns the index of the first appearance of the value {@code target} in
2139    * {@code array}.
2140    *
2141    * @param array an array of {@code byte} values, possibly empty
2142    * @param target a primitive {@code byte} value
2143    * @return the least index {@code i} for which {@code array[i] == target}, or
2144    *     {@code -1} if no such index exists.
2145    */
2146   public static int indexOf(byte[] array, byte target) {
2147     for (int i = 0; i < array.length; i++) {
2148       if (array[i] == target) {
2149         return i;
2150       }
2151     }
2152     return -1;
2153   }
2154 
2155   /**
2156    * Returns the start position of the first occurrence of the specified {@code
2157    * target} within {@code array}, or {@code -1} if there is no such occurrence.
2158    *
2159    * <p>More formally, returns the lowest index {@code i} such that {@code
2160    * java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
2161    * the same elements as {@code target}.
2162    *
2163    * @param array the array to search for the sequence {@code target}
2164    * @param target the array to search for as a sub-sequence of {@code array}
2165    */
2166   public static int indexOf(byte[] array, byte[] target) {
2167     checkNotNull(array, "array");
2168     checkNotNull(target, "target");
2169     if (target.length == 0) {
2170       return 0;
2171     }
2172 
2173     outer:
2174     for (int i = 0; i < array.length - target.length + 1; i++) {
2175       for (int j = 0; j < target.length; j++) {
2176         if (array[i + j] != target[j]) {
2177           continue outer;
2178         }
2179       }
2180       return i;
2181     }
2182     return -1;
2183   }
2184 
2185   /**
2186    * @param array an array of {@code byte} values, possibly empty
2187    * @param target a primitive {@code byte} value
2188    * @return {@code true} if {@code target} is present as an element anywhere in {@code array}.
2189    */
2190   public static boolean contains(byte[] array, byte target) {
2191     return indexOf(array, target) > -1;
2192   }
2193 
2194   /**
2195    * @param array an array of {@code byte} values, possibly empty
2196    * @param target an array of {@code byte}
2197    * @return {@code true} if {@code target} is present anywhere in {@code array}
2198    */
2199   public static boolean contains(byte[] array, byte[] target) {
2200     return indexOf(array, target) > -1;
2201   }
2202 
2203   /**
2204    * Fill given array with zeros.
2205    * @param b array which needs to be filled with zeros
2206    */
2207   public static void zero(byte[] b) {
2208     zero(b, 0, b.length);
2209   }
2210 
2211   /**
2212    * Fill given array with zeros at the specified position.
2213    * @param b
2214    * @param offset
2215    * @param length
2216    */
2217   public static void zero(byte[] b, int offset, int length) {
2218     checkPositionIndex(offset, b.length, "offset");
2219     checkArgument(length > 0, "length must be greater than 0");
2220     checkPositionIndex(offset + length, b.length, "offset + length");
2221     Arrays.fill(b, offset, offset + length, (byte) 0);
2222   }
2223 
2224   private static final SecureRandom RNG = new SecureRandom();
2225 
2226   /**
2227    * Fill given array with random bytes.
2228    * @param b array which needs to be filled with random bytes
2229    */
2230   public static void random(byte[] b) {
2231     RNG.nextBytes(b);
2232   }
2233 
2234   /**
2235    * Fill given array with random bytes at the specified position.
2236    * @param b
2237    * @param offset
2238    * @param length
2239    */
2240   public static void random(byte[] b, int offset, int length) {
2241     checkPositionIndex(offset, b.length, "offset");
2242     checkArgument(length > 0, "length must be greater than 0");
2243     checkPositionIndex(offset + length, b.length, "offset + length");
2244     byte[] buf = new byte[length];
2245     RNG.nextBytes(buf);
2246     System.arraycopy(buf, 0, b, offset, length);
2247   }
2248 
2249   /**
2250    * Create a max byte array with the specified max byte count
2251    * @param maxByteCount the length of returned byte array
2252    * @return the created max byte array
2253    */
2254   public static byte[] createMaxByteArray(int maxByteCount) {
2255     byte[] maxByteArray = new byte[maxByteCount];
2256     for (int i = 0; i < maxByteArray.length; i++) {
2257       maxByteArray[i] = (byte) 0xff;
2258     }
2259     return maxByteArray;
2260   }
2261 
2262   /**
2263    * Create a byte array which is multiple given bytes
2264    * @param srcBytes
2265    * @param multiNum
2266    * @return byte array
2267    */
2268   public static byte[] multiple(byte[] srcBytes, int multiNum) {
2269     if (multiNum <= 0) {
2270       return new byte[0];
2271     }
2272     byte[] result = new byte[srcBytes.length * multiNum];
2273     for (int i = 0; i < multiNum; i++) {
2274       System.arraycopy(srcBytes, 0, result, i * srcBytes.length,
2275         srcBytes.length);
2276     }
2277     return result;
2278   }
2279   
2280   /**
2281    * Convert a byte array into a hex string
2282    * @param b
2283    */
2284   public static String toHex(byte[] b) {
2285     checkArgument(b.length > 0, "length must be greater than 0");
2286     return String.format("%x", new BigInteger(1, b));
2287   }
2288 
2289   /**
2290    * Create a byte array from a string of hash digits. The length of the
2291    * string must be a multiple of 2
2292    * @param hex
2293    */
2294   public static byte[] fromHex(String hex) {
2295     checkArgument(hex.length() > 0, "length must be greater than 0");
2296     checkArgument(hex.length() % 2 == 0, "length must be a multiple of 2");
2297     // Make sure letters are upper case
2298     hex = hex.toUpperCase();
2299     byte[] b = new byte[hex.length() / 2];
2300     for (int i = 0; i < b.length; i++) {
2301       b[i] = (byte)((toBinaryFromHex((byte)hex.charAt(2 * i)) << 4) +
2302         toBinaryFromHex((byte)hex.charAt((2 * i + 1))));
2303     }
2304     return b;
2305   }
2306 
2307 }