changeset 7427:308d1362b60a

6471906: java.lang.NegativeArraySizeException in tenToThe Reviewed-by: darcy Contributed-by: brian.burkhalter@oracle.com
author dmeetry
date Sun, 03 Feb 2013 18:20:24 +0400
parents 25831e7009c4
children 962d6612cace
files src/share/classes/java/math/BigDecimal.java
diffstat 1 files changed, 19 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/math/BigDecimal.java	Sat Feb 02 12:08:43 2013 -0800
+++ b/src/share/classes/java/math/BigDecimal.java	Sun Feb 03 18:20:24 2013 +0400
@@ -3537,13 +3537,25 @@
             else
                 return expandBigIntegerTenPowers(n);
         }
-        // BigInteger.pow is slow, so make 10**n by constructing a
-        // BigInteger from a character string (still not very fast)
-        char tenpow[] = new char[n + 1];
-        tenpow[0] = '1';
-        for (int i = 1; i <= n; i++)
-            tenpow[i] = '0';
-        return new BigInteger(tenpow,1, tenpow.length);
+
+        if (n < 1024*524288) {
+            // BigInteger.pow is slow, so make 10**n by constructing a
+            // BigInteger from a character string (still not very fast)
+            // which occupies no more than 1GB (!) of memory.
+            char tenpow[] = new char[n + 1];
+            tenpow[0] = '1';
+            for (int i = 1; i <= n; i++) {
+                tenpow[i] = '0';
+            }
+            return new BigInteger(tenpow, 1, tenpow.length);
+        }
+
+        if ((n & 0x1) == 0x1) {
+            return BigInteger.TEN.multiply(bigTenToThe(n - 1));
+        } else {
+            BigInteger tmp = bigTenToThe(n/2);
+            return tmp.multiply(tmp);
+        }
     }
 
     /**