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