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