From commits-return-70639-archive-asf-public=cust-asf.ponee.io@hbase.apache.org Thu Apr 5 16:47:13 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 721CE180778 for ; Thu, 5 Apr 2018 16:47:11 +0200 (CEST) Received: (qmail 96017 invoked by uid 500); 5 Apr 2018 14:47:10 -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 95045 invoked by uid 99); 5 Apr 2018 14:47:09 -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; Thu, 05 Apr 2018 14:47:09 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A7130F6B91; Thu, 5 Apr 2018 14:47:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: git-site-role@apache.org To: commits@hbase.apache.org Date: Thu, 05 Apr 2018 14:47:13 -0000 Message-Id: <8693c343a5fc45e5ada24a6349678260@git.apache.org> In-Reply-To: <1554f91be0e64619983c78c3637800e3@git.apache.org> References: <1554f91be0e64619983c78c3637800e3@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [06/40] hbase-site git commit: Published site at e2b0490d18f7cc03aa59475a1b423597ddc481fb. http://git-wip-us.apache.org/repos/asf/hbase-site/blob/6c67ddd7/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.html b/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.html index 8c0d57c..e606e82 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/util/Bytes.LexicographicalComparerHolder.html @@ -46,2582 +46,2583 @@ 038import java.util.Iterator; 039import java.util.List; 040 -041import org.apache.hadoop.hbase.Cell; -042import org.apache.hadoop.hbase.CellComparator; -043import org.apache.hadoop.hbase.KeyValue; -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.com.google.common.annotations.VisibleForTesting; -052 -053import com.google.protobuf.ByteString; -054 -055import sun.misc.Unsafe; -056 -057/** -058 * Utility class that handles byte arrays, conversions to/from other types, -059 * comparisons, hash code generation, manufacturing keys for HashMaps or -060 * HashSets, and can be used as key in maps or trees. -061 */ -062@SuppressWarnings("restriction") -063@InterfaceAudience.Public -064@edu.umd.cs.findbugs.annotations.SuppressWarnings( -065 value="EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS", -066 justification="It has been like this forever") -067public class Bytes implements Comparable<Bytes> { -068 -069 // Using the charset canonical name for String/byte[] conversions is much -070 // more efficient due to use of cached encoders/decoders. -071 private static final String UTF8_CSN = StandardCharsets.UTF_8.name(); -072 -073 //HConstants.EMPTY_BYTE_ARRAY should be updated if this changed -074 private static final byte [] EMPTY_BYTE_ARRAY = new byte [0]; -075 -076 private static final Logger LOG = LoggerFactory.getLogger(Bytes.class); -077 -078 /** -079 * Size of boolean in bytes -080 */ -081 public static final int SIZEOF_BOOLEAN = Byte.SIZE / Byte.SIZE; -082 -083 /** -084 * Size of byte in bytes -085 */ -086 public static final int SIZEOF_BYTE = SIZEOF_BOOLEAN; -087 -088 /** -089 * Size of char in bytes -090 */ -091 public static final int SIZEOF_CHAR = Character.SIZE / Byte.SIZE; -092 -093 /** -094 * Size of double in bytes -095 */ -096 public static final int SIZEOF_DOUBLE = Double.SIZE / Byte.SIZE; -097 -098 /** -099 * Size of float in bytes -100 */ -101 public static final int SIZEOF_FLOAT = Float.SIZE / Byte.SIZE; -102 -103 /** -104 * Size of int in bytes -105 */ -106 public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE; -107 -108 /** -109 * Size of long in bytes -110 */ -111 public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE; -112 -113 /** -114 * Size of short in bytes -115 */ -116 public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE; -117 -118 /** -119 * Mask to apply to a long to reveal the lower int only. Use like this: -120 * int i = (int)(0xFFFFFFFF00000000L ^ some_long_value); -121 */ -122 public static final long MASK_FOR_LOWER_INT_IN_LONG = 0xFFFFFFFF00000000L; -123 -124 /** -125 * Estimate of size cost to pay beyond payload in jvm for instance of byte []. -126 * Estimate based on study of jhat and jprofiler numbers. -127 */ -128 // JHat says BU is 56 bytes. -129 // SizeOf which uses java.lang.instrument says 24 bytes. (3 longs?) -130 public static final int ESTIMATED_HEAP_TAX = 16; -131 -132 private static final boolean UNSAFE_UNALIGNED = UnsafeAvailChecker.unaligned(); -133 -134 /** -135 * Returns length of the byte array, returning 0 if the array is null. -136 * Useful for calculating sizes. -137 * @param b byte array, which can be null -138 * @return 0 if b is null, otherwise returns length -139 */ -140 final public static int len(byte[] b) { -141 return b == null ? 0 : b.length; -142 } -143 -144 private byte[] bytes; -145 private int offset; -146 private int length; -147 -148 /** -149 * Create a zero-size sequence. -150 */ -151 public Bytes() { -152 super(); -153 } -154 -155 /** -156 * Create a Bytes using the byte array as the initial value. -157 * @param bytes This array becomes the backing storage for the object. -158 */ -159 public Bytes(byte[] bytes) { -160 this(bytes, 0, bytes.length); -161 } -162 -163 /** -164 * Set the new Bytes to the contents of the passed -165 * <code>ibw</code>. -166 * @param ibw the value to set this Bytes to. -167 */ -168 public Bytes(final Bytes ibw) { -169 this(ibw.get(), ibw.getOffset(), ibw.getLength()); -170 } -171 -172 /** -173 * Set the value to a given byte range -174 * @param bytes the new byte range to set to -175 * @param offset the offset in newData to start at -176 * @param length the number of bytes in the range -177 */ -178 public Bytes(final byte[] bytes, final int offset, -179 final int length) { -180 this.bytes = bytes; -181 this.offset = offset; -182 this.length = length; -183 } -184 -185 /** -186 * Copy bytes from ByteString instance. -187 * @param byteString copy from -188 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -189 */ -190 @Deprecated -191 public Bytes(final ByteString byteString) { -192 this(byteString.toByteArray()); -193 } -194 -195 /** -196 * Get the data from the Bytes. -197 * @return The data is only valid between offset and offset+length. -198 */ -199 public byte [] get() { -200 if (this.bytes == null) { -201 throw new IllegalStateException("Uninitialiized. Null constructor " + -202 "called w/o accompaying readFields invocation"); -203 } -204 return this.bytes; -205 } -206 -207 /** -208 * @param b Use passed bytes as backing array for this instance. -209 */ -210 public void set(final byte [] b) { -211 set(b, 0, b.length); -212 } -213 -214 /** -215 * @param b Use passed bytes as backing array for this instance. -216 * @param offset -217 * @param length -218 */ -219 public void set(final byte [] b, final int offset, final int length) { -220 this.bytes = b; -221 this.offset = offset; -222 this.length = length; -223 } -224 -225 /** -226 * @return the number of valid bytes in the buffer -227 * @deprecated use {@link #getLength()} instead -228 */ -229 @Deprecated -230 public int getSize() { -231 if (this.bytes == null) { -232 throw new IllegalStateException("Uninitialiized. Null constructor " + -233 "called w/o accompaying readFields invocation"); -234 } -235 return this.length; -236 } -237 -238 /** -239 * @return the number of valid bytes in the buffer -240 */ -241 public int getLength() { -242 if (this.bytes == null) { -243 throw new IllegalStateException("Uninitialiized. Null constructor " + -244 "called w/o accompaying readFields invocation"); -245 } -246 return this.length; -247 } -248 -249 /** -250 * @return offset -251 */ -252 public int getOffset(){ -253 return this.offset; -254 } -255 -256 /** -257 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -258 */ -259 @Deprecated -260 public ByteString toByteString() { -261 return ByteString.copyFrom(this.bytes, this.offset, this.length); -262 } -263 -264 @Override -265 public int hashCode() { -266 return Bytes.hashCode(bytes, offset, length); -267 } -268 -269 /** -270 * Define the sort order of the Bytes. -271 * @param that The other bytes writable -272 * @return Positive if left is bigger than right, 0 if they are equal, and -273 * negative if left is smaller than right. -274 */ -275 @Override -276 public int compareTo(Bytes that) { -277 return BYTES_RAWCOMPARATOR.compare( -278 this.bytes, this.offset, this.length, -279 that.bytes, that.offset, that.length); -280 } -281 -282 /** -283 * Compares the bytes in this object to the specified byte array -284 * @param that -285 * @return Positive if left is bigger than right, 0 if they are equal, and -286 * negative if left is smaller than right. -287 */ -288 public int compareTo(final byte [] that) { -289 return BYTES_RAWCOMPARATOR.compare( -290 this.bytes, this.offset, this.length, -291 that, 0, that.length); -292 } -293 -294 /** -295 * @see Object#equals(Object) -296 */ -297 @Override -298 public boolean equals(Object right_obj) { -299 if (right_obj instanceof byte []) { -300 return compareTo((byte [])right_obj) == 0; -301 } -302 if (right_obj instanceof Bytes) { -303 return compareTo((Bytes)right_obj) == 0; -304 } -305 return false; -306 } -307 -308 /** -309 * @see Object#toString() -310 */ -311 @Override -312 public String toString() { -313 return Bytes.toString(bytes, offset, length); -314 } -315 -316 /** -317 * @param array List of byte []. -318 * @return Array of byte []. -319 */ -320 public static byte [][] toArray(final List<byte []> array) { -321 // List#toArray doesn't work on lists of byte []. -322 byte[][] results = new byte[array.size()][]; -323 for (int i = 0; i < array.size(); i++) { -324 results[i] = array.get(i); -325 } -326 return results; -327 } -328 -329 /** -330 * Returns a copy of the bytes referred to by this writable -331 */ -332 public byte[] copyBytes() { -333 return Arrays.copyOfRange(bytes, offset, offset+length); -334 } -335 /** -336 * Byte array comparator class. -337 */ -338 @InterfaceAudience.Public -339 public static class ByteArrayComparator implements RawComparator<byte []> { -340 /** -341 * Constructor -342 */ -343 public ByteArrayComparator() { -344 super(); -345 } -346 @Override -347 public int compare(byte [] left, byte [] right) { -348 return compareTo(left, right); -349 } -350 @Override -351 public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) { -352 return LexicographicalComparerHolder.BEST_COMPARER. -353 compareTo(b1, s1, l1, b2, s2, l2); -354 } -355 } -356 -357 /** -358 * A {@link ByteArrayComparator} that treats the empty array as the largest value. -359 * This is useful for comparing row end keys for regions. -360 */ -361 // TODO: unfortunately, HBase uses byte[0] as both start and end keys for region -362 // boundaries. Thus semantically, we should treat empty byte array as the smallest value -363 // while comparing row keys, start keys etc; but as the largest value for comparing -364 // region boundaries for endKeys. -365 @InterfaceAudience.Public -366 public static class RowEndKeyComparator extends ByteArrayComparator { -367 @Override -368 public int compare(byte[] left, byte[] right) { -369 return compare(left, 0, left.length, right, 0, right.length); -370 } -371 @Override -372 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { -373 if (b1 == b2 && s1 == s2 && l1 == l2) { -374 return 0; -375 } -376 if (l1 == 0) { -377 return l2; //0 or positive -378 } -379 if (l2 == 0) { -380 return -1; -381 } -382 return super.compare(b1, s1, l1, b2, s2, l2); -383 } -384 } -385 -386 /** -387 * Pass this to TreeMaps where byte [] are keys. -388 */ -389 public final static Comparator<byte []> BYTES_COMPARATOR = new ByteArrayComparator(); -390 -391 /** -392 * Use comparing byte arrays, byte-by-byte -393 */ -394 public final static RawComparator<byte []> BYTES_RAWCOMPARATOR = new ByteArrayComparator(); -395 -396 /** -397 * Read byte-array written with a WritableableUtils.vint prefix. -398 * @param in Input to read from. -399 * @return byte array read off <code>in</code> -400 * @throws IOException e -401 */ -402 public static byte [] readByteArray(final DataInput in) -403 throws IOException { -404 int len = WritableUtils.readVInt(in); -405 if (len < 0) { -406 throw new NegativeArraySizeException(Integer.toString(len)); -407 } -408 byte [] result = new byte[len]; -409 in.readFully(result, 0, len); -410 return result; -411 } -412 -413 /** -414 * Read byte-array written with a WritableableUtils.vint prefix. -415 * IOException is converted to a RuntimeException. -416 * @param in Input to read from. -417 * @return byte array read off <code>in</code> -418 */ -419 public static byte [] readByteArrayThrowsRuntime(final DataInput in) { -420 try { -421 return readByteArray(in); -422 } catch (Exception e) { -423 throw new RuntimeException(e); -424 } -425 } -426 -427 /** -428 * Write byte-array with a WritableableUtils.vint prefix. -429 * @param out output stream to be written to -430 * @param b array to write -431 * @throws IOException e -432 */ -433 public static void writeByteArray(final DataOutput out, final byte [] b) -434 throws IOException { -435 if(b == null) { -436 WritableUtils.writeVInt(out, 0); -437 } else { -438 writeByteArray(out, b, 0, b.length); -439 } -440 } -441 -442 /** -443 * Write byte-array to out with a vint length prefix. -444 * @param out output stream -445 * @param b array -446 * @param offset offset into array -447 * @param length length past offset -448 * @throws IOException e -449 */ -450 public static void writeByteArray(final DataOutput out, final byte [] b, -451 final int offset, final int length) -452 throws IOException { -453 WritableUtils.writeVInt(out, length); -454 out.write(b, offset, length); -455 } -456 -457 /** -458 * Write byte-array from src to tgt with a vint length prefix. -459 * @param tgt target array -460 * @param tgtOffset offset into target array -461 * @param src source array -462 * @param srcOffset source offset -463 * @param srcLength source length -464 * @return New offset in src array. -465 */ -466 public static int writeByteArray(final byte [] tgt, final int tgtOffset, -467 final byte [] src, final int srcOffset, final int srcLength) { -468 byte [] vint = vintToBytes(srcLength); -469 System.arraycopy(vint, 0, tgt, tgtOffset, vint.length); -470 int offset = tgtOffset + vint.length; -471 System.arraycopy(src, srcOffset, tgt, offset, srcLength); -472 return offset + srcLength; -473 } -474 -475 /** -476 * Put bytes at the specified byte array position. -477 * @param tgtBytes the byte array -478 * @param tgtOffset position in the array -479 * @param srcBytes array to write out -480 * @param srcOffset source offset -481 * @param srcLength source length -482 * @return incremented offset -483 */ -484 public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes, -485 int srcOffset, int srcLength) { -486 System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength); -487 return tgtOffset + srcLength; -488 } -489 -490 /** -491 * Write a single byte out to the specified byte array position. -492 * @param bytes the byte array -493 * @param offset position in the array -494 * @param b byte to write out -495 * @return incremented offset -496 */ -497 public static int putByte(byte[] bytes, int offset, byte b) { -498 bytes[offset] = b; -499 return offset + 1; -500 } -501 -502 /** -503 * Add the whole content of the ByteBuffer to the bytes arrays. The ByteBuffer is modified. -504 * @param bytes the byte array -505 * @param offset position in the array -506 * @param buf ByteBuffer to write out -507 * @return incremented offset -508 */ -509 public static int putByteBuffer(byte[] bytes, int offset, ByteBuffer buf) { -510 int len = buf.remaining(); -511 buf.get(bytes, offset, len); -512 return offset + len; -513 } -514 -515 /** -516 * Returns a new byte array, copied from the given {@code buf}, -517 * from the index 0 (inclusive) to the limit (exclusive), -518 * regardless of the current position. -519 * The position and the other index parameters are not changed. -520 * -521 * @param buf a byte buffer -522 * @return the byte array -523 * @see #getBytes(ByteBuffer) -524 */ -525 public static byte[] toBytes(ByteBuffer buf) { -526 ByteBuffer dup = buf.duplicate(); -527 dup.position(0); -528 return readBytes(dup); -529 } -530 -531 private static byte[] readBytes(ByteBuffer buf) { -532 byte [] result = new byte[buf.remaining()]; -533 buf.get(result); -534 return result; -535 } -536 -537 /** -538 * @param b Presumed UTF-8 encoded byte array. -539 * @return String made from <code>b</code> -540 */ -541 public static String toString(final byte [] b) { -542 if (b == null) { -543 return null; -544 } -545 return toString(b, 0, b.length); -546 } -547 -548 /** -549 * Joins two byte arrays together using a separator. -550 * @param b1 The first byte array. -551 * @param sep The separator to use. -552 * @param b2 The second byte array. -553 */ -554 public static String toString(final byte [] b1, -555 String sep, -556 final byte [] b2) { -557 return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length); -558 } -559 -560 /** -561 * This method will convert utf8 encoded bytes into a string. If -562 * the given byte array is null, this method will return null. -563 * -564 * @param b Presumed UTF-8 encoded byte array. -565 * @param off offset into array -566 * @return String made from <code>b</code> or null -567 */ -568 public static String toString(final byte[] b, int off) { -569 if (b == null) { -570 return null; -571 } -572 int len = b.length - off; -573 if (len <= 0) { -574 return ""; -575 } -576 try { -577 return new String(b, off, len, UTF8_CSN); -578 } catch (UnsupportedEncodingException e) { -579 // should never happen! -580 throw new IllegalArgumentException("UTF8 encoding is not supported", e); -581 } -582 } -583 -584 /** -585 * This method will convert utf8 encoded bytes into a string. If -586 * the given byte array is null, this method will return null. -587 * -588 * @param b Presumed UTF-8 encoded byte array. -589 * @param off offset into array -590 * @param len length of utf-8 sequence -591 * @return String made from <code>b</code> or null -592 */ -593 public static String toString(final byte[] b, int off, int len) { -594 if (b == null) { -595 return null; -596 } -597 if (len == 0) { -598 return ""; -599 } -600 try { -601 return new String(b, off, len, UTF8_CSN); -602 } catch (UnsupportedEncodingException e) { -603 // should never happen! -604 throw new IllegalArgumentException("UTF8 encoding is not supported", e); -605 } -606 } -607 -608 /** -609 * Write a printable representation of a byte array. -610 * -611 * @param b byte array -612 * @return string -613 * @see #toStringBinary(byte[], int, int) -614 */ -615 public static String toStringBinary(final byte [] b) { -616 if (b == null) -617 return "null"; -618 return toStringBinary(b, 0, b.length); -619 } -620 -621 /** -622 * Converts the given byte buffer to a printable representation, -623 * from the index 0 (inclusive) to the limit (exclusive), -624 * regardless of the current position. -625 * The position and the other index parameters are not changed. -626 * -627 * @param buf a byte buffer -628 * @return a string representation of the buffer's binary contents -629 * @see #toBytes(ByteBuffer) -630 * @see #getBytes(ByteBuffer) -631 */ -632 public static String toStringBinary(ByteBuffer buf) { -633 if (buf == null) -634 return "null"; -635 if (buf.hasArray()) { -636 return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit()); -637 } -638 return toStringBinary(toBytes(buf)); -639 } -640 -641 private static final char[] HEX_CHARS_UPPER = { -642 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' -643 }; -644 -645 /** -646 * Write a printable representation of a byte array. Non-printable -647 * characters are hex escaped in the format \\x%02X, eg: -648 * \x00 \x05 etc -649 * -650 * @param b array to write out -651 * @param off offset to start at -652 * @param len length to write -653 * @return string output -654 */ -655 public static String toStringBinary(final byte [] b, int off, int len) { -656 StringBuilder result = new StringBuilder(); -657 // Just in case we are passed a 'len' that is > buffer length... -658 if (off >= b.length) return result.toString(); -659 if (off + len > b.length) len = b.length - off; -660 for (int i = off; i < off + len ; ++i) { -661 int ch = b[i] & 0xFF; -662 if (ch >= ' ' && ch <= '~' && ch != '\\') { -663 result.append((char)ch); -664 } else { -665 result.append("\\x"); -666 result.append(HEX_CHARS_UPPER[ch / 0x10]); -667 result.append(HEX_CHARS_UPPER[ch % 0x10]); -668 } -669 } -670 return result.toString(); -671 } -672 -673 private static boolean isHexDigit(char c) { -674 return -675 (c >= 'A' && c <= 'F') || -676 (c >= '0' && c <= '9'); -677 } -678 -679 /** -680 * Takes a ASCII digit in the range A-F0-9 and returns -681 * the corresponding integer/ordinal value. -682 * @param ch The hex digit. -683 * @return The converted hex value as a byte. -684 */ -685 public static byte toBinaryFromHex(byte ch) { -686 if (ch >= 'A' && ch <= 'F') -687 return (byte) ((byte)10 + (byte) (ch - 'A')); -688 // else -689 return (byte) (ch - '0'); -690 } -691 -692 public static byte [] toBytesBinary(String in) { -693 // this may be bigger than we need, but let's be safe. -694 byte [] b = new byte[in.length()]; -695 int size = 0; -696 for (int i = 0; i < in.length(); ++i) { -697 char ch = in.charAt(i); -698 if (ch == '\\' && in.length() > i+1 && in.charAt(i+1) == 'x') { -699 // ok, take next 2 hex digits. -700 char hd1 = in.charAt(i+2); -701 char hd2 = in.charAt(i+3); -702 -703 // they need to be A-F0-9: -704 if (!isHexDigit(hd1) || -705 !isHexDigit(hd2)) { -706 // bogus escape code, ignore: -707 continue; -708 } -709 // turn hex ASCII digit -> number -710 byte d = (byte) ((toBinaryFromHex((byte)hd1) << 4) + toBinaryFromHex((byte)hd2)); -711 -712 b[size++] = d; -713 i += 3; // skip 3 -714 } else { -715 b[size++] = (byte) ch; -716 } -717 } -718 // resize: -719 byte [] b2 = new byte[size]; -720 System.arraycopy(b, 0, b2, 0, size); -721 return b2; -722 } -723 -724 /** -725 * Converts a string to a UTF-8 byte array. -726 * @param s string -727 * @return the byte array -728 */ -729 public static byte[] toBytes(String s) { -730 try { -731 return s.getBytes(UTF8_CSN); -732 } catch (UnsupportedEncodingException e) { -733 // should never happen! -734 throw new IllegalArgumentException("UTF8 decoding is not supported", e); -735 } -736 } -737 -738 /** -739 * Convert a boolean to a byte array. True becomes -1 -740 * and false becomes 0. -741 * -742 * @param b value -743 * @return <code>b</code> encoded in a byte array. -744 */ -745 public static byte [] toBytes(final boolean b) { -746 return new byte[] { b ? (byte) -1 : (byte) 0 }; -747 } -748 -749 /** -750 * Reverses {@link #toBytes(boolean)} -751 * @param b array -752 * @return True or false. -753 */ -754 public static boolean toBoolean(final byte [] b) { -755 if (b.length != 1) { -756 throw new IllegalArgumentException("Array has wrong size: " + b.length); -757 } -758 return b[0] != (byte) 0; -759 } -760 -761 /** -762 * Convert a long value to a byte array using big-endian. -763 * -764 * @param val value to convert -765 * @return the byte array -766 */ -767 public static byte[] toBytes(long val) { -768 byte [] b = new byte[8]; -769 for (int i = 7; i > 0; i--) { -770 b[i] = (byte) val; -771 val >>>= 8; -772 } -773 b[0] = (byte) val; -774 return b; -775 } -776 -777 /** -778 * Converts a byte array to a long value. Reverses -779 * {@link #toBytes(long)} -780 * @param bytes array -781 * @return the long value -782 */ -783 public static long toLong(byte[] bytes) { -784 return toLong(bytes, 0, SIZEOF_LONG); -785 } -786 -787 /** -788 * Converts a byte array to a long value. Assumes there will be -789 * {@link #SIZEOF_LONG} bytes available. -790 * -791 * @param bytes bytes -792 * @param offset offset -793 * @return the long value -794 */ -795 public static long toLong(byte[] bytes, int offset) { -796 return toLong(bytes, offset, SIZEOF_LONG); -797 } -798 -799 /** -800 * Converts a byte array to a long value. -801 * -802 * @param bytes array of bytes -803 * @param offset offset into array -804 * @param length length of data (must be {@link #SIZEOF_LONG}) -805 * @return the long value -806 * @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or -807 * if there's not enough room in the array at the offset indicated. -808 */ -809 public static long toLong(byte[] bytes, int offset, final int length) { -810 if (length != SIZEOF_LONG || offset + length > bytes.length) { -811 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG); -812 } -813 if (UNSAFE_UNALIGNED) { -814 return UnsafeAccess.toLong(bytes, offset); -815 } else { -816 long l = 0; -817 for(int i = offset; i < offset + length; i++) { -818 l <<= 8; -819 l ^= bytes[i] & 0xFF; -820 } -821 return l; -822 } -823 } -824 -825 private static IllegalArgumentException -826 explainWrongLengthOrOffset(final byte[] bytes, -827 final int offset, -828 final int length, -829 final int expectedLength) { -830 String reason; -831 if (length != expectedLength) { -832 reason = "Wrong length: " + length + ", expected " + expectedLength; -833 } else { -834 reason = "offset (" + offset + ") + length (" + length + ") exceed the" -835 + " capacity of the array: " + bytes.length; -836 } -837 return new IllegalArgumentException(reason); -838 } -839 -840 /** -841 * Put a long value out to the specified byte array position. -842 * @param bytes the byte array -843 * @param offset position in the array -844 * @param val long to write out -845 * @return incremented offset -846 * @throws IllegalArgumentException if the byte array given doesn't have -847 * enough room at the offset specified. -848 */ -849 public static int putLong(byte[] bytes, int offset, long val) { -850 if (bytes.length - offset < SIZEOF_LONG) { -851 throw new IllegalArgumentException("Not enough room to put a long at" -852 + " offset " + offset + " in a " + bytes.length + " byte array"); -853 } -854 if (UNSAFE_UNALIGNED) { -855 return UnsafeAccess.putLong(bytes, offset, val); -856 } else { -857 for(int i = offset + 7; i > offset; i--) { -858 bytes[i] = (byte) val; -859 val >>>= 8; -860 } -861 bytes[offset] = (byte) val; -862 return offset + SIZEOF_LONG; -863 } -864 } -865 -866 /** -867 * Put a long value out to the specified byte array position (Unsafe). -868 * @param bytes the byte array -869 * @param offset position in the array -870 * @param val long to write out -871 * @return incremented offset -872 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -873 */ -874 @Deprecated -875 public static int putLongUnsafe(byte[] bytes, int offset, long val) { -876 return UnsafeAccess.putLong(bytes, offset, val); -877 } -878 -879 /** -880 * Presumes float encoded as IEEE 754 floating-point "single format" -881 * @param bytes byte array -882 * @return Float made from passed byte array. -883 */ -884 public static float toFloat(byte [] bytes) { -885 return toFloat(bytes, 0); -886 } -887 -888 /** -889 * Presumes float encoded as IEEE 754 floating-point "single format" -890 * @param bytes array to convert -891 * @param offset offset into array -892 * @return Float made from passed byte array. -893 */ -894 public static float toFloat(byte [] bytes, int offset) { -895 return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT)); -896 } -897 -898 /** -899 * @param bytes byte array -900 * @param offset offset to write to -901 * @param f float value -902 * @return New offset in <code>bytes</code> -903 */ -904 public static int putFloat(byte [] bytes, int offset, float f) { -905 return putInt(bytes, offset, Float.floatToRawIntBits(f)); -906 } -907 -908 /** -909 * @param f float value -910 * @return the float represented as byte [] -911 */ -912 public static byte [] toBytes(final float f) { -913 // Encode it as int -914 return Bytes.toBytes(Float.floatToRawIntBits(f)); -915 } -916 -917 /** -918 * @param bytes byte array -919 * @return Return double made from passed bytes. -920 */ -921 public static double toDouble(final byte [] bytes) { -922 return toDouble(bytes, 0); -923 } -924 -925 /** -926 * @param bytes byte array -927 * @param offset offset where double is -928 * @return Return double made from passed bytes. -929 */ -930 public static double toDouble(final byte [] bytes, final int offset) { -931 return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG)); -932 } -933 -934 /** -935 * @param bytes byte array -936 * @param offset offset to write to -937 * @param d value -938 * @return New offset into array <code>bytes</code> -939 */ -940 public static int putDouble(byte [] bytes, int offset, double d) { -941 return putLong(bytes, offset, Double.doubleToLongBits(d)); -942 } -943 -944 /** -945 * Serialize a double as the IEEE 754 double format output. The resultant -946 * array will be 8 bytes long. -947 * -948 * @param d value -949 * @return the double represented as byte [] -950 */ -951 public static byte [] toBytes(final double d) { -952 // Encode it as a long -953 return Bytes.toBytes(Double.doubleToRawLongBits(d)); -954 } -955 -956 /** -957 * Convert an int value to a byte array. Big-endian. Same as what DataOutputStream.writeInt -958 * does. -959 * -960 * @param val value -961 * @return the byte array -962 */ -963 public static byte[] toBytes(int val) { -964 byte [] b = new byte[4]; -965 for(int i = 3; i > 0; i--) { -966 b[i] = (byte) val; -967 val >>>= 8; -968 } -969 b[0] = (byte) val; -970 return b; -971 } -972 -973 /** -974 * Converts a byte array to an int value -975 * @param bytes byte array -976 * @return the int value -977 */ -978 public static int toInt(byte[] bytes) { -979 return toInt(bytes, 0, SIZEOF_INT); -980 } -981 -982 /** -983 * Converts a byte array to an int value -984 * @param bytes byte array -985 * @param offset offset into array -986 * @return the int value -987 */ -988 public static int toInt(byte[] bytes, int offset) { -989 return toInt(bytes, offset, SIZEOF_INT); -990 } -991 -992 /** -993 * Converts a byte array to an int value -994 * @param bytes byte array -995 * @param offset offset into array -996 * @param length length of int (has to be {@link #SIZEOF_INT}) -997 * @return the int value -998 * @throws IllegalArgumentException if length is not {@link #SIZEOF_INT} or -999 * if there's not enough room in the array at the offset indicated. -1000 */ -1001 public static int toInt(byte[] bytes, int offset, final int length) { -1002 if (length != SIZEOF_INT || offset + length > bytes.length) { -1003 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT); -1004 } -1005 if (UNSAFE_UNALIGNED) { -1006 return UnsafeAccess.toInt(bytes, offset); -1007 } else { -1008 int n = 0; -1009 for(int i = offset; i < (offset + length); i++) { -1010 n <<= 8; -1011 n ^= bytes[i] & 0xFF; -1012 } -1013 return n; -1014 } -1015 } -1016 -1017 /** -1018 * Converts a byte array to an int value (Unsafe version) -1019 * @param bytes byte array -1020 * @param offset offset into array -1021 * @return the int value -1022 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. -