Fix capacity calculation in ArrayList and StringBuilder.

See #289 and #290
This commit is contained in:
Alexey Andreev 2017-06-25 13:37:15 +03:00
parent d982f89ab6
commit b477a7dcad
2 changed files with 8 additions and 2 deletions

View File

@ -581,7 +581,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
if (buffer.length >= capacity) { if (buffer.length >= capacity) {
return; return;
} }
buffer = TArrays.copyOf(buffer, capacity * 2 + 1); int newLength = buffer.length < Integer.MAX_VALUE / 2
? Math.max(capacity, Math.max(buffer.length * 2, 5))
: Integer.MAX_VALUE;
buffer = TArrays.copyOf(buffer, newLength);
} }
public void trimToSize() { public void trimToSize() {

View File

@ -53,7 +53,10 @@ public class TArrayList<E> extends TAbstractList<E> implements TCloneable, TSeri
public void ensureCapacity(int minCapacity) { public void ensureCapacity(int minCapacity) {
if (array.length < minCapacity) { if (array.length < minCapacity) {
array = TArrays.copyOf(array, array.length + TMath.max(5, array.length / 2)); int newLength = array.length < Integer.MAX_VALUE / 2
? Math.max(minCapacity, Math.max(array.length * 2, 5))
: Integer.MAX_VALUE;
array = TArrays.copyOf(array, newLength);
} }
} }