From f697e160a17bd4e0166dd82f7a48ca9601173c87 Mon Sep 17 00:00:00 2001 From: Claes Redestad Date: Fri, 18 Jul 2014 22:32:24 +0200 Subject: [PATCH] 8050114: Expose Integer/Long formatUnsigned methods internally Reviewed-by: mduigou --- jdk/src/share/classes/java/lang/Integer.java | 19 +++-- jdk/src/share/classes/java/lang/Long.java | 19 +++-- jdk/src/share/classes/java/lang/System.java | 6 ++ .../classes/sun/misc/JavaLangAccess.java | 10 +++ .../misc/JavaLangAccess/FormatUnsigned.java | 78 +++++++++++++++++++ 5 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 jdk/test/sun/misc/JavaLangAccess/FormatUnsigned.java diff --git a/jdk/src/share/classes/java/lang/Integer.java b/jdk/src/share/classes/java/lang/Integer.java index 626fa337528..94b0aff7970 100644 --- a/jdk/src/share/classes/java/lang/Integer.java +++ b/jdk/src/share/classes/java/lang/Integer.java @@ -320,24 +320,27 @@ public final class Integer extends Number implements Comparable { } /** - * Format a long (treated as unsigned) into a character buffer. + * Format an {@code int} (treated as unsigned) into a character buffer. If + * {@code len} exceeds the formatted ASCII representation of {@code val}, + * {@code buf} will be padded with leading zeroes. + * * @param val the unsigned int to format * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) * @param buf the character buffer to write to * @param offset the offset in the destination buffer to start at * @param len the number of characters to write - * @return the lowest character location used */ - static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { - int charPos = len; + static void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { + // assert shift > 0 && shift <=5 : "Illegal shift value"; + // assert offset >= 0 && offset < buf.length : "illegal offset"; + // assert len > 0 && (offset + len) <= buf.length : "illegal length"; + int charPos = offset + len; int radix = 1 << shift; int mask = radix - 1; do { - buf[offset + --charPos] = Integer.digits[val & mask]; + buf[--charPos] = Integer.digits[val & mask]; val >>>= shift; - } while (val != 0 && charPos > 0); - - return charPos; + } while (charPos > offset); } final static char [] DigitTens = { diff --git a/jdk/src/share/classes/java/lang/Long.java b/jdk/src/share/classes/java/lang/Long.java index 71548bb7fd8..76caa77fac7 100644 --- a/jdk/src/share/classes/java/lang/Long.java +++ b/jdk/src/share/classes/java/lang/Long.java @@ -361,24 +361,27 @@ public final class Long extends Number implements Comparable { } /** - * Format a long (treated as unsigned) into a character buffer. + * Format a long (treated as unsigned) into a character buffer. If + * {@code len} exceeds the formatted ASCII representation of {@code val}, + * {@code buf} will be padded with leading zeroes. + * * @param val the unsigned long to format * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) * @param buf the character buffer to write to * @param offset the offset in the destination buffer to start at * @param len the number of characters to write - * @return the lowest character location used */ - static int formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { - int charPos = len; + static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { + // assert shift > 0 && shift <=5 : "Illegal shift value"; + // assert offset >= 0 && offset < buf.length : "illegal offset"; + // assert len > 0 && (offset + len) <= buf.length : "illegal length"; + int charPos = offset + len; int radix = 1 << shift; int mask = radix - 1; do { - buf[offset + --charPos] = Integer.digits[((int) val) & mask]; + buf[--charPos] = Integer.digits[((int) val) & mask]; val >>>= shift; - } while (val != 0 && charPos > 0); - - return charPos; + } while (charPos > offset); } /** diff --git a/jdk/src/share/classes/java/lang/System.java b/jdk/src/share/classes/java/lang/System.java index e96bd3b6f14..fbac697c245 100644 --- a/jdk/src/share/classes/java/lang/System.java +++ b/jdk/src/share/classes/java/lang/System.java @@ -1263,6 +1263,12 @@ public final class System { public void invokeFinalize(Object o) throws Throwable { o.finalize(); } + public void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { + Long.formatUnsignedLong(val, shift, buf, offset, len); + } + public void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { + Integer.formatUnsignedInt(val, shift, buf, offset, len); + } }); } } diff --git a/jdk/src/share/classes/sun/misc/JavaLangAccess.java b/jdk/src/share/classes/sun/misc/JavaLangAccess.java index 812f88e2098..a86453df430 100644 --- a/jdk/src/share/classes/sun/misc/JavaLangAccess.java +++ b/jdk/src/share/classes/sun/misc/JavaLangAccess.java @@ -132,4 +132,14 @@ public interface JavaLangAccess { * Invokes the finalize method of the given object. */ void invokeFinalize(Object o) throws Throwable; + + /** + * Invokes Long.formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) + */ + void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len); + + /** + * Invokes Integer.formatUnsignedInt(long val, int shift, char[] buf, int offset, int len) + */ + void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len); } diff --git a/jdk/test/sun/misc/JavaLangAccess/FormatUnsigned.java b/jdk/test/sun/misc/JavaLangAccess/FormatUnsigned.java new file mode 100644 index 00000000000..ff3411af244 --- /dev/null +++ b/jdk/test/sun/misc/JavaLangAccess/FormatUnsigned.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import sun.misc.JavaLangAccess; +import sun.misc.SharedSecrets; + +/* + * @test + * @summary Test JavaLangAccess.formatUnsignedInt/-Long + * @bug 8050114 + */ +public class FormatUnsigned { + + static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess(); + + public static void testFormatUnsignedInt() { + testFormatUnsignedInt("7fffffff", Integer.MAX_VALUE, 8, 4, 0, 8); + testFormatUnsignedInt("80000000", Integer.MIN_VALUE, 8, 4, 0, 8); + testFormatUnsignedInt("4711", 04711, 4, 3, 0, 4); + testFormatUnsignedInt("4711", 0x4711, 4, 4, 0, 4); + testFormatUnsignedInt("1010", 0b1010, 4, 1, 0, 4); + testFormatUnsignedInt("00001010", 0b1010, 8, 1, 0, 8); + testFormatUnsignedInt("\u0000\u000000001010", 0b1010, 10, 1, 2, 8); + } + + public static void testFormatUnsignedLong() { + testFormatUnsignedLong("7fffffffffffffff", Long.MAX_VALUE, 16, 4, 0, 16); + testFormatUnsignedLong("8000000000000000", Long.MIN_VALUE, 16, 4, 0, 16); + testFormatUnsignedLong("4711", 04711L, 4, 3, 0, 4); + testFormatUnsignedLong("4711", 0x4711L, 4, 4, 0, 4); + testFormatUnsignedLong("1010", 0b1010L, 4, 1, 0, 4); + testFormatUnsignedLong("00001010", 0b1010L, 8, 1, 0, 8); + testFormatUnsignedLong("\u0000\u000000001010", 0b1010L, 10, 1, 2, 8); + } + + public static void testFormatUnsignedInt(String expected, int value, int arraySize, int shift, int offset, int length) { + char[] chars = new char[arraySize]; + jla.formatUnsignedInt(value, shift, chars, offset, length); + String s = new String(chars); + if (!expected.equals(s)) { + throw new Error(s + " should be equal to expected " + expected); + } + } + + public static void testFormatUnsignedLong(String expected, long value, int arraySize, int shift, int offset, int length) { + char[] chars = new char[arraySize]; + jla.formatUnsignedLong(value, shift, chars, offset, length); + String s = new String(chars); + if (!expected.equals(s)) { + throw new Error(s + " should be equal to expected " + expected); + } + } + + public static void main(String[] args) { + testFormatUnsignedInt(); + testFormatUnsignedLong(); + } +}