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