View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.util;
21  
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.io.RawComparator;
27  import org.apache.hadoop.io.WritableComparator;
28  import org.apache.hadoop.io.WritableUtils;
29  
30  import java.io.DataInput;
31  import java.io.DataOutput;
32  import java.io.IOException;
33  import java.io.UnsupportedEncodingException;
34  import java.math.BigInteger;
35  import java.nio.ByteBuffer;
36  import java.util.Comparator;
37  import java.util.Iterator;
38  
39  /**
40   * Utility class that handles byte arrays, conversions to/from other types,
41   * comparisons, hash code generation, manufacturing keys for HashMaps or
42   * HashSets, etc.
43   */
44  public class Bytes {
45  
46    private static final Log LOG = LogFactory.getLog(Bytes.class);
47  
48    /**
49     * Size of boolean in bytes
50     */
51    public static final int SIZEOF_BOOLEAN = Byte.SIZE / Byte.SIZE;
52  
53    /**
54     * Size of byte in bytes
55     */
56    public static final int SIZEOF_BYTE = SIZEOF_BOOLEAN;
57  
58    /**
59     * Size of char in bytes
60     */
61    public static final int SIZEOF_CHAR = Character.SIZE / Byte.SIZE;
62  
63    /**
64     * Size of double in bytes
65     */
66    public static final int SIZEOF_DOUBLE = Double.SIZE / Byte.SIZE;
67  
68    /**
69     * Size of float in bytes
70     */
71    public static final int SIZEOF_FLOAT = Float.SIZE / Byte.SIZE;
72  
73    /**
74     * Size of int in bytes
75     */
76    public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE;
77  
78    /**
79     * Size of long in bytes
80     */
81    public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE;
82  
83    /**
84     * Size of short in bytes
85     */
86    public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE;
87  
88  
89    /**
90     * Estimate of size cost to pay beyond payload in jvm for instance of byte [].
91     * Estimate based on study of jhat and jprofiler numbers.
92     */
93    // JHat says BU is 56 bytes.
94    // SizeOf which uses java.lang.instrument says 24 bytes. (3 longs?)
95    public static final int ESTIMATED_HEAP_TAX = 16;
96  
97    /**
98     * Byte array comparator class.
99     */
100   public static class ByteArrayComparator implements RawComparator<byte []> {
101     /**
102      * Constructor
103      */
104     public ByteArrayComparator() {
105       super();
106     }
107     public int compare(byte [] left, byte [] right) {
108       return compareTo(left, right);
109     }
110     public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) {
111       return compareTo(b1, s1, l1, b2, s2, l2);
112     }
113   }
114 
115   /**
116    * Pass this to TreeMaps where byte [] are keys.
117    */
118   public static Comparator<byte []> BYTES_COMPARATOR =
119     new ByteArrayComparator();
120 
121   /**
122    * Use comparing byte arrays, byte-by-byte
123    */
124   public static RawComparator<byte []> BYTES_RAWCOMPARATOR =
125     new ByteArrayComparator();
126 
127   /**
128    * Read byte-array written with a WritableableUtils.vint prefix.
129    * @param in Input to read from.
130    * @return byte array read off <code>in</code>
131    * @throws IOException e
132    */
133   public static byte [] readByteArray(final DataInput in)
134   throws IOException {
135     int len = WritableUtils.readVInt(in);
136     if (len < 0) {
137       throw new NegativeArraySizeException(Integer.toString(len));
138     }
139     byte [] result = new byte[len];
140     in.readFully(result, 0, len);
141     return result;
142   }
143 
144   /**
145    * Read byte-array written with a WritableableUtils.vint prefix.
146    * IOException is converted to a RuntimeException.
147    * @param in Input to read from.
148    * @return byte array read off <code>in</code>
149    */
150   public static byte [] readByteArrayThrowsRuntime(final DataInput in) {
151     try {
152       return readByteArray(in);
153     } catch (Exception e) {
154       throw new RuntimeException(e);
155     }
156   }
157 
158   /**
159    * Write byte-array with a WritableableUtils.vint prefix.
160    * @param out output stream to be written to
161    * @param b array to write
162    * @throws IOException e
163    */
164   public static void writeByteArray(final DataOutput out, final byte [] b)
165   throws IOException {
166     if(b == null) {
167       WritableUtils.writeVInt(out, 0);
168     } else {
169       writeByteArray(out, b, 0, b.length);
170     }
171   }
172 
173   /**
174    * Write byte-array to out with a vint length prefix.
175    * @param out output stream
176    * @param b array
177    * @param offset offset into array
178    * @param length length past offset
179    * @throws IOException e
180    */
181   public static void writeByteArray(final DataOutput out, final byte [] b,
182       final int offset, final int length)
183   throws IOException {
184     WritableUtils.writeVInt(out, length);
185     out.write(b, offset, length);
186   }
187 
188   /**
189    * Write byte-array from src to tgt with a vint length prefix.
190    * @param tgt target array
191    * @param tgtOffset offset into target array
192    * @param src source array
193    * @param srcOffset source offset
194    * @param srcLength source length
195    * @return New offset in src array.
196    */
197   public static int writeByteArray(final byte [] tgt, final int tgtOffset,
198       final byte [] src, final int srcOffset, final int srcLength) {
199     byte [] vint = vintToBytes(srcLength);
200     System.arraycopy(vint, 0, tgt, tgtOffset, vint.length);
201     int offset = tgtOffset + vint.length;
202     System.arraycopy(src, srcOffset, tgt, offset, srcLength);
203     return offset + srcLength;
204   }
205 
206   /**
207    * Put bytes at the specified byte array position.
208    * @param tgtBytes the byte array
209    * @param tgtOffset position in the array
210    * @param srcBytes array to write out
211    * @param srcOffset source offset
212    * @param srcLength source length
213    * @return incremented offset
214    */
215   public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes,
216       int srcOffset, int srcLength) {
217     System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength);
218     return tgtOffset + srcLength;
219   }
220 
221   /**
222    * Write a single byte out to the specified byte array position.
223    * @param bytes the byte array
224    * @param offset position in the array
225    * @param b byte to write out
226    * @return incremented offset
227    */
228   public static int putByte(byte[] bytes, int offset, byte b) {
229     bytes[offset] = b;
230     return offset + 1;
231   }
232 
233   /**
234    * Returns a new byte array, copied from the passed ByteBuffer.
235    * @param bb A ByteBuffer
236    * @return the byte array
237    */
238   public static byte[] toBytes(ByteBuffer bb) {
239     int length = bb.limit();
240     byte [] result = new byte[length];
241     System.arraycopy(bb.array(), bb.arrayOffset(), result, 0, length);
242     return result;
243   }
244 
245   /**
246    * @param b Presumed UTF-8 encoded byte array.
247    * @return String made from <code>b</code>
248    */
249   public static String toString(final byte [] b) {
250     if (b == null) {
251       return null;
252     }
253     return toString(b, 0, b.length);
254   }
255 
256   /**
257    * Joins two byte arrays together using a separator.
258    * @param b1 The first byte array.
259    * @param sep The separator to use.
260    * @param b2 The second byte array.
261    */
262   public static String toString(final byte [] b1,
263                                 String sep,
264                                 final byte [] b2) {
265     return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length);
266   }
267 
268   /**
269    * This method will convert utf8 encoded bytes into a string. If
270    * an UnsupportedEncodingException occurs, this method will eat it
271    * and return null instead.
272    *
273    * @param b Presumed UTF-8 encoded byte array.
274    * @param off offset into array
275    * @param len length of utf-8 sequence
276    * @return String made from <code>b</code> or null
277    */
278   public static String toString(final byte [] b, int off, int len) {
279     if (b == null) {
280       return null;
281     }
282     if (len == 0) {
283       return "";
284     }
285     try {
286       return new String(b, off, len, HConstants.UTF8_ENCODING);
287     } catch (UnsupportedEncodingException e) {
288       LOG.error("UTF-8 not supported?", e);
289       return null;
290     }
291   }
292 
293   /**
294    * Write a printable representation of a byte array.
295    *
296    * @param b byte array
297    * @return string
298    * @see #toStringBinary(byte[], int, int)
299    */
300   public static String toStringBinary(final byte [] b) {
301     return toStringBinary(b, 0, b.length);
302   }
303 
304   /**
305    * Write a printable representation of a byte array. Non-printable
306    * characters are hex escaped in the format \\x%02X, eg:
307    * \x00 \x05 etc
308    *
309    * @param b array to write out
310    * @param off offset to start at
311    * @param len length to write
312    * @return string output
313    */
314   public static String toStringBinary(final byte [] b, int off, int len) {
315     StringBuilder result = new StringBuilder();
316     try {
317       String first = new String(b, off, len, "ISO-8859-1");
318       for (int i = 0; i < first.length() ; ++i ) {
319         int ch = first.charAt(i) & 0xFF;
320         if ( (ch >= '0' && ch <= '9')
321             || (ch >= 'A' && ch <= 'Z')
322             || (ch >= 'a' && ch <= 'z')
323             || " `~!@#$%^&*()-_=+[]{}\\|;:'\",.<>/?".indexOf(ch) >= 0 ) {
324           result.append(first.charAt(i));
325         } else {
326           result.append(String.format("\\x%02X", ch));
327         }
328       }
329     } catch (UnsupportedEncodingException e) {
330       LOG.error("ISO-8859-1 not supported?", e);
331     }
332     return result.toString();
333   }
334 
335   private static boolean isHexDigit(char c) {
336     return
337         (c >= 'A' && c <= 'F') ||
338         (c >= '0' && c <= '9');
339   }
340 
341   /**
342    * Takes a ASCII digit in the range A-F0-9 and returns
343    * the corresponding integer/ordinal value.
344    * @param ch  The hex digit.
345    * @return The converted hex value as a byte.
346    */
347   public static byte toBinaryFromHex(byte ch) {
348     if ( ch >= 'A' && ch <= 'F' )
349       return (byte) ((byte)10 + (byte) (ch - 'A'));
350     // else
351     return (byte) (ch - '0');
352   }
353 
354   public static byte [] toBytesBinary(String in) {
355     // this may be bigger than we need, but lets be safe.
356     byte [] b = new byte[in.length()];
357     int size = 0;
358     for (int i = 0; i < in.length(); ++i) {
359       char ch = in.charAt(i);
360       if (ch == '\\') {
361         // begin hex escape:
362         char next = in.charAt(i+1);
363         if (next != 'x') {
364           // invalid escape sequence, ignore this one.
365           b[size++] = (byte)ch;
366           continue;
367         }
368         // ok, take next 2 hex digits.
369         char hd1 = in.charAt(i+2);
370         char hd2 = in.charAt(i+3);
371 
372         // they need to be A-F0-9:
373         if (!isHexDigit(hd1) ||
374             !isHexDigit(hd2)) {
375           // bogus escape code, ignore:
376           continue;
377         }
378         // turn hex ASCII digit -> number
379         byte d = (byte) ((toBinaryFromHex((byte)hd1) << 4) + toBinaryFromHex((byte)hd2));
380 
381         b[size++] = d;
382         i += 3; // skip 3
383       } else {
384         b[size++] = (byte) ch;
385       }
386     }
387     // resize:
388     byte [] b2 = new byte[size];
389     System.arraycopy(b, 0, b2, 0, size);
390     return b2;
391   }
392 
393   /**
394    * Converts a string to a UTF-8 byte array.
395    * @param s string
396    * @return the byte array
397    */
398   public static byte[] toBytes(String s) {
399     try {
400       return s.getBytes(HConstants.UTF8_ENCODING);
401     } catch (UnsupportedEncodingException e) {
402       LOG.error("UTF-8 not supported?", e);
403       return null;
404     }
405   }
406 
407   /**
408    * Convert a boolean to a byte array. True becomes -1
409    * and false becomes 0.
410    *
411    * @param b value
412    * @return <code>b</code> encoded in a byte array.
413    */
414   public static byte [] toBytes(final boolean b) {
415     return new byte[] { b ? (byte) -1 : (byte) 0 };
416   }
417 
418   /**
419    * Reverses {@link #toBytes(boolean)}
420    * @param b array
421    * @return True or false.
422    */
423   public static boolean toBoolean(final byte [] b) {
424     if (b.length != 1) {
425       throw new IllegalArgumentException("Array has wrong size: " + b.length);
426     }
427     return b[0] != (byte) 0;
428   }
429 
430   /**
431    * Convert a long value to a byte array using big-endian.
432    *
433    * @param val value to convert
434    * @return the byte array
435    */
436   public static byte[] toBytes(long val) {
437     byte [] b = new byte[8];
438     for (int i = 7; i > 0; i--) {
439       b[i] = (byte) val;
440       val >>>= 8;
441     }
442     b[0] = (byte) val;
443     return b;
444   }
445 
446   /**
447    * Converts a byte array to a long value. Reverses
448    * {@link #toBytes(long)}
449    * @param bytes array
450    * @return the long value
451    */
452   public static long toLong(byte[] bytes) {
453     return toLong(bytes, 0, SIZEOF_LONG);
454   }
455 
456   /**
457    * Converts a byte array to a long value. Assumes there will be
458    * {@link #SIZEOF_LONG} bytes available.
459    *
460    * @param bytes bytes
461    * @param offset offset
462    * @return the long value
463    */
464   public static long toLong(byte[] bytes, int offset) {
465     return toLong(bytes, offset, SIZEOF_LONG);
466   }
467 
468   /**
469    * Converts a byte array to a long value.
470    *
471    * @param bytes array of bytes
472    * @param offset offset into array
473    * @param length length of data (must be {@link #SIZEOF_LONG})
474    * @return the long value
475    * @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or
476    * if there's not enough room in the array at the offset indicated.
477    */
478   public static long toLong(byte[] bytes, int offset, final int length) {
479     if (length != SIZEOF_LONG || offset + length > bytes.length) {
480       throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG);
481     }
482     long l = 0;
483     for(int i = offset; i < offset + length; i++) {
484       l <<= 8;
485       l ^= bytes[i] & 0xFF;
486     }
487     return l;
488   }
489 
490   private static IllegalArgumentException
491     explainWrongLengthOrOffset(final byte[] bytes,
492                                final int offset,
493                                final int length,
494                                final int expectedLength) {
495     String reason;
496     if (length != expectedLength) {
497       reason = "Wrong length: " + length + ", expected " + expectedLength;
498     } else {
499      reason = "offset (" + offset + ") + length (" + length + ") exceed the"
500         + " capacity of the array: " + bytes.length;
501     }
502     return new IllegalArgumentException(reason);
503   }
504 
505   /**
506    * Put a long value out to the specified byte array position.
507    * @param bytes the byte array
508    * @param offset position in the array
509    * @param val long to write out
510    * @return incremented offset
511    * @throws IllegalArgumentException if the byte array given doesn't have
512    * enough room at the offset specified.
513    */
514   public static int putLong(byte[] bytes, int offset, long val) {
515     if (bytes.length - offset < SIZEOF_LONG) {
516       throw new IllegalArgumentException("Not enough room to put a long at"
517           + " offset " + offset + " in a " + bytes.length + " byte array");
518     }
519     for(int i = offset + 7; i > offset; i--) {
520       bytes[i] = (byte) val;
521       val >>>= 8;
522     }
523     bytes[offset] = (byte) val;
524     return offset + SIZEOF_LONG;
525   }
526 
527   /**
528    * Presumes float encoded as IEEE 754 floating-point "single format"
529    * @param bytes byte array
530    * @return Float made from passed byte array.
531    */
532   public static float toFloat(byte [] bytes) {
533     return toFloat(bytes, 0);
534   }
535 
536   /**
537    * Presumes float encoded as IEEE 754 floating-point "single format"
538    * @param bytes array to convert
539    * @param offset offset into array
540    * @return Float made from passed byte array.
541    */
542   public static float toFloat(byte [] bytes, int offset) {
543     return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT));
544   }
545 
546   /**
547    * @param bytes byte array
548    * @param offset offset to write to
549    * @param f float value
550    * @return New offset in <code>bytes</code>
551    */
552   public static int putFloat(byte [] bytes, int offset, float f) {
553     return putInt(bytes, offset, Float.floatToRawIntBits(f));
554   }
555 
556   /**
557    * @param f float value
558    * @return the float represented as byte []
559    */
560   public static byte [] toBytes(final float f) {
561     // Encode it as int
562     return Bytes.toBytes(Float.floatToRawIntBits(f));
563   }
564 
565   /**
566    * @param bytes byte array
567    * @return Return double made from passed bytes.
568    */
569   public static double toDouble(final byte [] bytes) {
570     return toDouble(bytes, 0);
571   }
572 
573   /**
574    * @param bytes byte array
575    * @param offset offset where double is
576    * @return Return double made from passed bytes.
577    */
578   public static double toDouble(final byte [] bytes, final int offset) {
579     return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG));
580   }
581 
582   /**
583    * @param bytes byte array
584    * @param offset offset to write to
585    * @param d value
586    * @return New offset into array <code>bytes</code>
587    */
588   public static int putDouble(byte [] bytes, int offset, double d) {
589     return putLong(bytes, offset, Double.doubleToLongBits(d));
590   }
591 
592   /**
593    * Serialize a double as the IEEE 754 double format output. The resultant
594    * array will be 8 bytes long.
595    *
596    * @param d value
597    * @return the double represented as byte []
598    */
599   public static byte [] toBytes(final double d) {
600     // Encode it as a long
601     return Bytes.toBytes(Double.doubleToRawLongBits(d));
602   }
603 
604   /**
605    * Convert an int value to a byte array
606    * @param val value
607    * @return the byte array
608    */
609   public static byte[] toBytes(int val) {
610     byte [] b = new byte[4];
611     for(int i = 3; i > 0; i--) {
612       b[i] = (byte) val;
613       val >>>= 8;
614     }
615     b[0] = (byte) val;
616     return b;
617   }
618 
619   /**
620    * Converts a byte array to an int value
621    * @param bytes byte array
622    * @return the int value
623    */
624   public static int toInt(byte[] bytes) {
625     return toInt(bytes, 0, SIZEOF_INT);
626   }
627 
628   /**
629    * Converts a byte array to an int value
630    * @param bytes byte array
631    * @param offset offset into array
632    * @return the int value
633    */
634   public static int toInt(byte[] bytes, int offset) {
635     return toInt(bytes, offset, SIZEOF_INT);
636   }
637 
638   /**
639    * Converts a byte array to an int value
640    * @param bytes byte array
641    * @param offset offset into array
642    * @param length length of int (has to be {@link #SIZEOF_INT})
643    * @return the int value
644    * @throws IllegalArgumentException if length is not {@link #SIZEOF_INT} or
645    * if there's not enough room in the array at the offset indicated.
646    */
647   public static int toInt(byte[] bytes, int offset, final int length) {
648     if (length != SIZEOF_INT || offset + length > bytes.length) {
649       throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT);
650     }
651     int n = 0;
652     for(int i = offset; i < (offset + length); i++) {
653       n <<= 8;
654       n ^= bytes[i] & 0xFF;
655     }
656     return n;
657   }
658 
659   /**
660    * Put an int value out to the specified byte array position.
661    * @param bytes the byte array
662    * @param offset position in the array
663    * @param val int to write out
664    * @return incremented offset
665    * @throws IllegalArgumentException if the byte array given doesn't have
666    * enough room at the offset specified.
667    */
668   public static int putInt(byte[] bytes, int offset, int val) {
669     if (bytes.length - offset < SIZEOF_INT) {
670       throw new IllegalArgumentException("Not enough room to put an int at"
671           + " offset " + offset + " in a " + bytes.length + " byte array");
672     }
673     for(int i= offset + 3; i > offset; i--) {
674       bytes[i] = (byte) val;
675       val >>>= 8;
676     }
677     bytes[offset] = (byte) val;
678     return offset + SIZEOF_INT;
679   }
680 
681   /**
682    * Convert a short value to a byte array of {@link #SIZEOF_SHORT} bytes long.
683    * @param val value
684    * @return the byte array
685    */
686   public static byte[] toBytes(short val) {
687     byte[] b = new byte[SIZEOF_SHORT];
688     b[1] = (byte) val;
689     val >>= 8;
690     b[0] = (byte) val;
691     return b;
692   }
693 
694   /**
695    * Converts a byte array to a short value
696    * @param bytes byte array
697    * @return the short value
698    */
699   public static short toShort(byte[] bytes) {
700     return toShort(bytes, 0, SIZEOF_SHORT);
701   }
702 
703   /**
704    * Converts a byte array to a short value
705    * @param bytes byte array
706    * @param offset offset into array
707    * @return the short value
708    */
709   public static short toShort(byte[] bytes, int offset) {
710     return toShort(bytes, offset, SIZEOF_SHORT);
711   }
712 
713   /**
714    * Converts a byte array to a short value
715    * @param bytes byte array
716    * @param offset offset into array
717    * @param length length, has to be {@link #SIZEOF_SHORT}
718    * @return the short value
719    * @throws IllegalArgumentException if length is not {@link #SIZEOF_SHORT}
720    * or if there's not enough room in the array at the offset indicated.
721    */
722   public static short toShort(byte[] bytes, int offset, final int length) {
723     if (length != SIZEOF_SHORT || offset + length > bytes.length) {
724       throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT);
725     }
726     short n = 0;
727     n ^= bytes[offset] & 0xFF;
728     n <<= 8;
729     n ^= bytes[offset+1] & 0xFF;
730     return n;
731   }
732 
733   /**
734    * Put a short value out to the specified byte array position.
735    * @param bytes the byte array
736    * @param offset position in the array
737    * @param val short to write out
738    * @return incremented offset
739    * @throws IllegalArgumentException if the byte array given doesn't have
740    * enough room at the offset specified.
741    */
742   public static int putShort(byte[] bytes, int offset, short val) {
743     if (bytes.length - offset < SIZEOF_SHORT) {
744       throw new IllegalArgumentException("Not enough room to put a short at"
745           + " offset " + offset + " in a " + bytes.length + " byte array");
746     }
747     bytes[offset+1] = (byte) val;
748     val >>= 8;
749     bytes[offset] = (byte) val;
750     return offset + SIZEOF_SHORT;
751   }
752 
753   /**
754    * @param vint Integer to make a vint of.
755    * @return Vint as bytes array.
756    */
757   public static byte [] vintToBytes(final long vint) {
758     long i = vint;
759     int size = WritableUtils.getVIntSize(i);
760     byte [] result = new byte[size];
761     int offset = 0;
762     if (i >= -112 && i <= 127) {
763       result[offset] = (byte) i;
764       return result;
765     }
766 
767     int len = -112;
768     if (i < 0) {
769       i ^= -1L; // take one's complement'
770       len = -120;
771     }
772 
773     long tmp = i;
774     while (tmp != 0) {
775       tmp = tmp >> 8;
776       len--;
777     }
778 
779     result[offset++] = (byte) len;
780 
781     len = (len < -120) ? -(len + 120) : -(len + 112);
782 
783     for (int idx = len; idx != 0; idx--) {
784       int shiftbits = (idx - 1) * 8;
785       long mask = 0xFFL << shiftbits;
786       result[offset++] = (byte)((i & mask) >> shiftbits);
787     }
788     return result;
789   }
790 
791   /**
792    * @param buffer buffer to convert
793    * @return vint bytes as an integer.
794    */
795   public static long bytesToVint(final byte [] buffer) {
796     int offset = 0;
797     byte firstByte = buffer[offset++];
798     int len = WritableUtils.decodeVIntSize(firstByte);
799     if (len == 1) {
800       return firstByte;
801     }
802     long i = 0;
803     for (int idx = 0; idx < len-1; idx++) {
804       byte b = buffer[offset++];
805       i = i << 8;
806       i = i | (b & 0xFF);
807     }
808     return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
809   }
810 
811   /**
812    * Reads a zero-compressed encoded long from input stream and returns it.
813    * @param buffer Binary array
814    * @param offset Offset into array at which vint begins.
815    * @throws java.io.IOException e
816    * @return deserialized long from stream.
817    */
818   public static long readVLong(final byte [] buffer, final int offset)
819   throws IOException {
820     byte firstByte = buffer[offset];
821     int len = WritableUtils.decodeVIntSize(firstByte);
822     if (len == 1) {
823       return firstByte;
824     }
825     long i = 0;
826     for (int idx = 0; idx < len-1; idx++) {
827       byte b = buffer[offset + 1 + idx];
828       i = i << 8;
829       i = i | (b & 0xFF);
830     }
831     return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
832   }
833 
834   /**
835    * @param left left operand
836    * @param right right operand
837    * @return 0 if equal, < 0 if left is less than right, etc.
838    */
839   public static int compareTo(final byte [] left, final byte [] right) {
840     return compareTo(left, 0, left.length, right, 0, right.length);
841   }
842 
843   /**
844    * Lexographically compare two arrays.
845    *
846    * @param buffer1 left operand
847    * @param buffer2 right operand
848    * @param offset1 Where to start comparing in the left buffer
849    * @param offset2 Where to start comparing in the right buffer
850    * @param length1 How much to compare from the left buffer
851    * @param length2 How much to compare from the right buffer
852    * @return 0 if equal, < 0 if left is less than right, etc.
853    */
854   public static int compareTo(byte[] buffer1, int offset1, int length1,
855       byte[] buffer2, int offset2, int length2) {
856     // Bring WritableComparator code local
857     int end1 = offset1 + length1;
858     int end2 = offset2 + length2;
859     for (int i = offset1, j = offset2; i < end1 && j < end2; i++, j++) {
860       int a = (buffer1[i] & 0xff);
861       int b = (buffer2[j] & 0xff);
862       if (a != b) {
863         return a - b;
864       }
865     }
866     return length1 - length2;
867   }
868 
869   /**
870    * @param left left operand
871    * @param right right operand
872    * @return True if equal
873    */
874   public static boolean equals(final byte [] left, final byte [] right) {
875     // Could use Arrays.equals?
876     //noinspection SimplifiableConditionalExpression
877     if (left == null && right == null) {
878       return true;
879     }
880     return (left == null || right == null || (left.length != right.length)
881             ? false : compareTo(left, right) == 0);
882   }
883 
884   /**
885    * Return true if the byte array on the right is a prefix of the byte
886    * array on the left.
887    */
888   public static boolean startsWith(byte[] bytes, byte[] prefix) {
889     return bytes != null && prefix != null &&
890       bytes.length >= prefix.length &&
891       compareTo(bytes, 0, prefix.length, prefix, 0, prefix.length) == 0;      
892   }
893 
894   /**
895    * @param b bytes to hash
896    * @return Runs {@link WritableComparator#hashBytes(byte[], int)} on the
897    * passed in array.  This method is what {@link org.apache.hadoop.io.Text} and
898    * {@link ImmutableBytesWritable} use calculating hash code.
899    */
900   public static int hashCode(final byte [] b) {
901     return hashCode(b, b.length);
902   }
903 
904   /**
905    * @param b value
906    * @param length length of the value
907    * @return Runs {@link WritableComparator#hashBytes(byte[], int)} on the
908    * passed in array.  This method is what {@link org.apache.hadoop.io.Text} and
909    * {@link ImmutableBytesWritable} use calculating hash code.
910    */
911   public static int hashCode(final byte [] b, final int length) {
912     return WritableComparator.hashBytes(b, length);
913   }
914 
915   /**
916    * @param b bytes to hash
917    * @return A hash of <code>b</code> as an Integer that can be used as key in
918    * Maps.
919    */
920   public static Integer mapKey(final byte [] b) {
921     return hashCode(b);
922   }
923 
924   /**
925    * @param b bytes to hash
926    * @param length length to hash
927    * @return A hash of <code>b</code> as an Integer that can be used as key in
928    * Maps.
929    */
930   public static Integer mapKey(final byte [] b, final int length) {
931     return hashCode(b, length);
932   }
933 
934   /**
935    * @param a lower half
936    * @param b upper half
937    * @return New array that has a in lower half and b in upper half.
938    */
939   public static byte [] add(final byte [] a, final byte [] b) {
940     return add(a, b, HConstants.EMPTY_BYTE_ARRAY);
941   }
942 
943   /**
944    * @param a first third
945    * @param b second third
946    * @param c third third
947    * @return New array made from a, b and c
948    */
949   public static byte [] add(final byte [] a, final byte [] b, final byte [] c) {
950     byte [] result = new byte[a.length + b.length + c.length];
951     System.arraycopy(a, 0, result, 0, a.length);
952     System.arraycopy(b, 0, result, a.length, b.length);
953     System.arraycopy(c, 0, result, a.length + b.length, c.length);
954     return result;
955   }
956 
957   /**
958    * @param a array
959    * @param length amount of bytes to grab
960    * @return First <code>length</code> bytes from <code>a</code>
961    */
962   public static byte [] head(final byte [] a, final int length) {
963     if (a.length < length) {
964       return null;
965     }
966     byte [] result = new byte[length];
967     System.arraycopy(a, 0, result, 0, length);
968     return result;
969   }
970 
971   /**
972    * @param a array
973    * @param length amount of bytes to snarf
974    * @return Last <code>length</code> bytes from <code>a</code>
975    */
976   public static byte [] tail(final byte [] a, final int length) {
977     if (a.length < length) {
978       return null;
979     }
980     byte [] result = new byte[length];
981     System.arraycopy(a, a.length - length, result, 0, length);
982     return result;
983   }
984 
985   /**
986    * @param a array
987    * @param length new array size
988    * @return Value in <code>a</code> plus <code>length</code> prepended 0 bytes
989    */
990   public static byte [] padHead(final byte [] a, final int length) {
991     byte [] padding = new byte[length];
992     for (int i = 0; i < length; i++) {
993       padding[i] = 0;
994     }
995     return add(padding,a);
996   }
997 
998   /**
999    * @param a array
1000    * @param length new array size
1001    * @return Value in <code>a</code> plus <code>length</code> appended 0 bytes
1002    */
1003   public static byte [] padTail(final byte [] a, final int length) {
1004     byte [] padding = new byte[length];
1005     for (int i = 0; i < length; i++) {
1006       padding[i] = 0;
1007     }
1008     return add(a,padding);
1009   }
1010 
1011   /**
1012    * Split passed range.  Expensive operation relatively.  Uses BigInteger math.
1013    * Useful splitting ranges for MapReduce jobs.
1014    * @param a Beginning of range
1015    * @param b End of range
1016    * @param num Number of times to split range.  Pass 1 if you want to split
1017    * the range in two; i.e. one split.
1018    * @return Array of dividing values
1019    */
1020   public static byte [][] split(final byte [] a, final byte [] b, final int num) {
1021     byte[][] ret = new byte[num+2][];
1022     int i = 0;
1023     Iterable<byte[]> iter = iterateOnSplits(a, b, num);
1024     if (iter == null) return null;
1025     for (byte[] elem : iter) {
1026       ret[i++] = elem;
1027     }
1028     return ret;
1029   }
1030   
1031   /**
1032    * Iterate over keys within the passed inclusive range.
1033    */
1034   public static Iterable<byte[]> iterateOnSplits(
1035       final byte[] a, final byte[]b, final int num)
1036   {  
1037     byte [] aPadded;
1038     byte [] bPadded;
1039     if (a.length < b.length) {
1040       aPadded = padTail(a, b.length - a.length);
1041       bPadded = b;
1042     } else if (b.length < a.length) {
1043       aPadded = a;
1044       bPadded = padTail(b, a.length - b.length);
1045     } else {
1046       aPadded = a;
1047       bPadded = b;
1048     }
1049     if (compareTo(aPadded,bPadded) >= 0) {
1050       throw new IllegalArgumentException("b <= a");
1051     }
1052     if (num <= 0) {
1053       throw new IllegalArgumentException("num cannot be < 0");
1054     }
1055     byte [] prependHeader = {1, 0};
1056     final BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
1057     final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
1058     final BigInteger diffBI = stopBI.subtract(startBI);
1059     final BigInteger splitsBI = BigInteger.valueOf(num + 1);
1060     if(diffBI.compareTo(splitsBI) < 0) {
1061       return null;
1062     }
1063     final BigInteger intervalBI;
1064     try {
1065       intervalBI = diffBI.divide(splitsBI);
1066     } catch(Exception e) {
1067       LOG.error("Exception caught during division", e);
1068       return null;
1069     }
1070 
1071     final Iterator<byte[]> iterator = new Iterator<byte[]>() {
1072       private int i = -1;
1073       
1074       @Override
1075       public boolean hasNext() {
1076         return i < num+1;
1077       }
1078 
1079       @Override
1080       public byte[] next() {
1081         i++;
1082         if (i == 0) return a;
1083         if (i == num + 1) return b;
1084         
1085         BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
1086         byte [] padded = curBI.toByteArray();
1087         if (padded[1] == 0)
1088           padded = tail(padded, padded.length - 2);
1089         else
1090           padded = tail(padded, padded.length - 1);
1091         return padded;
1092       }
1093 
1094       @Override
1095       public void remove() {
1096         throw new UnsupportedOperationException();
1097       }
1098       
1099     };
1100     
1101     return new Iterable<byte[]>() {
1102       @Override
1103       public Iterator<byte[]> iterator() {
1104         return iterator;
1105       }
1106     };
1107   }
1108 
1109   /**
1110    * @param t operands
1111    * @return Array of byte arrays made from passed array of Text
1112    */
1113   public static byte [][] toByteArrays(final String [] t) {
1114     byte [][] result = new byte[t.length][];
1115     for (int i = 0; i < t.length; i++) {
1116       result[i] = Bytes.toBytes(t[i]);
1117     }
1118     return result;
1119   }
1120 
1121   /**
1122    * @param column operand
1123    * @return A byte array of a byte array where first and only entry is
1124    * <code>column</code>
1125    */
1126   public static byte [][] toByteArrays(final String column) {
1127     return toByteArrays(toBytes(column));
1128   }
1129 
1130   /**
1131    * @param column operand
1132    * @return A byte array of a byte array where first and only entry is
1133    * <code>column</code>
1134    */
1135   public static byte [][] toByteArrays(final byte [] column) {
1136     byte [][] result = new byte[1][];
1137     result[0] = column;
1138     return result;
1139   }
1140 
1141   /**
1142    * Binary search for keys in indexes.
1143    * @param arr array of byte arrays to search for
1144    * @param key the key you want to find
1145    * @param offset the offset in the key you want to find
1146    * @param length the length of the key
1147    * @param comparator a comparator to compare.
1148    * @return index of key
1149    */
1150   public static int binarySearch(byte [][]arr, byte []key, int offset,
1151       int length, RawComparator<byte []> comparator) {
1152     int low = 0;
1153     int high = arr.length - 1;
1154 
1155     while (low <= high) {
1156       int mid = (low+high) >>> 1;
1157       // we have to compare in this order, because the comparator order
1158       // has special logic when the 'left side' is a special key.
1159       int cmp = comparator.compare(key, offset, length,
1160           arr[mid], 0, arr[mid].length);
1161       // key lives above the midpoint
1162       if (cmp > 0)
1163         low = mid + 1;
1164       // key lives below the midpoint
1165       else if (cmp < 0)
1166         high = mid - 1;
1167       // BAM. how often does this really happen?
1168       else
1169         return mid;
1170     }
1171     return - (low+1);
1172   }
1173 
1174   /**
1175    * Bytewise binary increment/deincrement of long contained in byte array
1176    * on given amount.
1177    *
1178    * @param value - array of bytes containing long (length <= SIZEOF_LONG)
1179    * @param amount value will be incremented on (deincremented if negative)
1180    * @return array of bytes containing incremented long (length == SIZEOF_LONG)
1181    * @throws IOException - if value.length > SIZEOF_LONG
1182    */
1183   public static byte [] incrementBytes(byte[] value, long amount)
1184   throws IOException {
1185     byte[] val = value;
1186     if (val.length < SIZEOF_LONG) {
1187       // Hopefully this doesn't happen too often.
1188       byte [] newvalue;
1189       if (val[0] < 0) {
1190         newvalue = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1};
1191       } else {
1192         newvalue = new byte[SIZEOF_LONG];
1193       }
1194       System.arraycopy(val, 0, newvalue, newvalue.length - val.length,
1195         val.length);
1196       val = newvalue;
1197     } else if (val.length > SIZEOF_LONG) {
1198       throw new IllegalArgumentException("Increment Bytes - value too big: " +
1199         val.length);
1200     }
1201     if(amount == 0) return val;
1202     if(val[0] < 0){
1203       return binaryIncrementNeg(val, amount);
1204     }
1205     return binaryIncrementPos(val, amount);
1206   }
1207 
1208   /* increment/deincrement for positive value */
1209   private static byte [] binaryIncrementPos(byte [] value, long amount) {
1210     long amo = amount;
1211     int sign = 1;
1212     if (amount < 0) {
1213       amo = -amount;
1214       sign = -1;
1215     }
1216     for(int i=0;i<value.length;i++) {
1217       int cur = ((int)amo % 256) * sign;
1218       amo = (amo >> 8);
1219       int val = value[value.length-i-1] & 0x0ff;
1220       int total = val + cur;
1221       if(total > 255) {
1222         amo += sign;
1223         total %= 256;
1224       } else if (total < 0) {
1225         amo -= sign;
1226       }
1227       value[value.length-i-1] = (byte)total;
1228       if (amo == 0) return value;
1229     }
1230     return value;
1231   }
1232 
1233   /* increment/deincrement for negative value */
1234   private static byte [] binaryIncrementNeg(byte [] value, long amount) {
1235     long amo = amount;
1236     int sign = 1;
1237     if (amount < 0) {
1238       amo = -amount;
1239       sign = -1;
1240     }
1241     for(int i=0;i<value.length;i++) {
1242       int cur = ((int)amo % 256) * sign;
1243       amo = (amo >> 8);
1244       int val = ((~value[value.length-i-1]) & 0x0ff) + 1;
1245       int total = cur - val;
1246       if(total >= 0) {
1247         amo += sign;
1248       } else if (total < -256) {
1249         amo -= sign;
1250         total %= 256;
1251       }
1252       value[value.length-i-1] = (byte)total;
1253       if (amo == 0) return value;
1254     }
1255     return value;
1256   }
1257 
1258 }