// When parsing a number with big exponential value, it requires to transform the
// value into a string representation to construct BigInteger instance. We want to
// set the maximum size because it can easily trigger OutOfMemoryException.
- // PARSE_MAX_EXPONENT is currently set to 1000, which is much bigger than MAX_VALUE of
- // Double ( See the problem reported by ticket#5698
- private static final int PARSE_MAX_EXPONENT = 1000;
+ // PARSE_MAX_EXPONENT is currently set to 1000 (See getParseMaxDigits()),
+ // which is much bigger than MAX_VALUE of Double ( See the problem reported by ticket#5698
+ private int PARSE_MAX_EXPONENT = 1000;
/**
* Parses the given text into a number. The text is parsed beginning at parsePosition,
// Adjust for exponent, if any
exponent += digits.decimalAt;
- if (exponent < -PARSE_MAX_EXPONENT) {
+ if (exponent < -getParseMaxDigits()) {
status[STATUS_UNDERFLOW] = true;
- } else if (exponent > PARSE_MAX_EXPONENT) {
+ } else if (exponent > getParseMaxDigits()) {
status[STATUS_INFINITE] = true;
} else {
digits.decimalAt = (int) exponent;
public boolean isParseBigDecimal() {
return parseBigDecimal;
}
+
+ /**
+ * Set the maximum number of exponent digits when parsing a number.
+ * If the limit is set too high, an OutOfMemoryException may be triggered.
+ * The default value is 1000.
+ * @param newValue the new limit
+ * @draft ICU 51
+ */
+ public void setParseMaxDigits(int newValue) {
+ if (newValue > 0) {
+ PARSE_MAX_EXPONENT = newValue;
+ }
+ }
+
+ /**
+ * Get the current maximum number of exponent digits when parsing a
+ * number.
+ *
+ * @draft ICU 51
+ */
+ public int getParseMaxDigits() {
+ return PARSE_MAX_EXPONENT;
+ }
private void writeObject(ObjectOutputStream stream) throws IOException {
// Ticket#6449 Format.Field instances are not serializable. When
}
}
}
+
+ public void TestParseMaxDigits() {
+ DecimalFormat fmt = new DecimalFormat();
+ String number = "100000000000";
+ int newParseMax = number.length() - 1;
+
+ fmt.setParseMaxDigits(-1);
+
+ /* Default value is 1000 */
+ if (fmt.getParseMaxDigits() != 1000) {
+ errln("Fail valid value checking in setParseMaxDigits.");
+ }
+
+ try {
+ if (fmt.parse(number).doubleValue() == Float.POSITIVE_INFINITY) {
+ errln("Got Infinity but should NOT when parsing number: " + number);
+ }
+
+ fmt.setParseMaxDigits(newParseMax);
+
+ if (fmt.parse(number).doubleValue() != Float.POSITIVE_INFINITY) {
+ errln("Did not get Infinity but should when parsing number: " + number);
+ }
+ } catch (ParseException ex) {
+
+ }
+ }
private static class FormatCharItrTestThread implements Runnable {
private final NumberFormat fmt;