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