View Javadoc

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