< prev index next >
src/java.base/share/classes/java/lang/StringBuffer.java
Print this page
rev 13124 : [mq]: XXXXXXX-ASB-shared-value
@@ -98,16 +98,10 @@
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
- /**
- * A cache of the last value returned by toString. Cleared
- * whenever the StringBuffer is modified.
- */
- private transient String toStringCache;
-
/** use serialVersionUID from JDK 1.0.2 for interoperability */
static final long serialVersionUID = 3388685877147921107L;
/**
* Constructs a string buffer with no characters in it and an
@@ -190,11 +184,10 @@
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
@Override
public synchronized void setLength(int newLength) {
- toStringCache = null;
super.setLength(newLength);
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
@@ -255,25 +248,22 @@
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
@Override
public synchronized void setCharAt(int index, char ch) {
- toStringCache = null;
super.setCharAt(index, ch);
}
@Override
public synchronized StringBuffer append(Object obj) {
- toStringCache = null;
super.append(String.valueOf(obj));
return this;
}
@Override
@HotSpotIntrinsicCandidate
public synchronized StringBuffer append(String str) {
- toStringCache = null;
super.append(str);
return this;
}
/**
@@ -299,21 +289,19 @@
* @param sb the {@code StringBuffer} to append.
* @return a reference to this object.
* @since 1.4
*/
public synchronized StringBuffer append(StringBuffer sb) {
- toStringCache = null;
super.append(sb);
return this;
}
/**
* @since 1.8
*/
@Override
synchronized StringBuffer append(AbstractStringBuilder asb) {
- toStringCache = null;
super.append(asb);
return this;
}
/**
@@ -337,11 +325,10 @@
* @return a reference to this object.
* @since 1.5
*/
@Override
public synchronized StringBuffer append(CharSequence s) {
- toStringCache = null;
super.append(s);
return this;
}
/**
@@ -349,115 +336,102 @@
* @since 1.5
*/
@Override
public synchronized StringBuffer append(CharSequence s, int start, int end)
{
- toStringCache = null;
super.append(s, start, end);
return this;
}
@Override
public synchronized StringBuffer append(char[] str) {
- toStringCache = null;
super.append(str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public synchronized StringBuffer append(char[] str, int offset, int len) {
- toStringCache = null;
super.append(str, offset, len);
return this;
}
@Override
public synchronized StringBuffer append(boolean b) {
- toStringCache = null;
super.append(b);
return this;
}
@Override
@HotSpotIntrinsicCandidate
public synchronized StringBuffer append(char c) {
- toStringCache = null;
super.append(c);
return this;
}
@Override
@HotSpotIntrinsicCandidate
public synchronized StringBuffer append(int i) {
- toStringCache = null;
super.append(i);
return this;
}
/**
* @since 1.5
*/
@Override
public synchronized StringBuffer appendCodePoint(int codePoint) {
- toStringCache = null;
super.appendCodePoint(codePoint);
return this;
}
@Override
public synchronized StringBuffer append(long lng) {
- toStringCache = null;
super.append(lng);
return this;
}
@Override
public synchronized StringBuffer append(float f) {
- toStringCache = null;
super.append(f);
return this;
}
@Override
public synchronized StringBuffer append(double d) {
- toStringCache = null;
super.append(d);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
@Override
public synchronized StringBuffer delete(int start, int end) {
- toStringCache = null;
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
@Override
public synchronized StringBuffer deleteCharAt(int index) {
- toStringCache = null;
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
@Override
public synchronized StringBuffer replace(int start, int end, String str) {
- toStringCache = null;
super.replace(start, end, str);
return this;
}
/**
@@ -493,41 +467,37 @@
*/
@Override
public synchronized StringBuffer insert(int index, char[] str, int offset,
int len)
{
- toStringCache = null;
super.insert(index, str, offset, len);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public synchronized StringBuffer insert(int offset, Object obj) {
- toStringCache = null;
super.insert(offset, String.valueOf(obj));
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public synchronized StringBuffer insert(int offset, String str) {
- toStringCache = null;
super.insert(offset, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public synchronized StringBuffer insert(int offset, char[] str) {
- toStringCache = null;
super.insert(offset, str);
return this;
}
/**
@@ -536,11 +506,10 @@
*/
@Override
public StringBuffer insert(int dstOffset, CharSequence s) {
// Note, synchronization achieved via invocations of other StringBuffer methods
// after narrowing of s to specific type
- // Ditto for toStringCache clearing
super.insert(dstOffset, s);
return this;
}
/**
@@ -549,11 +518,10 @@
*/
@Override
public synchronized StringBuffer insert(int dstOffset, CharSequence s,
int start, int end)
{
- toStringCache = null;
super.insert(dstOffset, s, start, end);
return this;
}
/**
@@ -561,21 +529,19 @@
*/
@Override
public StringBuffer insert(int offset, boolean b) {
// Note, synchronization achieved via invocation of StringBuffer insert(int, String)
// after conversion of b to String by super class method
- // Ditto for toStringCache clearing
super.insert(offset, b);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public synchronized StringBuffer insert(int offset, char c) {
- toStringCache = null;
super.insert(offset, c);
return this;
}
/**
@@ -583,11 +549,10 @@
*/
@Override
public StringBuffer insert(int offset, int i) {
// Note, synchronization achieved via invocation of StringBuffer insert(int, String)
// after conversion of i to String by super class method
- // Ditto for toStringCache clearing
super.insert(offset, i);
return this;
}
/**
@@ -595,11 +560,10 @@
*/
@Override
public StringBuffer insert(int offset, long l) {
// Note, synchronization achieved via invocation of StringBuffer insert(int, String)
// after conversion of l to String by super class method
- // Ditto for toStringCache clearing
super.insert(offset, l);
return this;
}
/**
@@ -607,11 +571,10 @@
*/
@Override
public StringBuffer insert(int offset, float f) {
// Note, synchronization achieved via invocation of StringBuffer insert(int, String)
// after conversion of f to String by super class method
- // Ditto for toStringCache clearing
super.insert(offset, f);
return this;
}
/**
@@ -619,11 +582,10 @@
*/
@Override
public StringBuffer insert(int offset, double d) {
// Note, synchronization achieved via invocation of StringBuffer insert(int, String)
// after conversion of d to String by super class method
- // Ditto for toStringCache clearing
super.insert(offset, d);
return this;
}
/**
@@ -663,24 +625,26 @@
/**
* @since 1.0.2
*/
@Override
public synchronized StringBuffer reverse() {
- toStringCache = null;
super.reverse();
return this;
}
@Override
@HotSpotIntrinsicCandidate
public synchronized String toString() {
- if (toStringCache == null) {
- return toStringCache =
- isLatin1() ? StringLatin1.newString(value, 0, count)
- : StringUTF16.newString(value, 0, count);
+ final byte[] value = this.value;
+ if (isLatin1()) {
+ if ((count << coder) < value.length) {
+ return StringLatin1.newString(value, 0, count);
+ }
+ shared = true;
+ return new String(value, String.LATIN1);
}
- return new String(toStringCache);
+ return StringUTF16.newString(value, 0, count);
}
/**
* Serializable fields for StringBuffer.
*
@@ -726,10 +690,11 @@
throws java.io.IOException, ClassNotFoundException {
java.io.ObjectInputStream.GetField fields = s.readFields();
char[] val = (char[])fields.get("value", null);
initBytes(val, 0, val.length);
count = fields.get("count", 0);
+ shared = false;
}
synchronized void getBytes(byte dst[], int dstBegin, byte coder) {
super.getBytes(dst, dstBegin, coder);
}
< prev index next >