public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence { static final long serialVersionUID = 3388685877147921107L; public StringBuffer() { super(16); } public StringBuffer(int capacity) { super(capacity); } public StringBuffer(String str) { super(str.length() + 16); append(str); } /** *这边也是一样的 */ public StringBuffer(CharSequence seq) { this(seq.length() + 16); append(seq); } public synchronized int length() { return count; } public synchronized int capacity() { return value.length; } /** * jdk 直接对外提供的 扩容 方法 */ public synchronized void ensureCapacity(int minimumCapacity) { if (minimumCapacity > value.length) { expandCapacity(minimumCapacity); } } /** * trim to actual size 也就是 array.length ---> count */ public synchronized void trimToSize() { super.trimToSize(); }public synchronized void setLength(int newLength) { super.setLength(newLength); } public synchronized char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); return value[index]; } public synchronized int codePointAt(int index) { return super.codePointAt(index); } public synchronized int codePointBefore(int index) { return super.codePointBefore(index); } public synchronized int codePointCount(int beginIndex, int endIndex) { return super.codePointCount(beginIndex, endIndex); } public synchronized int offsetByCodePoints(int index, int codePointOffset) { return super.offsetByCodePoints(index, codePointOffset); } public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { super.getChars(srcBegin, srcEnd, dst, dstBegin); } public synchronized void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); value[index] = ch; } /** * append */ public synchronized StringBuffer append(Object obj) { super.append(String.valueOf(obj)); return this; } public synchronized StringBuffer appendCodePoint(int codePoint) { super.appendCodePoint(codePoint); return this; } public synchronized StringBuffer delete(int start, int end) { super.delete(start, end); return this; } public synchronized StringBuffer replace(int start, int end, String str) { super.replace(start, end, str); return this; } public synchronized CharSequence subSequence(int start, int end) { return super.substring(start, end); } public synchronized String substring(int start, int end) { return super.substring(start, end); } public synchronized StringBuffer insert(int index, char str[], int offset, int len) { super.insert(index, str, offset, len); return this; } public synchronized StringBuffer insert(int offset, String str) { super.insert(offset, str); return this; } public synchronized StringBuffer insert(int offset, char str[]) { super.insert(offset, str); return this; } public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end) { super.insert(dstOffset, s, start, end); return this; } /** * 默认检索起始位置 为 0 */ public int indexOf(String str) { return indexOf(str, 0); } public synchronized int indexOf(String str, int fromIndex) { return String.indexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } public int lastIndexOf(String str) { // Note, synchronization achieved via other invocations return lastIndexOf(str, count); } public synchronized int lastIndexOf(String str, int fromIndex) { return String.lastIndexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } /** * reverse 和 replace 这些和 顺序 有关的方法 * 在 Collections 里边也有 只不过 只支持 List 实现类(与顺序相关) */ public synchronized StringBuffer reverse() { super.reverse(); return this; } public synchronized String toString() { return new String(value, 0, count); } private static final java.io.ObjectStreamField[] serialPersistentFields = { new java.io.ObjectStreamField("value", char[].class), new java.io.ObjectStreamField("count", Integer.TYPE), new java.io.ObjectStreamField("shared", Boolean.TYPE), }; private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { java.io.ObjectOutputStream.PutField fields = s.putFields(); fields.put("value", value); fields.put("count", count); fields.put("shared", false); s.writeFields(); } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { java.io.ObjectInputStream.GetField fields = s.readFields(); value = (char[])fields.get("value", null); count = (int)fields.get("count", 0); } }
[源码]String StringBuffer StringBudlider(2)StringBuffer StringBuilder源码分析
原文:http://www.cnblogs.com/zongjihengfei/p/6005377.html