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