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

Print this page
rev 10110 : 8050114: Expose Integer/Long formatUnsigned methods internally
Reviewed-by: mduigou


 343     public static String toBinaryString(long i) {
 344         return toUnsignedString0(i, 1);
 345     }
 346 
 347     /**
 348      * Format a long (treated as unsigned) into a String.
 349      * @param val the value to format
 350      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 351      */
 352     static String toUnsignedString0(long val, int shift) {
 353         // assert shift > 0 && shift <=5 : "Illegal shift value";
 354         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 355         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 356         char[] buf = new char[chars];
 357 
 358         formatUnsignedLong(val, shift, buf, 0, chars);
 359         return new String(buf, true);
 360     }
 361 
 362     /**
 363      * Format a long (treated as unsigned) into a character buffer.



 364      * @param val the unsigned long to format
 365      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 366      * @param buf the character buffer to write to
 367      * @param offset the offset in the destination buffer to start at
 368      * @param len the number of characters to write
 369      * @return the lowest character location used
 370      */
 371      static int formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
 372         int charPos = len;



 373         int radix = 1 << shift;
 374         int mask = radix - 1;
 375         do {
 376             buf[offset + --charPos] = Integer.digits[((int) val) & mask];
 377             val >>>= shift;
 378         } while (val != 0 && charPos > 0);
 379 
 380         return charPos;
 381     }
 382 
 383     /**
 384      * Returns a {@code String} object representing the specified
 385      * {@code long}.  The argument is converted to signed decimal
 386      * representation and returned as a string, exactly as if the
 387      * argument and the radix 10 were given as arguments to the {@link
 388      * #toString(long, int)} method.
 389      *
 390      * @param   i   a {@code long} to be converted.
 391      * @return  a string representation of the argument in base&nbsp;10.
 392      */
 393     public static String toString(long i) {
 394         if (i == Long.MIN_VALUE)
 395             return "-9223372036854775808";
 396         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 397         char[] buf = new char[size];
 398         getChars(i, size, buf);
 399         return new String(buf, true);
 400     }




 343     public static String toBinaryString(long i) {
 344         return toUnsignedString0(i, 1);
 345     }
 346 
 347     /**
 348      * Format a long (treated as unsigned) into a String.
 349      * @param val the value to format
 350      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 351      */
 352     static String toUnsignedString0(long val, int shift) {
 353         // assert shift > 0 && shift <=5 : "Illegal shift value";
 354         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 355         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 356         char[] buf = new char[chars];
 357 
 358         formatUnsignedLong(val, shift, buf, 0, chars);
 359         return new String(buf, true);
 360     }
 361 
 362     /**
 363      * Format a long (treated as unsigned) into a character buffer. If
 364      * {@code len} exceeds the formatted ASCII representation of {@code val},
 365      * {@code buf} will be padded with leading zeroes.
 366      *
 367      * @param val the unsigned long to format
 368      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 369      * @param buf the character buffer to write to
 370      * @param offset the offset in the destination buffer to start at
 371      * @param len the number of characters to write

 372      */
 373      static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
 374         // assert shift > 0 && shift <=5 : "Illegal shift value";
 375         // assert offset >= 0 && offset < buf.length : "illegal offset";
 376         // assert len > 0 && (offset + len) <= buf.length : "illegal length";
 377         int charPos = offset + len;
 378         int radix = 1 << shift;
 379         int mask = radix - 1;
 380         while (charPos > offset) {
 381             buf[--charPos] = Integer.digits[((int) val) & mask];
 382             val >>>= shift;
 383         }


 384     }
 385 
 386     /**
 387      * Returns a {@code String} object representing the specified
 388      * {@code long}.  The argument is converted to signed decimal
 389      * representation and returned as a string, exactly as if the
 390      * argument and the radix 10 were given as arguments to the {@link
 391      * #toString(long, int)} method.
 392      *
 393      * @param   i   a {@code long} to be converted.
 394      * @return  a string representation of the argument in base&nbsp;10.
 395      */
 396     public static String toString(long i) {
 397         if (i == Long.MIN_VALUE)
 398             return "-9223372036854775808";
 399         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 400         char[] buf = new char[size];
 401         getChars(i, size, buf);
 402         return new String(buf, true);
 403     }