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