Am 31.05.2015 um 08:38 schrieb Peter Levart:
Hi,
Yes, this one is much easier to grasp.
As I understand the check is to avoid overflow in calculation of StringBuilder initial capacity
(newLenHint). If overflow happened, newLenHint would be negative and StringBuilder construction
would fail with NegativeArraySizeException. But the calculation of newLenHint:
int newLenHint = value.length - targLen + replValue.length;
...considering the following:
value.length >= 0
targLength >= 0
replValue.length >= 0
targLength <= value.length
in case of overflow, it can only produce a negative value. So the check could
simply be:
if (newLenHint < 0) {
throw new OutOfMemoryError();
}
Hm, what has this situation to do with Out-Of-Memory ?
In other words, IMHO NegativeArraySizeException is much better here and
additionally saves performance.
Additionally you could propagate it to InvalidArgumentException.
-Ulf