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