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