--- old/src/share/classes/java/lang/StringBuffer.java 2013-05-10 01:10:13.398680589 -0400 +++ new/src/share/classes/java/lang/StringBuffer.java 2013-05-10 01:10:12.126609573 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -98,6 +98,12 @@ 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; @@ -184,6 +190,7 @@ @Override public synchronized void setLength(int newLength) { super.setLength(newLength); + toStringCache = null; } /** @@ -248,17 +255,20 @@ if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); value[index] = ch; + toStringCache = null; } @Override public synchronized StringBuffer append(Object obj) { super.append(String.valueOf(obj)); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(String str) { super.append(str); + toStringCache = null; return this; } @@ -288,6 +298,7 @@ */ public synchronized StringBuffer append(StringBuffer sb) { super.append(sb); + toStringCache = null; return this; } @@ -297,6 +308,7 @@ @Override synchronized StringBuffer append(AbstractStringBuilder asb) { super.append(asb); + toStringCache = null; return this; } @@ -325,6 +337,7 @@ public StringBuffer append(CharSequence s) { // Note, synchronization achieved via invocations of other StringBuffer methods after // narrowing of s to specific type + // Ditto for toStringCache clearing super.append(s); return this; } @@ -337,12 +350,14 @@ public synchronized StringBuffer append(CharSequence s, int start, int end) { super.append(s, start, end); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(char[] str) { super.append(str); + toStringCache = null; return this; } @@ -352,24 +367,28 @@ @Override public synchronized StringBuffer append(char[] str, int offset, int len) { super.append(str, offset, len); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(boolean b) { super.append(b); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(char c) { super.append(c); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(int i) { super.append(i); + toStringCache = null; return this; } @@ -379,24 +398,28 @@ @Override public synchronized StringBuffer appendCodePoint(int codePoint) { super.appendCodePoint(codePoint); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(long lng) { super.append(lng); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(float f) { super.append(f); + toStringCache = null; return this; } @Override public synchronized StringBuffer append(double d) { super.append(d); + toStringCache = null; return this; } @@ -407,6 +430,7 @@ @Override public synchronized StringBuffer delete(int start, int end) { super.delete(start, end); + toStringCache = null; return this; } @@ -417,6 +441,7 @@ @Override public synchronized StringBuffer deleteCharAt(int index) { super.deleteCharAt(index); + toStringCache = null; return this; } @@ -427,6 +452,7 @@ @Override public synchronized StringBuffer replace(int start, int end, String str) { super.replace(start, end, str); + toStringCache = null; return this; } @@ -466,6 +492,7 @@ int len) { super.insert(index, str, offset, len); + toStringCache = null; return this; } @@ -475,6 +502,7 @@ @Override public synchronized StringBuffer insert(int offset, Object obj) { super.insert(offset, String.valueOf(obj)); + toStringCache = null; return this; } @@ -484,6 +512,7 @@ @Override public synchronized StringBuffer insert(int offset, String str) { super.insert(offset, str); + toStringCache = null; return this; } @@ -493,6 +522,7 @@ @Override public synchronized StringBuffer insert(int offset, char[] str) { super.insert(offset, str); + toStringCache = null; return this; } @@ -504,6 +534,7 @@ 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; } @@ -517,6 +548,7 @@ int start, int end) { super.insert(dstOffset, s, start, end); + toStringCache = null; return this; } @@ -527,6 +559,7 @@ 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; } @@ -537,6 +570,7 @@ @Override public synchronized StringBuffer insert(int offset, char c) { super.insert(offset, c); + toStringCache = null; return this; } @@ -547,6 +581,7 @@ 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; } @@ -558,6 +593,7 @@ 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; } @@ -569,6 +605,7 @@ 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; } @@ -580,6 +617,7 @@ 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; } @@ -624,12 +662,18 @@ @Override public synchronized StringBuffer reverse() { super.reverse(); + toStringCache = null; return this; } @Override public synchronized String toString() { - return new String(value, 0, count); + if (toStringCache == null) { + toStringCache = new String(value, 0, count); + return toStringCache; + } else { + return new String(toStringCache); + } } /**