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