StringBuffer 源代码

StringBuffer.java

001	/**
002	 * @(#)StringBuffer.java    1.101 05/11/17
003	 *
004	 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
005	 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
006	 */
007	 
008	package java.lang;

059	 *
060	 * @author  Arthur van Hoff
061	 * @version     1.101, 11/17/05
062	 * @see     java.lang.StringBuilder
063	 * @see     java.lang.String
064	 * @since   JDK1.0
065	 */
066	 public final class StringBuffer
067	    extends AbstractStringBuilder
068	    implements java.io.Serializable, CharSequence
069	{
070	 
071	    /*** use serialVersionUID from JDK 1.0.2 for interoperability */
072	    static final long serialVersionUID = 3388685877147921107L;
073	 
078	    public StringBuffer() {
079	    super(16);
080	    }
081	 
090	    public StringBuffer(int capacity) {
091	    super(capacity);
092	    }
093	 
102	    public StringBuffer(String str) {
103	    super(str.length() + 16);
104	    append(str);
105	    }
106	 
121	    public StringBuffer(CharSequence seq) {
122	        this(seq.length() + 16);
123	        append(seq);
124	    }
125	 
126	    public synchronized int length() {
127	    return count;
128	    }
129	 
130	    public synchronized int capacity() {
131	    return value.length;
132	    }
133	 
134	 
135	    public synchronized void ensureCapacity(int minimumCapacity) {
136	    if (minimumCapacity > value.length) {
137	        expandCapacity(minimumCapacity);
138	    }
139	    }
140	 
144	    public synchronized void trimToSize() {
145	        super.trimToSize();
146	    }
147	 
152	    public synchronized void setLength(int newLength) {
153	    super.setLength(newLength);
154	    }
155	 
160	    public synchronized char charAt(int index) {
161	    if ((index < 0) || (index >= count))
162	        throw new StringIndexOutOfBoundsException(index);
163	    return value[index];
164	    }
165	 
169	    public synchronized int codePointAt(int index) {
170	        return super.codePointAt(index);
171	    }
172	 
176	    public synchronized int codePointBefore(int index) {
177	        return super.codePointBefore(index);
178	    }
179	 
183	    public synchronized int codePointCount(int beginIndex, int endIndex) {
184	    return super.codePointCount(beginIndex, endIndex);
185	    }
186	 
190	    public synchronized int offsetByCodePoints(int index, int codePointOffset) {
191	    return super.offsetByCodePoints(index, codePointOffset);
192	    }
193	 
198	    public synchronized void getChars(int srcBegin, int srcEnd, char dst[],
199	                                      int dstBegin)
200	    {
201	    super.getChars(srcBegin, srcEnd, dst, dstBegin);
202	    }
203	 
208	    public synchronized void setCharAt(int index, char ch) {
209	    if ((index < 0) || (index >= count))
210	        throw new StringIndexOutOfBoundsException(index);
211	    value[index] = ch;
212	    }
213	 
218	    public synchronized StringBuffer append(Object obj) {
219	    super.append(String.valueOf(obj));
220	        return this;
221	    }
222	 
223	    public synchronized StringBuffer append(String str) {
224	    super.append(str);
225	        return this;
226	    }
227	 
252	    public synchronized StringBuffer append(StringBuffer sb) {
253	        super.append(sb);
254	        return this;
255	    }

279	    public StringBuffer append(CharSequence s) {
280	        // Note, synchronization achieved via other invocations
281	        if (s == null)
282	            s = "null";
283	        if (s instanceof String)
284	            return this.append((String)s);
285	        if (s instanceof StringBuffer)
286	            return this.append((StringBuffer)s);
287	        return this.append(s, 0, s.length());
288	    }
289	 
294	    public synchronized StringBuffer append(CharSequence s, int start, int end)
295	    {
296	        super.append(s, start, end);
297	        return this;
298	    }
299	 
300	    public synchronized StringBuffer append(char str[]) {
301	        super.append(str);
302	        return this;
303	    }
304	 
305	    public synchronized StringBuffer append(char str[], int offset, int len) {
306	        super.append(str, offset, len);
307	        return this;
308	    }
309	 
314	    public synchronized StringBuffer append(boolean b) {
315	        super.append(b);
316	        return this;
317	    }
318	 
319	    public synchronized StringBuffer append(char c) {
320	        super.append(c);
321	        return this;
322	    }

328	    public synchronized StringBuffer append(int i) {
329	    super.append(i);
330	        return this;
331	    }
332	 
336	    public synchronized StringBuffer appendCodePoint(int codePoint) {
337	    super.appendCodePoint(codePoint);
338	    return this;
339	    }
340	 
345	    public synchronized StringBuffer append(long lng) {
346	        super.append(lng);
347	    return this;
348	    }
349	 
354	    public synchronized StringBuffer append(float f) {
355	    super.append(f);
356	    return this;
357	    }
358	 
363	    public synchronized StringBuffer append(double d) {
364	    super.append(d);
365	    return this;
366	    }
367	 
372	    public synchronized StringBuffer delete(int start, int end) {
373	        super.delete(start, end);
374	        return this;
375	    }
376	 
381	    public synchronized StringBuffer deleteCharAt(int index) {
382	        super.deleteCharAt(index);
383	        return this;
384	    }
385	 
390	    public synchronized StringBuffer replace(int start, int end, String str) {
391	        super.replace(start, end, str);
392	        return this;
393	    }
394	 
399	    public synchronized String substring(int start) {
400	        return substring(start, count);
401	    }
402	 
407	    public synchronized CharSequence subSequence(int start, int end) {
408	        return super.substring(start, end);
409	    }
410	 
415	    public synchronized String substring(int start, int end) {
416	        return super.substring(start, end);
417	    }
418	 
423	    public synchronized StringBuffer insert(int index, char str[], int offset,
424	                                            int len)
425	    {
426	        super.insert(index, str, offset, len);
427	        return this;
428	    }
429	 
436	    public synchronized StringBuffer insert(int offset, Object obj) {
437	    super.insert(offset, String.valueOf(obj));
438	        return this;
439	    }
440	 
445	    public synchronized StringBuffer insert(int offset, String str) {
446	        super.insert(offset, str);
447	        return this;
448	    }
449	 
453	    public synchronized StringBuffer insert(int offset, char str[]) {
454	        super.insert(offset, str);
455	    return this;
456	    }
457	 
462	    public StringBuffer insert(int dstOffset, CharSequence s) {
463	        // Note, synchronization achieved via other invocations
464	        if (s == null)
465	            s = "null";
466	        if (s instanceof String)
467	            return this.insert(dstOffset, (String)s);
468	        return this.insert(dstOffset, s, 0, s.length());
469	    }
470	 
475	    public synchronized StringBuffer insert(int dstOffset, CharSequence s,
476	                                            int start, int end)
477	    {
478	        super.insert(dstOffset, s, start, end);
479	        return this;
480	    }
481	 
488	    public StringBuffer insert(int offset, boolean b) {
489	    return insert(offset, String.valueOf(b));
490	    }
491	 
496	    public synchronized StringBuffer insert(int offset, char c) {
497	    super.insert(offset, c);
498	    return this;
499	    }
500	 
507	    public StringBuffer insert(int offset, int i) {
508	    return insert(offset, String.valueOf(i));
509	    }
510	 
517	    public StringBuffer insert(int offset, long l) {
518	    return insert(offset, String.valueOf(l));
519	    }
520	 
527	    public StringBuffer insert(int offset, float f) {
528	    return insert(offset, String.valueOf(f));
529	    }
530	 
537	    public StringBuffer insert(int offset, double d) {
538	    return insert(offset, String.valueOf(d));
539	    }
540	 
545	    public int indexOf(String str) {
546	    return indexOf(str, 0);
547	    }
548	 
553	    public synchronized int indexOf(String str, int fromIndex) {
554	        return String.indexOf(value, 0, count,
555	                              str.toCharArray(), 0, str.length(), fromIndex);
556	    }
557	 
562	    public int lastIndexOf(String str) {
563	        // Note, synchronization achieved via other invocations
564	        return lastIndexOf(str, count);
565	    }
566	 
571	    public synchronized int lastIndexOf(String str, int fromIndex) {
572	        return String.lastIndexOf(value, 0, count,
573	                              str.toCharArray(), 0, str.length(), fromIndex);
574	    }
575	 
579	    public synchronized StringBuffer reverse() {
580	    super.reverse();
581	    return this;
582	    }
583	 
584	    public synchronized String toString() {
585	    return new String(value, 0, count);
586	    }
587	 
599	    private static final java.io.ObjectStreamField[] serialPersistentFields =
600	    {
601	        new java.io.ObjectStreamField("value", char[].class),
602	        new java.io.ObjectStreamField("count", Integer.TYPE),
603	        new java.io.ObjectStreamField("shared", Boolean.TYPE),
604	    };
605	 
610	    private synchronized void writeObject(java.io.ObjectOutputStream s)
611	        throws java.io.IOException {
612	        java.io.ObjectOutputStream.PutField fields = s.putFields();
613	        fields.put("value", value);
614	        fields.put("count", count);
615	        fields.put("shared", false);
616	        s.writeFields();
617	    }
618	 
623	    private void readObject(java.io.ObjectInputStream s)
624	        throws java.io.IOException, ClassNotFoundException {
625	        java.io.ObjectInputStream.GetField fields = s.readFields();
626	        value = (char[])fields.get("value", null);
627	        count = (int)fields.get("count", 0);
628	    }
629	}



你可能感兴趣的:(源码分析)