< prev index next >

src/java.base/share/classes/java/lang/Long.java

Print this page




 384             formatUnsignedLong0(val, shift, buf, 0, chars);
 385             return new String(buf, LATIN1);
 386         } else {
 387             byte[] buf = new byte[chars * 2];
 388             formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
 389             return new String(buf, UTF16);
 390         }
 391     }
 392 
 393     /**
 394      * Format a long (treated as unsigned) into a character buffer. If
 395      * {@code len} exceeds the formatted ASCII representation of {@code val},
 396      * {@code buf} will be padded with leading zeroes.
 397      *
 398      * @param val the unsigned long to format
 399      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 400      * @param buf the character buffer to write to
 401      * @param offset the offset in the destination buffer to start at
 402      * @param len the number of characters to write
 403      */
 404      static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
 405         // assert shift > 0 && shift <=5 : "Illegal shift value";
 406         // assert offset >= 0 && offset < buf.length : "illegal offset";
 407         // assert len > 0 && (offset + len) <= buf.length : "illegal length";
 408         int charPos = offset + len;
 409         int radix = 1 << shift;
 410         int mask = radix - 1;
 411         do {
 412             buf[--charPos] = Integer.digits[((int) val) & mask];
 413             val >>>= shift;
 414         } while (charPos > offset);
 415     }
 416 
 417     /** byte[]/LATIN1 version    */
 418     static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {
 419         int charPos = offset + len;
 420         int radix = 1 << shift;
 421         int mask = radix - 1;
 422         do {
 423             buf[--charPos] = (byte)Integer.digits[((int) val) & mask];
 424             val >>>= shift;
 425         } while (charPos > offset);
 426     }
 427 
 428     /** byte[]/UTF16 version    */
 429     static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, int offset, int len) {
 430         int charPos = offset + len;
 431         int radix = 1 << shift;
 432         int mask = radix - 1;
 433         do {
 434             StringUTF16.putChar(buf, --charPos, Integer.digits[((int) val) & mask]);
 435             val >>>= shift;
 436         } while (charPos > offset);

































 437     }
 438 
 439     /**
 440      * Returns a {@code String} object representing the specified
 441      * {@code long}.  The argument is converted to signed decimal
 442      * representation and returned as a string, exactly as if the
 443      * argument and the radix 10 were given as arguments to the {@link
 444      * #toString(long, int)} method.
 445      *
 446      * @param   i   a {@code long} to be converted.
 447      * @return  a string representation of the argument in base&nbsp;10.
 448      */
 449     public static String toString(long i) {
 450         int size = stringSize(i);
 451         if (COMPACT_STRINGS) {
 452             byte[] buf = new byte[size];
 453             getChars(i, size, buf);
 454             return new String(buf, LATIN1);
 455         } else {
 456             byte[] buf = new byte[size * 2];




 384             formatUnsignedLong0(val, shift, buf, 0, chars);
 385             return new String(buf, LATIN1);
 386         } else {
 387             byte[] buf = new byte[chars * 2];
 388             formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
 389             return new String(buf, UTF16);
 390         }
 391     }
 392 
 393     /**
 394      * Format a long (treated as unsigned) into a character buffer. If
 395      * {@code len} exceeds the formatted ASCII representation of {@code val},
 396      * {@code buf} will be padded with leading zeroes.
 397      *
 398      * @param val the unsigned long to format
 399      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 400      * @param buf the character buffer to write to
 401      * @param offset the offset in the destination buffer to start at
 402      * @param len the number of characters to write
 403      */












 404 
 405     /** byte[]/LATIN1 version    */
 406     static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {
 407         int charPos = offset + len;
 408         int radix = 1 << shift;
 409         int mask = radix - 1;
 410         do {
 411             buf[--charPos] = (byte)Integer.digits[((int) val) & mask];
 412             val >>>= shift;
 413         } while (charPos > offset);
 414     }
 415 
 416     /** byte[]/UTF16 version    */
 417     static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, int offset, int len) {
 418         int charPos = offset + len;
 419         int radix = 1 << shift;
 420         int mask = radix - 1;
 421         do {
 422             StringUTF16.putChar(buf, --charPos, Integer.digits[((int) val) & mask]);
 423             val >>>= shift;
 424         } while (charPos > offset);
 425     }
 426 
 427     static String fastUUID(long lsb, long msb) {
 428         if (COMPACT_STRINGS) {
 429             byte[] buf = new byte[36];
 430             formatUnsignedLong0(lsb,        4, buf, 24, 12);
 431             formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4);
 432             formatUnsignedLong0(msb,        4, buf, 14, 4);
 433             formatUnsignedLong0(msb >>> 16, 4, buf, 9,  4);
 434             formatUnsignedLong0(msb >>> 32, 4, buf, 0,  8);
 435 
 436             buf[23] = '-';
 437             buf[18] = '-';
 438             buf[13] = '-';
 439             buf[8]  = '-';
 440 
 441             return new String(buf, LATIN1);
 442         } else {
 443             byte[] buf = new byte[72];
 444 
 445             formatUnsignedLong0UTF16(lsb,        4, buf, 24, 12);
 446             formatUnsignedLong0UTF16(lsb >>> 48, 4, buf, 19, 4);
 447             formatUnsignedLong0UTF16(msb,        4, buf, 14, 4);
 448             formatUnsignedLong0UTF16(msb >>> 16, 4, buf, 9,  4);
 449             formatUnsignedLong0UTF16(msb >>> 32, 4, buf, 0,  8);
 450 
 451             StringUTF16.putChar(buf, 23, '-');
 452             StringUTF16.putChar(buf, 18, '-');
 453             StringUTF16.putChar(buf, 13, '-');
 454             StringUTF16.putChar(buf,  8, '-');
 455 
 456             return new String(buf, UTF16);
 457         }
 458     }
 459 
 460     /**
 461      * Returns a {@code String} object representing the specified
 462      * {@code long}.  The argument is converted to signed decimal
 463      * representation and returned as a string, exactly as if the
 464      * argument and the radix 10 were given as arguments to the {@link
 465      * #toString(long, int)} method.
 466      *
 467      * @param   i   a {@code long} to be converted.
 468      * @return  a string representation of the argument in base&nbsp;10.
 469      */
 470     public static String toString(long i) {
 471         int size = stringSize(i);
 472         if (COMPACT_STRINGS) {
 473             byte[] buf = new byte[size];
 474             getChars(i, size, buf);
 475             return new String(buf, LATIN1);
 476         } else {
 477             byte[] buf = new byte[size * 2];


< prev index next >