Summary
Reduce the maximum fraction digits that an empty pattern DecimalFormat is initialized with.
Problem
An empty pattern DecimalFormat is initialized with Integer.MAX_VALUE
digits for the maximum fraction digits. When toPattern()
is invoked, a StringBuilder
will append digits up to Integer.MAX_VALUE
and internally double capacity until an OutOfMemoryError
occurs. Additionally, DecimalFormat::toString
utilizes toPattern()
, which means that if toString()
is invoked on an empty pattern DecimalFormat, an OutOfMemoryError
occurs in this case as well.
Solution
Restrict the initial maximum fraction digits for an empty pattern from Integer.MAX_VALUE
to the private constant variable, DecimalFormat.DOUBLE_FRACTION_DIGITS
. This allows for empty pattern DecimalFormat's to no longer cause an OutOfMemoryError
, as the value is reduced to 340. This value is a reasonable choice, serving as the upper fraction limit for a Java double.
Specification
N/A (Behavioral change only)
- csr of
-
JDK-8326908 DecimalFormat::toPattern throws OutOfMemoryError when pattern is empty string
- Resolved