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