src/share/classes/java/lang/StringBuffer.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File
*** old/src/share/classes/java/lang/StringBuffer.java	Mon May 13 00:27:11 2013
--- new/src/share/classes/java/lang/StringBuffer.java	Mon May 13 00:27:09 2013

*** 1,7 **** --- 1,7 ---- /* ! * 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 23,32 **** --- 23,33 ---- * questions. */ package java.lang; + import java.util.Arrays; /** * A thread-safe, mutable sequence of characters. * A string buffer is like a {@link String}, but can be modified. At any * point in time it contains some particular sequence of characters, but
*** 96,105 **** --- 97,112 ---- 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 char[] 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
*** 182,191 **** --- 189,199 ---- * @see #length() */ @Override public synchronized void setLength(int newLength) { super.setLength(newLength); + toStringCache = null; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length()
*** 246,266 **** --- 254,277 ---- @Override public synchronized void setCharAt(int index, char ch) { 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; } /** * Appends the specified {@code StringBuffer} to this sequence.
*** 286,304 **** --- 297,317 ---- * @return a reference to this object. * @since 1.4 */ public synchronized StringBuffer append(StringBuffer sb) { super.append(sb); + toStringCache = null; return this; } /** * @since 1.8 */ @Override synchronized StringBuffer append(AbstractStringBuilder asb) { super.append(asb); + toStringCache = null; return this; } /** * Appends the specified {@code CharSequence} to this
*** 323,332 **** --- 336,346 ---- */ @Override 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; } /**
*** 335,434 **** --- 349,461 ---- */ @Override 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; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @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; } /** * @since 1.5 */ @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; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer delete(int start, int end) { super.delete(start, end); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer deleteCharAt(int index) { super.deleteCharAt(index); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer replace(int start, int end, String str) { super.replace(start, end, str); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc}
*** 464,500 **** --- 491,531 ---- @Override public synchronized StringBuffer insert(int index, char[] str, int offset, int len) { super.insert(index, str, offset, len); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, Object obj) { super.insert(offset, String.valueOf(obj)); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, String str) { super.insert(offset, str); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, char[] str) { super.insert(offset, str); + toStringCache = null; return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc}
*** 502,511 **** --- 533,543 ---- */ @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; } /**
*** 515,554 **** --- 547,590 ---- @Override public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end) { super.insert(dstOffset, s, start, end); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @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) { super.insert(offset, c); + toStringCache = null; return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @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; } /**
*** 556,565 **** --- 592,602 ---- */ @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; } /**
*** 567,576 **** --- 604,614 ---- */ @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; } /**
*** 578,587 **** --- 616,626 ---- */ @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; } /**
*** 622,637 **** --- 661,680 ---- * @since JDK1.0.2 */ @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 = Arrays.copyOfRange(value, 0, count); + } + return new String(toStringCache, true); } /** * Serializable fields for StringBuffer. *

src/share/classes/java/lang/StringBuffer.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File