Return-Path: X-Original-To: apmail-hbase-commits-archive@www.apache.org Delivered-To: apmail-hbase-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9C65B104F5 for ; Fri, 11 Mar 2016 16:59:37 +0000 (UTC) Received: (qmail 75618 invoked by uid 500); 11 Mar 2016 16:59:36 -0000 Delivered-To: apmail-hbase-commits-archive@hbase.apache.org Received: (qmail 75464 invoked by uid 500); 11 Mar 2016 16:59:36 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 74810 invoked by uid 99); 11 Mar 2016 16:59:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Mar 2016 16:59:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C57B9E0418; Fri, 11 Mar 2016 16:59:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: misty@apache.org To: commits@hbase.apache.org Date: Fri, 11 Mar 2016 16:59:42 -0000 Message-Id: <7254ed755a2c48ac914797c53dc56b53@git.apache.org> In-Reply-To: <732a255596bb4989af8ad4b197228002@git.apache.org> References: <732a255596bb4989af8ad4b197228002@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [08/51] [partial] hbase-site git commit: Published site at eea8b38dfa0180d3e6f93d3e8055d5d4fbf673c3. http://git-wip-us.apache.org/repos/asf/hbase-site/blob/0b785cb2/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.UnsafeComparer.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.UnsafeComparer.html b/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.UnsafeComparer.html index a529fdf..cf8fb3f 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.UnsafeComparer.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.UnsafeComparer.html @@ -140,2514 +140,2515 @@ 132 // SizeOf which uses java.lang.instrument says 24 bytes. (3 longs?) 133 public static final int ESTIMATED_HEAP_TAX = 16; 134 -135 -136 /** -137 * Returns length of the byte array, returning 0 if the array is null. -138 * Useful for calculating sizes. -139 * @param b byte array, which can be null -140 * @return 0 if b is null, otherwise returns length -141 */ -142 final public static int len(byte[] b) { -143 return b == null ? 0 : b.length; -144 } -145 -146 private byte[] bytes; -147 private int offset; -148 private int length; -149 -150 /** -151 * Create a zero-size sequence. -152 */ -153 public Bytes() { -154 super(); -155 } -156 -157 /** -158 * Create a Bytes using the byte array as the initial value. -159 * @param bytes This array becomes the backing storage for the object. -160 */ -161 public Bytes(byte[] bytes) { -162 this(bytes, 0, bytes.length); -163 } -164 -165 /** -166 * Set the new Bytes to the contents of the passed -167 * <code>ibw</code>. -168 * @param ibw the value to set this Bytes to. -169 */ -170 public Bytes(final Bytes ibw) { -171 this(ibw.get(), ibw.getOffset(), ibw.getLength()); -172 } -173 -174 /** -175 * Set the value to a given byte range -176 * @param bytes the new byte range to set to -177 * @param offset the offset in newData to start at -178 * @param length the number of bytes in the range -179 */ -180 public Bytes(final byte[] bytes, final int offset, -181 final int length) { -182 this.bytes = bytes; -183 this.offset = offset; -184 this.length = length; -185 } -186 -187 /** -188 * Copy bytes from ByteString instance. -189 * @param byteString copy from -190 */ -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 public ByteString toByteString() { -257 return ByteString.copyFrom(this.bytes, this.offset, this.length); -258 } -259 -260 @Override -261 public int hashCode() { -262 return Bytes.hashCode(bytes, offset, length); -263 } -264 -265 /** -266 * Define the sort order of the Bytes. -267 * @param that The other bytes writable -268 * @return Positive if left is bigger than right, 0 if they are equal, and -269 * negative if left is smaller than right. -270 */ -271 public int compareTo(Bytes that) { -272 return BYTES_RAWCOMPARATOR.compare( -273 this.bytes, this.offset, this.length, -274 that.bytes, that.offset, that.length); -275 } -276 -277 /** -278 * Compares the bytes in this object to the specified byte array -279 * @param that -280 * @return Positive if left is bigger than right, 0 if they are equal, and -281 * negative if left is smaller than right. -282 */ -283 public int compareTo(final byte [] that) { -284 return BYTES_RAWCOMPARATOR.compare( -285 this.bytes, this.offset, this.length, -286 that, 0, that.length); -287 } -288 -289 /** -290 * @see Object#equals(Object) -291 */ -292 @Override -293 public boolean equals(Object right_obj) { -294 if (right_obj instanceof byte []) { -295 return compareTo((byte [])right_obj) == 0; -296 } -297 if (right_obj instanceof Bytes) { -298 return compareTo((Bytes)right_obj) == 0; -299 } -300 return false; -301 } -302 -303 /** -304 * @see Object#toString() -305 */ -306 @Override -307 public String toString() { -308 return Bytes.toString(bytes, offset, length); -309 } -310 -311 /** -312 * @param array List of byte []. -313 * @return Array of byte []. -314 */ -315 public static byte [][] toArray(final List<byte []> array) { -316 // List#toArray doesn't work on lists of byte []. -317 byte[][] results = new byte[array.size()][]; -318 for (int i = 0; i < array.size(); i++) { -319 results[i] = array.get(i); -320 } -321 return results; -322 } -323 -324 /** -325 * Returns a copy of the bytes referred to by this writable -326 */ -327 public byte[] copyBytes() { -328 return Arrays.copyOfRange(bytes, offset, offset+length); -329 } -330 /** -331 * Byte array comparator class. -332 */ -333 @InterfaceAudience.Public -334 @InterfaceStability.Stable -335 public static class ByteArrayComparator implements RawComparator<byte []> { -336 /** -337 * Constructor -338 */ -339 public ByteArrayComparator() { -340 super(); -341 } -342 @Override -343 public int compare(byte [] left, byte [] right) { -344 return compareTo(left, right); -345 } -346 @Override -347 public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) { -348 return LexicographicalComparerHolder.BEST_COMPARER. -349 compareTo(b1, s1, l1, b2, s2, l2); -350 } -351 } -352 -353 /** -354 * A {@link ByteArrayComparator} that treats the empty array as the largest value. -355 * This is useful for comparing row end keys for regions. -356 */ -357 // TODO: unfortunately, HBase uses byte[0] as both start and end keys for region -358 // boundaries. Thus semantically, we should treat empty byte array as the smallest value -359 // while comparing row keys, start keys etc; but as the largest value for comparing -360 // region boundaries for endKeys. -361 @InterfaceAudience.Public -362 @InterfaceStability.Stable -363 public static class RowEndKeyComparator extends ByteArrayComparator { -364 @Override -365 public int compare(byte[] left, byte[] right) { -366 return compare(left, 0, left.length, right, 0, right.length); -367 } -368 @Override -369 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { -370 if (b1 == b2 && s1 == s2 && l1 == l2) { -371 return 0; -372 } -373 if (l1 == 0) { -374 return l2; //0 or positive -375 } -376 if (l2 == 0) { -377 return -1; -378 } -379 return super.compare(b1, s1, l1, b2, s2, l2); -380 } -381 } -382 -383 /** -384 * Pass this to TreeMaps where byte [] are keys. -385 */ -386 public final static Comparator<byte []> BYTES_COMPARATOR = new ByteArrayComparator(); -387 -388 /** -389 * Use comparing byte arrays, byte-by-byte -390 */ -391 public final static RawComparator<byte []> BYTES_RAWCOMPARATOR = new ByteArrayComparator(); -392 -393 /** -394 * Read byte-array written with a WritableableUtils.vint prefix. -395 * @param in Input to read from. -396 * @return byte array read off <code>in</code> -397 * @throws IOException e -398 */ -399 public static byte [] readByteArray(final DataInput in) -400 throws IOException { -401 int len = WritableUtils.readVInt(in); -402 if (len < 0) { -403 throw new NegativeArraySizeException(Integer.toString(len)); -404 } -405 byte [] result = new byte[len]; -406 in.readFully(result, 0, len); -407 return result; -408 } -409 -410 /** -411 * Read byte-array written with a WritableableUtils.vint prefix. -412 * IOException is converted to a RuntimeException. -413 * @param in Input to read from. -414 * @return byte array read off <code>in</code> -415 */ -416 public static byte [] readByteArrayThrowsRuntime(final DataInput in) { -417 try { -418 return readByteArray(in); -419 } catch (Exception e) { -420 throw new RuntimeException(e); -421 } -422 } -423 -424 /** -425 * Write byte-array with a WritableableUtils.vint prefix. -426 * @param out output stream to be written to -427 * @param b array to write -428 * @throws IOException e -429 */ -430 public static void writeByteArray(final DataOutput out, final byte [] b) -431 throws IOException { -432 if(b == null) { -433 WritableUtils.writeVInt(out, 0); -434 } else { -435 writeByteArray(out, b, 0, b.length); -436 } -437 } -438 -439 /** -440 * Write byte-array to out with a vint length prefix. -441 * @param out output stream -442 * @param b array -443 * @param offset offset into array -444 * @param length length past offset -445 * @throws IOException e -446 */ -447 public static void writeByteArray(final DataOutput out, final byte [] b, -448 final int offset, final int length) -449 throws IOException { -450 WritableUtils.writeVInt(out, length); -451 out.write(b, offset, length); -452 } -453 -454 /** -455 * Write byte-array from src to tgt with a vint length prefix. -456 * @param tgt target array -457 * @param tgtOffset offset into target array -458 * @param src source array -459 * @param srcOffset source offset -460 * @param srcLength source length -461 * @return New offset in src array. -462 */ -463 public static int writeByteArray(final byte [] tgt, final int tgtOffset, -464 final byte [] src, final int srcOffset, final int srcLength) { -465 byte [] vint = vintToBytes(srcLength); -466 System.arraycopy(vint, 0, tgt, tgtOffset, vint.length); -467 int offset = tgtOffset + vint.length; -468 System.arraycopy(src, srcOffset, tgt, offset, srcLength); -469 return offset + srcLength; -470 } -471 -472 /** -473 * Put bytes at the specified byte array position. -474 * @param tgtBytes the byte array -475 * @param tgtOffset position in the array -476 * @param srcBytes array to write out -477 * @param srcOffset source offset -478 * @param srcLength source length -479 * @return incremented offset -480 */ -481 public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes, -482 int srcOffset, int srcLength) { -483 System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength); -484 return tgtOffset + srcLength; -485 } -486 -487 /** -488 * Write a single byte out to the specified byte array position. -489 * @param bytes the byte array -490 * @param offset position in the array -491 * @param b byte to write out -492 * @return incremented offset -493 */ -494 public static int putByte(byte[] bytes, int offset, byte b) { -495 bytes[offset] = b; -496 return offset + 1; -497 } -498 -499 /** -500 * Add the whole content of the ByteBuffer to the bytes arrays. The ByteBuffer is modified. -501 * @param bytes the byte array -502 * @param offset position in the array -503 * @param buf ByteBuffer to write out -504 * @return incremented offset -505 */ -506 public static int putByteBuffer(byte[] bytes, int offset, ByteBuffer buf) { -507 int len = buf.remaining(); -508 buf.get(bytes, offset, len); -509 return offset + len; -510 } -511 -512 /** -513 * Returns a new byte array, copied from the given {@code buf}, -514 * from the index 0 (inclusive) to the limit (exclusive), -515 * regardless of the current position. -516 * The position and the other index parameters are not changed. -517 * -518 * @param buf a byte buffer -519 * @return the byte array -520 * @see #getBytes(ByteBuffer) -521 */ -522 public static byte[] toBytes(ByteBuffer buf) { -523 ByteBuffer dup = buf.duplicate(); -524 dup.position(0); -525 return readBytes(dup); -526 } -527 -528 private static byte[] readBytes(ByteBuffer buf) { -529 byte [] result = new byte[buf.remaining()]; -530 buf.get(result); -531 return result; -532 } -533 -534 /** -535 * @param b Presumed UTF-8 encoded byte array. -536 * @return String made from <code>b</code> -537 */ -538 public static String toString(final byte [] b) { -539 if (b == null) { -540 return null; -541 } -542 return toString(b, 0, b.length); -543 } -544 -545 /** -546 * Joins two byte arrays together using a separator. -547 * @param b1 The first byte array. -548 * @param sep The separator to use. -549 * @param b2 The second byte array. -550 */ -551 public static String toString(final byte [] b1, -552 String sep, -553 final byte [] b2) { -554 return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length); -555 } -556 -557 /** -558 * This method will convert utf8 encoded bytes into a string. If -559 * the given byte array is null, this method will return null. -560 * -561 * @param b Presumed UTF-8 encoded byte array. -562 * @param off offset into array -563 * @return String made from <code>b</code> or null -564 */ -565 public static String toString(final byte [] b, int off) { -566 if (b == null) { -567 return null; -568 } -569 int len = b.length - off; -570 if (len <= 0) { -571 return ""; -572 } -573 return new String(b, off, len, UTF8_CHARSET); -574 } -575 -576 /** -577 * This method will convert utf8 encoded bytes into a string. If -578 * the given byte array is null, this method will return null. -579 * -580 * @param b Presumed UTF-8 encoded byte array. -581 * @param off offset into array -582 * @param len length of utf-8 sequence -583 * @return String made from <code>b</code> or null -584 */ -585 public static String toString(final byte [] b, int off, int len) { -586 if (b == null) { -587 return null; -588 } -589 if (len == 0) { -590 return ""; -591 } -592 return new String(b, off, len, UTF8_CHARSET); -593 } -594 -595 /** -596 * Write a printable representation of a byte array. -597 * -598 * @param b byte array -599 * @return string -600 * @see #toStringBinary(byte[], int, int) -601 */ -602 public static String toStringBinary(final byte [] b) { -603 if (b == null) -604 return "null"; -605 return toStringBinary(b, 0, b.length); -606 } -607 -608 /** -609 * Converts the given byte buffer to a printable representation, -610 * from the index 0 (inclusive) to the limit (exclusive), -611 * regardless of the current position. -612 * The position and the other index parameters are not changed. -613 * -614 * @param buf a byte buffer -615 * @return a string representation of the buffer's binary contents -616 * @see #toBytes(ByteBuffer) -617 * @see #getBytes(ByteBuffer) -618 */ -619 public static String toStringBinary(ByteBuffer buf) { -620 if (buf == null) -621 return "null"; -622 if (buf.hasArray()) { -623 return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit()); -624 } -625 return toStringBinary(toBytes(buf)); -626 } -627 -628 /** -629 * Write a printable representation of a byte array. Non-printable -630 * characters are hex escaped in the format \\x%02X, eg: -631 * \x00 \x05 etc -632 * -633 * @param b array to write out -634 * @param off offset to start at -635 * @param len length to write -636 * @return string output -637 */ -638 public static String toStringBinary(final byte [] b, int off, int len) { -639 StringBuilder result = new StringBuilder(); -640 // Just in case we are passed a 'len' that is > buffer length... -641 if (off >= b.length) return result.toString(); -642 if (off + len > b.length) len = b.length - off; -643 for (int i = off; i < off + len ; ++i) { -644 int ch = b[i] & 0xFF; -645 if ((ch >= '0' && ch <= '9') -646 || (ch >= 'A' && ch <= 'Z') -647 || (ch >= 'a' && ch <= 'z') -648 || " `~!@#$%^&*()-_=+[]{}|;:'\",.<>/?".indexOf(ch) >= 0) { -649 result.append((char)ch); -650 } else { -651 result.append(String.format("\\x%02X", ch)); -652 } -653 } -654 return result.toString(); -655 } -656 -657 private static boolean isHexDigit(char c) { -658 return -659 (c >= 'A' && c <= 'F') || -660 (c >= '0' && c <= '9'); -661 } -662 -663 /** -664 * Takes a ASCII digit in the range A-F0-9 and returns -665 * the corresponding integer/ordinal value. -666 * @param ch The hex digit. -667 * @return The converted hex value as a byte. -668 */ -669 public static byte toBinaryFromHex(byte ch) { -670 if (ch >= 'A' && ch <= 'F') -671 return (byte) ((byte)10 + (byte) (ch - 'A')); -672 // else -673 return (byte) (ch - '0'); -674 } -675 -676 public static byte [] toBytesBinary(String in) { -677 // this may be bigger than we need, but let's be safe. -678 byte [] b = new byte[in.length()]; -679 int size = 0; -680 for (int i = 0; i < in.length(); ++i) { -681 char ch = in.charAt(i); -682 if (ch == '\\' && in.length() > i+1 && in.charAt(i+1) == 'x') { -683 // ok, take next 2 hex digits. -684 char hd1 = in.charAt(i+2); -685 char hd2 = in.charAt(i+3); -686 -687 // they need to be A-F0-9: -688 if (!isHexDigit(hd1) || -689 !isHexDigit(hd2)) { -690 // bogus escape code, ignore: -691 continue; -692 } -693 // turn hex ASCII digit -> number -694 byte d = (byte) ((toBinaryFromHex((byte)hd1) << 4) + toBinaryFromHex((byte)hd2)); -695 -696 b[size++] = d; -697 i += 3; // skip 3 -698 } else { -699 b[size++] = (byte) ch; -700 } -701 } -702 // resize: -703 byte [] b2 = new byte[size]; -704 System.arraycopy(b, 0, b2, 0, size); -705 return b2; -706 } -707 -708 /** -709 * Converts a string to a UTF-8 byte array. -710 * @param s string -711 * @return the byte array -712 */ -713 public static byte[] toBytes(String s) { -714 return s.getBytes(UTF8_CHARSET); -715 } -716 -717 /** -718 * Convert a boolean to a byte array. True becomes -1 -719 * and false becomes 0. -720 * -721 * @param b value -722 * @return <code>b</code> encoded in a byte array. -723 */ -724 public static byte [] toBytes(final boolean b) { -725 return new byte[] { b ? (byte) -1 : (byte) 0 }; -726 } -727 -728 /** -729 * Reverses {@link #toBytes(boolean)} -730 * @param b array -731 * @return True or false. -732 */ -733 public static boolean toBoolean(final byte [] b) { -734 if (b.length != 1) { -735 throw new IllegalArgumentException("Array has wrong size: " + b.length); -736 } -737 return b[0] != (byte) 0; -738 } -739 -740 /** -741 * Convert a long value to a byte array using big-endian. -742 * -743 * @param val value to convert -744 * @return the byte array -745 */ -746 public static byte[] toBytes(long val) { -747 byte [] b = new byte[8]; -748 for (int i = 7; i > 0; i--) { -749 b[i] = (byte) val; -750 val >>>= 8; -751 } -752 b[0] = (byte) val; -753 return b; -754 } -755 -756 /** -757 * Converts a byte array to a long value. Reverses -758 * {@link #toBytes(long)} -759 * @param bytes array -760 * @return the long value -761 */ -762 public static long toLong(byte[] bytes) { -763 return toLong(bytes, 0, SIZEOF_LONG); -764 } -765 -766 /** -767 * Converts a byte array to a long value. Assumes there will be -768 * {@link #SIZEOF_LONG} bytes available. -769 * -770 * @param bytes bytes -771 * @param offset offset -772 * @return the long value -773 */ -774 public static long toLong(byte[] bytes, int offset) { -775 return toLong(bytes, offset, SIZEOF_LONG); -776 } -777 -778 /** -779 * Converts a byte array to a long value. -780 * -781 * @param bytes array of bytes -782 * @param offset offset into array -783 * @param length length of data (must be {@link #SIZEOF_LONG}) -784 * @return the long value -785 * @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or -786 * if there's not enough room in the array at the offset indicated. -787 */ -788 public static long toLong(byte[] bytes, int offset, final int length) { -789 if (length != SIZEOF_LONG || offset + length > bytes.length) { -790 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG); -791 } -792 if (UnsafeAccess.unaligned()) { -793 return UnsafeAccess.toLong(bytes, offset); -794 } else { -795 long l = 0; -796 for(int i = offset; i < offset + length; i++) { -797 l <<= 8; -798 l ^= bytes[i] & 0xFF; -799 } -800 return l; -801 } -802 } -803 -804 private static IllegalArgumentException -805 explainWrongLengthOrOffset(final byte[] bytes, -806 final int offset, -807 final int length, -808 final int expectedLength) { -809 String reason; -810 if (length != expectedLength) { -811 reason = "Wrong length: " + length + ", expected " + expectedLength; -812 } else { -813 reason = "offset (" + offset + ") + length (" + length + ") exceed the" -814 + " capacity of the array: " + bytes.length; -815 } -816 return new IllegalArgumentException(reason); -817 } -818 -819 /** -820 * Put a long value out to the specified byte array position. -821 * @param bytes the byte array -822 * @param offset position in the array -823 * @param val long to write out -824 * @return incremented offset -825 * @throws IllegalArgumentException if the byte array given doesn't have -826 * enough room at the offset specified. -827 */ -828 public static int putLong(byte[] bytes, int offset, long val) { -829 if (bytes.length - offset < SIZEOF_LONG) { -830 throw new IllegalArgumentException("Not enough room to put a long at" -831 + " offset " + offset + " in a " + bytes.length + " byte array"); -832 } -833 if (UnsafeAccess.unaligned()) { -834 return UnsafeAccess.putLong(bytes, offset, val); -835 } else { -836 for(int i = offset + 7; i > offset; i--) { -837 bytes[i] = (byte) val; -838 val >>>= 8; -839 } -840 bytes[offset] = (byte) val; -841 return offset + SIZEOF_LONG; -842 } -843 } -844 -845 /** -846 * Put a long value out to the specified byte array position (Unsafe). -847 * @param bytes the byte array -848 * @param offset position in the array -849 * @param val long to write out -850 * @return incremented offset -851 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -852 */ -853 @Deprecated -854 public static int putLongUnsafe(byte[] bytes, int offset, long val) { -855 return UnsafeAccess.putLong(bytes, offset, val); -856 } -857 -858 /** -859 * Presumes float encoded as IEEE 754 floating-point "single format" -860 * @param bytes byte array -861 * @return Float made from passed byte array. -862 */ -863 public static float toFloat(byte [] bytes) { -864 return toFloat(bytes, 0); -865 } -866 -867 /** -868 * Presumes float encoded as IEEE 754 floating-point "single format" -869 * @param bytes array to convert -870 * @param offset offset into array -871 * @return Float made from passed byte array. -872 */ -873 public static float toFloat(byte [] bytes, int offset) { -874 return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT)); -875 } -876 -877 /** -878 * @param bytes byte array -879 * @param offset offset to write to -880 * @param f float value -881 * @return New offset in <code>bytes</code> -882 */ -883 public static int putFloat(byte [] bytes, int offset, float f) { -884 return putInt(bytes, offset, Float.floatToRawIntBits(f)); -885 } -886 -887 /** -888 * @param f float value -889 * @return the float represented as byte [] -890 */ -891 public static byte [] toBytes(final float f) { -892 // Encode it as int -893 return Bytes.toBytes(Float.floatToRawIntBits(f)); -894 } -895 -896 /** -897 * @param bytes byte array -898 * @return Return double made from passed bytes. -899 */ -900 public static double toDouble(final byte [] bytes) { -901 return toDouble(bytes, 0); -902 } -903 -904 /** -905 * @param bytes byte array -906 * @param offset offset where double is -907 * @return Return double made from passed bytes. -908 */ -909 public static double toDouble(final byte [] bytes, final int offset) { -910 return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG)); -911 } -912 -913 /** -914 * @param bytes byte array -915 * @param offset offset to write to -916 * @param d value -917 * @return New offset into array <code>bytes</code> -918 */ -919 public static int putDouble(byte [] bytes, int offset, double d) { -920 return putLong(bytes, offset, Double.doubleToLongBits(d)); -921 } -922 -923 /** -924 * Serialize a double as the IEEE 754 double format output. The resultant -925 * array will be 8 bytes long. -926 * -927 * @param d value -928 * @return the double represented as byte [] -929 */ -930 public static byte [] toBytes(final double d) { -931 // Encode it as a long -932 return Bytes.toBytes(Double.doubleToRawLongBits(d)); -933 } -934 -935 /** -936 * Convert an int value to a byte array. Big-endian. Same as what DataOutputStream.writeInt -937 * does. -938 * -939 * @param val value -940 * @return the byte array -941 */ -942 public static byte[] toBytes(int val) { -943 byte [] b = new byte[4]; -944 for(int i = 3; i > 0; i--) { -945 b[i] = (byte) val; -946 val >>>= 8; -947 } -948 b[0] = (byte) val; -949 return b; -950 } -951 -952 /** -953 * Converts a byte array to an int value -954 * @param bytes byte array -955 * @return the int value -956 */ -957 public static int toInt(byte[] bytes) { -958 return toInt(bytes, 0, SIZEOF_INT); -959 } -960 -961 /** -962 * Converts a byte array to an int value -963 * @param bytes byte array -964 * @param offset offset into array -965 * @return the int value -966 */ -967 public static int toInt(byte[] bytes, int offset) { -968 return toInt(bytes, offset, SIZEOF_INT); -969 } -970 -971 /** -972 * Converts a byte array to an int value -973 * @param bytes byte array -974 * @param offset offset into array -975 * @param length length of int (has to be {@link #SIZEOF_INT}) -976 * @return the int value -977 * @throws IllegalArgumentException if length is not {@link #SIZEOF_INT} or -978 * if there's not enough room in the array at the offset indicated. -979 */ -980 public static int toInt(byte[] bytes, int offset, final int length) { -981 if (length != SIZEOF_INT || offset + length > bytes.length) { -982 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT); -983 } -984 if (UnsafeAccess.unaligned()) { -985 return UnsafeAccess.toInt(bytes, offset); -986 } else { -987 int n = 0; -988 for(int i = offset; i < (offset + length); i++) { -989 n <<= 8; -990 n ^= bytes[i] & 0xFF; -991 } -992 return n; -993 } -994 } -995 -996 /** -997 * Converts a byte array to an int value (Unsafe version) -998 * @param bytes byte array -999 * @param offset offset into array -1000 * @return the int value -1001 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -1002 */ -1003 @Deprecated -1004 public static int toIntUnsafe(byte[] bytes, int offset) { -1005 return UnsafeAccess.toInt(bytes, offset); -1006 } -1007 -1008 /** -1009 * Converts a byte array to an short value (Unsafe version) -1010 * @param bytes byte array -1011 * @param offset offset into array -1012 * @return the short value -1013 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -1014 */ -1015 @Deprecated -1016 public static short toShortUnsafe(byte[] bytes, int offset) { -1017 return UnsafeAccess.toShort(bytes, offset); -1018 } -1019 -1020 /** -1021 * Converts a byte array to an long value (Unsafe version) -1022 * @param bytes byte array -1023 * @param offset offset into array -1024 * @return the long value -1025 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -1026 */ -1027 @Deprecated -1028 public static long toLongUnsafe(byte[] bytes, int offset) { -1029 return UnsafeAccess.toLong(bytes, offset); -1030 } -1031 -1032 /** -1033 * Converts a byte array to an int value -1034 * @param bytes byte array -1035 * @param offset offset into array -1036 * @param length how many bytes should be considered for creating int -1037 * @return the int value -1038 * @throws IllegalArgumentException if there's not enough room in the array at the offset -1039 * indicated. -1040 */ -1041 public static int readAsInt(byte[] bytes, int offset, final int length) { -1042 if (offset + length > bytes.length) { -1043 throw new IllegalArgumentException("offset (" + offset + ") + length (" + length -1044 + ") exceed the" + " capacity of the array: " + bytes.length); -1045 } -1046 int n = 0; -1047 for(int i = offset; i < (offset + length); i++) { -1048 n <<= 8; -1049 n ^= bytes[i] & 0xFF; -1050 } -1051 return n; -1052 } -1053 -1054 /** -1055 * Put an int value out to the specified byte array position. -1056 * @param bytes the byte array -1057 * @param offset position in the array -1058 * @param val int to write out -1059 * @return incremented offset -1060 * @throws IllegalArgumentException if the byte array given doesn't have -1061 * enough room at the offset specified. -1062 */ -1063 public static int putInt(byte[] bytes, int offset, int val) { -1064 if (bytes.length - offset < SIZEOF_INT) { -1065 throw new IllegalArgumentException("Not enough room to put an int at" -1066 + " offset " + offset + " in a " + bytes.length + " byte array"); -1067 } -1068 if (UnsafeAccess.unaligned()) { -1069 return UnsafeAccess.putInt(bytes, offset, val); -1070 } else { -1071 for(int i= offset + 3; i > offset; i--) { -1072 bytes[i] = (byte) val; -1073 val >>>= 8; -1074 } -1075 bytes[offset] = (byte) val; -1076 return offset + SIZEOF_INT; -1077 } -1078 } -1079 -1080 /** -1081 * Put an int value out to the specified byte array position (Unsafe). -1082 * @param bytes the byte array -1083 * @param offset position in the array -1084 * @param val int to write out -1085 * @return incremented offset -1086 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -1087 */ -1088 @Deprecated -1089 public static int putIntUnsafe(byte[] bytes, int offset, int val) { -1090 return UnsafeAccess.putInt(bytes, offset, val); -1091 } -1092 -1093 /** -1094 * Convert a short value to a byte array of {@link #SIZEOF_SHORT} bytes long. -1095 * @param val value -1096 * @return the byte array -1097 */ -1098 public static byte[] toBytes(short val) { -1099 byte[] b = new byte[SIZEOF_SHORT]; -1100 b[1] = (byte) val; -1101 val >>= 8; -1102 b[0] = (byte) val; -1103 return b; -1104 } -1105 -1106 /** -1107 * Converts a byte array to a short value -1108 * @param bytes byte array -1109 * @return the short value -1110 */ -1111 public static short toShort(byte[] bytes) { -1112