Apache Harmony 实现依赖于AbstractStringBuilder 中的方法来管理添加/删除(StringBuffer 扩展AbstractStringBuilder)。
AbstractStringBuilder 保留一个字符缓冲区(即chars 的数组)来保存当前的“字符串”。当将任何对象的下一个字符串表示附加到此缓冲区时,它会检查缓冲区是否包含足够的空间,如果没有足够的空间,它会分配一个新的字符缓冲区,复制旧缓冲区,然后将新字符串添加到该缓冲区。我们可以从enlargeBuffer 的内部收集到这一点:
private void enlargeBuffer(int min) {
int newSize = ((value.length >> 1) + value.length) + 2;
char[] newData = new char[min > newSize ? min : newSize];
System.arraycopy(value, 0, newData, 0, count);
value = newData;
shared = false;
}
...当value(保存字符缓冲区的私有成员)的容量将被超出时,在任何附加方法中都会调用此方法:
final void append0(char chars[]) {
int newSize = count + chars.length;
if (newSize > value.length) {
enlargeBuffer(newSize);
}
System.arraycopy(chars, 0, value, count, chars.length);
count = newSize;
}
标准的 OpenJDK 实现非常相似。同样,StringBuffer 依赖于AbstractStringBuilder:
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
}
注意Arrays.copyOf 复制字符数组value 用空字符填充它以获得总大小newCapacity,这基本上相当于Harmony 方法中的new char[...] 调用。同样,当没有足够的空间添加下一个字符串段时,将调用expandCapacity 方法:
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}