Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/java.base/share/classes/java/lang/AbstractStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,30 @@ public void ensureCapacity(int minimumCapacity) {
*/
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
int oldCapacity = value.length >> coder;
ensureCapacityInternal(minimumCapacity, coder);
}

/**
* For positive values of {@code minimumCapacity}, this method
* behaves like {@code ensureCapacity}, however it is never
* synchronized.
* If {@code minimumCapacity} is non positive due to numeric
* overflow, this method throws {@code OutOfMemoryError}.
*
* @param minimumCapacity the minimum desired capacity.
* @param coder the coder to be used when calculating the capacity length.
* @return Returns the value that ensures the capacity
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complete the javadoc with the @param tags and descriptions.

private byte[] ensureCapacityInternal(int minimumCapacity, byte coder) {
// overflow-conscious code
byte[] val = this.value;
int oldCapacity = val.length >> coder;
if (minimumCapacity - oldCapacity > 0) {
value = Arrays.copyOf(value,
newCapacity(minimumCapacity) << coder);
val = Arrays.copyOf(val,
newCapacity(minimumCapacity, val.length, coder) << coder);
this.value = val;
}
return val;
}

/**
Expand All @@ -257,11 +276,12 @@ private void ensureCapacityInternal(int minimumCapacity) {
* unless the given minimum capacity is greater than that.
*
* @param minCapacity the desired minimum capacity
* @param oldLength the raw length of the current value array, without being processed using coder.
* @param coder the coder to be used when calculating the capacity length
* @throws OutOfMemoryError if minCapacity is less than zero or
* greater than (Integer.MAX_VALUE >> coder)
*/
private int newCapacity(int minCapacity) {
int oldLength = value.length;
private static int newCapacity(int minCapacity, int oldLength, byte coder) {
int newLength = minCapacity << coder;
int growth = newLength - oldLength;
int length = ArraysSupport.newLength(oldLength, growth, oldLength + (2 << coder));
Expand Down Expand Up @@ -838,8 +858,9 @@ public AbstractStringBuilder append(char c) {
public AbstractStringBuilder append(int i) {
int count = this.count;
int spaceNeeded = count + DecimalDigits.stringSize(i);
ensureCapacityInternal(spaceNeeded);
if (isLatin1()) {
byte coder = this.coder;
byte[] value = ensureCapacityInternal(spaceNeeded, coder);
if (coder == LATIN1) {
DecimalDigits.getCharsLatin1(i, spaceNeeded, value);
} else {
DecimalDigits.getCharsUTF16(i, spaceNeeded, value);
Expand All @@ -863,8 +884,9 @@ public AbstractStringBuilder append(int i) {
public AbstractStringBuilder append(long l) {
int count = this.count;
int spaceNeeded = count + DecimalDigits.stringSize(l);
ensureCapacityInternal(spaceNeeded);
if (isLatin1()) {
byte coder = this.coder;
byte[] value = ensureCapacityInternal(spaceNeeded, coder);
if (coder == LATIN1) {
DecimalDigits.getCharsLatin1(l, spaceNeeded, value);
} else {
DecimalDigits.getCharsUTF16(l, spaceNeeded, value);
Expand Down