1 /*
2  * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.lang;
27 
28 import dalvik.annotation.optimization.NeverInline;
29 import java.util.Arrays;
30 import jdk.internal.HotSpotIntrinsicCandidate;
31 
32 /**
33  * A thread-safe, mutable sequence of characters.
34  * A string buffer is like a {@link String}, but can be modified. At any
35  * point in time it contains some particular sequence of characters, but
36  * the length and content of the sequence can be changed through certain
37  * method calls.
38  * <p>
39  * String buffers are safe for use by multiple threads. The methods
40  * are synchronized where necessary so that all the operations on any
41  * particular instance behave as if they occur in some serial order
42  * that is consistent with the order of the method calls made by each of
43  * the individual threads involved.
44  * <p>
45  * The principal operations on a {@code StringBuffer} are the
46  * {@code append} and {@code insert} methods, which are
47  * overloaded so as to accept data of any type. Each effectively
48  * converts a given datum to a string and then appends or inserts the
49  * characters of that string to the string buffer. The
50  * {@code append} method always adds these characters at the end
51  * of the buffer; the {@code insert} method adds the characters at
52  * a specified point.
53  * <p>
54  * For example, if {@code z} refers to a string buffer object
55  * whose current contents are {@code "start"}, then
56  * the method call {@code z.append("le")} would cause the string
57  * buffer to contain {@code "startle"}, whereas
58  * {@code z.insert(4, "le")} would alter the string buffer to
59  * contain {@code "starlet"}.
60  * <p>
61  * In general, if sb refers to an instance of a {@code StringBuffer},
62  * then {@code sb.append(x)} has the same effect as
63  * {@code sb.insert(sb.length(), x)}.
64  * <p>
65  * Whenever an operation occurs involving a source sequence (such as
66  * appending or inserting from a source sequence), this class synchronizes
67  * only on the string buffer performing the operation, not on the source.
68  * Note that while {@code StringBuffer} is designed to be safe to use
69  * concurrently from multiple threads, if the constructor or the
70  * {@code append} or {@code insert} operation is passed a source sequence
71  * that is shared across threads, the calling code must ensure
72  * that the operation has a consistent and unchanging view of the source
73  * sequence for the duration of the operation.
74  * This could be satisfied by the caller holding a lock during the
75  * operation's call, by using an immutable source sequence, or by not
76  * sharing the source sequence across threads.
77  * <p>
78  * Every string buffer has a capacity. As long as the length of the
79  * character sequence contained in the string buffer does not exceed
80  * the capacity, it is not necessary to allocate a new internal
81  * buffer array. If the internal buffer overflows, it is
82  * automatically made larger.
83  * <p>
84  * Unless otherwise noted, passing a {@code null} argument to a constructor
85  * or method in this class will cause a {@link NullPointerException} to be
86  * thrown.
87  * <p>
88  * As of  release JDK 5, this class has been supplemented with an equivalent
89  * class designed for use by a single thread, {@link StringBuilder}.  The
90  * {@code StringBuilder} class should generally be used in preference to
91  * this one, as it supports all of the same operations but it is faster, as
92  * it performs no synchronization.
93  *
94  * @apiNote
95  * {@code StringBuffer} implements {@code Comparable} but does not override
96  * {@link Object#equals equals}. Thus, the natural ordering of {@code StringBuffer}
97  * is inconsistent with equals. Care should be exercised if {@code StringBuffer}
98  * objects are used as keys in a {@code SortedMap} or elements in a {@code SortedSet}.
99  * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
100  * {@link java.util.SortedSet SortedSet} for more information.
101  *
102  * @author      Arthur van Hoff
103  * @see     java.lang.StringBuilder
104  * @see     java.lang.String
105  * @since   1.0
106  */
107  public final class StringBuffer
108     extends AbstractStringBuilder
109     implements java.io.Serializable, Comparable<StringBuffer>, CharSequence
110 {
111 
112     /**
113      * A cache of the last value returned by toString. Cleared
114      * whenever the StringBuffer is modified.
115      */
116     private transient String toStringCache;
117 
118     /** use serialVersionUID from JDK 1.0.2 for interoperability */
119     static final long serialVersionUID = 3388685877147921107L;
120 
121     /**
122      * Constructs a string buffer with no characters in it and an
123      * initial capacity of 16 characters.
124      */
125     @HotSpotIntrinsicCandidate
StringBuffer()126     public StringBuffer() {
127         super(16);
128     }
129 
130     /**
131      * Constructs a string buffer with no characters in it and
132      * the specified initial capacity.
133      *
134      * @param      capacity  the initial capacity.
135      * @throws     NegativeArraySizeException  if the {@code capacity}
136      *             argument is less than {@code 0}.
137      */
138     @HotSpotIntrinsicCandidate
StringBuffer(int capacity)139     public StringBuffer(int capacity) {
140         super(capacity);
141     }
142 
143     /**
144      * Constructs a string buffer initialized to the contents of the
145      * specified string. The initial capacity of the string buffer is
146      * {@code 16} plus the length of the string argument.
147      *
148      * @param   str   the initial contents of the buffer.
149      */
150     @HotSpotIntrinsicCandidate
StringBuffer(String str)151     public StringBuffer(String str) {
152         super(str.length() + 16);
153         append(str);
154     }
155 
156     /**
157      * Constructs a string buffer that contains the same characters
158      * as the specified {@code CharSequence}. The initial capacity of
159      * the string buffer is {@code 16} plus the length of the
160      * {@code CharSequence} argument.
161      * <p>
162      * If the length of the specified {@code CharSequence} is
163      * less than or equal to zero, then an empty buffer of capacity
164      * {@code 16} is returned.
165      *
166      * @param      seq   the sequence to copy.
167      * @since 1.5
168      */
StringBuffer(CharSequence seq)169     public StringBuffer(CharSequence seq) {
170         this(seq.length() + 16);
171         append(seq);
172     }
173 
174     /**
175      * Compares two {@code StringBuffer} instances lexicographically. This method
176      * follows the same rules for lexicographical comparison as defined in the
177      * {@linkplain java.lang.CharSequence#compare(java.lang.CharSequence,
178      * java.lang.CharSequence)  CharSequence.compare(this, another)} method.
179      *
180      * <p>
181      * For finer-grained, locale-sensitive String comparison, refer to
182      * {@link java.text.Collator}.
183      *
184      * @implNote
185      * This method synchronizes on {@code this}, the current object, but not
186      * {@code StringBuffer another} with which {@code this StringBuffer} is compared.
187      *
188      * @param another the {@code StringBuffer} to be compared with
189      *
190      * @return  the value {@code 0} if this {@code StringBuffer} contains the same
191      * character sequence as that of the argument {@code StringBuffer}; a negative integer
192      * if this {@code StringBuffer} is lexicographically less than the
193      * {@code StringBuffer} argument; or a positive integer if this {@code StringBuffer}
194      * is lexicographically greater than the {@code StringBuffer} argument.
195      *
196      * @since 11
197      */
198     @Override
compareTo(StringBuffer another)199     public synchronized int compareTo(StringBuffer another) {
200         return super.compareTo(another);
201     }
202 
203     @Override
204     // We don't want to inline this method to be able to perform String-related
205     // optimizations with intrinsics.
206     @NeverInline
length()207     public synchronized int length() {
208         return count;
209     }
210 
211     @Override
capacity()212     public synchronized int capacity() {
213         return super.capacity();
214     }
215 
216 
217     @Override
ensureCapacity(int minimumCapacity)218     public synchronized void ensureCapacity(int minimumCapacity) {
219         super.ensureCapacity(minimumCapacity);
220     }
221 
222     /**
223      * @since      1.5
224      */
225     @Override
trimToSize()226     public synchronized void trimToSize() {
227         super.trimToSize();
228     }
229 
230     /**
231      * @throws IndexOutOfBoundsException {@inheritDoc}
232      * @see        #length()
233      */
234     @Override
setLength(int newLength)235     public synchronized void setLength(int newLength) {
236         toStringCache = null;
237         super.setLength(newLength);
238     }
239 
240     /**
241      * @throws IndexOutOfBoundsException {@inheritDoc}
242      * @see        #length()
243      */
244     @Override
charAt(int index)245     public synchronized char charAt(int index) {
246         return super.charAt(index);
247     }
248 
249     /**
250      * @throws IndexOutOfBoundsException {@inheritDoc}
251      * @since      1.5
252      */
253     @Override
codePointAt(int index)254     public synchronized int codePointAt(int index) {
255         return super.codePointAt(index);
256     }
257 
258     /**
259      * @throws IndexOutOfBoundsException {@inheritDoc}
260      * @since     1.5
261      */
262     @Override
codePointBefore(int index)263     public synchronized int codePointBefore(int index) {
264         return super.codePointBefore(index);
265     }
266 
267     /**
268      * @throws IndexOutOfBoundsException {@inheritDoc}
269      * @since     1.5
270      */
271     @Override
codePointCount(int beginIndex, int endIndex)272     public synchronized int codePointCount(int beginIndex, int endIndex) {
273         return super.codePointCount(beginIndex, endIndex);
274     }
275 
276     /**
277      * @throws IndexOutOfBoundsException {@inheritDoc}
278      * @since     1.5
279      */
280     @Override
offsetByCodePoints(int index, int codePointOffset)281     public synchronized int offsetByCodePoints(int index, int codePointOffset) {
282         return super.offsetByCodePoints(index, codePointOffset);
283     }
284 
285     /**
286      * @throws IndexOutOfBoundsException {@inheritDoc}
287      */
288     @Override
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)289     public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
290                                       int dstBegin)
291     {
292         super.getChars(srcBegin, srcEnd, dst, dstBegin);
293     }
294 
295     /**
296      * @throws IndexOutOfBoundsException {@inheritDoc}
297      * @see        #length()
298      */
299     @Override
setCharAt(int index, char ch)300     public synchronized void setCharAt(int index, char ch) {
301         toStringCache = null;
302         super.setCharAt(index, ch);
303     }
304 
305     @Override
append(Object obj)306     public synchronized StringBuffer append(Object obj) {
307         toStringCache = null;
308         super.append(String.valueOf(obj));
309         return this;
310     }
311 
312     @Override
313     @HotSpotIntrinsicCandidate
314     // We don't want to inline this method to be able to perform String-related
315     // optimizations with intrinsics.
316     @NeverInline
append(String str)317     public synchronized StringBuffer append(String str) {
318         toStringCache = null;
319         super.append(str);
320         return this;
321     }
322 
323     /**
324      * Appends the specified {@code StringBuffer} to this sequence.
325      * <p>
326      * The characters of the {@code StringBuffer} argument are appended,
327      * in order, to the contents of this {@code StringBuffer}, increasing the
328      * length of this {@code StringBuffer} by the length of the argument.
329      * If {@code sb} is {@code null}, then the four characters
330      * {@code "null"} are appended to this {@code StringBuffer}.
331      * <p>
332      * Let <i>n</i> be the length of the old character sequence, the one
333      * contained in the {@code StringBuffer} just prior to execution of the
334      * {@code append} method. Then the character at index <i>k</i> in
335      * the new character sequence is equal to the character at index <i>k</i>
336      * in the old character sequence, if <i>k</i> is less than <i>n</i>;
337      * otherwise, it is equal to the character at index <i>k-n</i> in the
338      * argument {@code sb}.
339      * <p>
340      * This method synchronizes on {@code this}, the destination
341      * object, but does not synchronize on the source ({@code sb}).
342      *
343      * @param   sb   the {@code StringBuffer} to append.
344      * @return  a reference to this object.
345      * @since 1.4
346      */
append(StringBuffer sb)347     public synchronized StringBuffer append(StringBuffer sb) {
348         toStringCache = null;
349         super.append(sb);
350         return this;
351     }
352 
353     /**
354      * @since 1.8
355      */
356     @Override
append(AbstractStringBuilder asb)357     synchronized StringBuffer append(AbstractStringBuilder asb) {
358         toStringCache = null;
359         super.append(asb);
360         return this;
361     }
362 
363     /**
364      * Appends the specified {@code CharSequence} to this
365      * sequence.
366      * <p>
367      * The characters of the {@code CharSequence} argument are appended,
368      * in order, increasing the length of this sequence by the length of the
369      * argument.
370      *
371      * <p>The result of this method is exactly the same as if it were an
372      * invocation of this.append(s, 0, s.length());
373      *
374      * <p>This method synchronizes on {@code this}, the destination
375      * object, but does not synchronize on the source ({@code s}).
376      *
377      * <p>If {@code s} is {@code null}, then the four characters
378      * {@code "null"} are appended.
379      *
380      * @param   s the {@code CharSequence} to append.
381      * @return  a reference to this object.
382      * @since 1.5
383      */
384     @Override
append(CharSequence s)385     public synchronized StringBuffer append(CharSequence s) {
386         toStringCache = null;
387         super.append(s);
388         return this;
389     }
390 
391     /**
392      * @throws IndexOutOfBoundsException {@inheritDoc}
393      * @since      1.5
394      */
395     @Override
append(CharSequence s, int start, int end)396     public synchronized StringBuffer append(CharSequence s, int start, int end)
397     {
398         toStringCache = null;
399         super.append(s, start, end);
400         return this;
401     }
402 
403     @Override
append(char[] str)404     public synchronized StringBuffer append(char[] str) {
405         toStringCache = null;
406         super.append(str);
407         return this;
408     }
409 
410     /**
411      * @throws IndexOutOfBoundsException {@inheritDoc}
412      */
413     @Override
append(char[] str, int offset, int len)414     public synchronized StringBuffer append(char[] str, int offset, int len) {
415         toStringCache = null;
416         super.append(str, offset, len);
417         return this;
418     }
419 
420     @Override
append(boolean b)421     public synchronized StringBuffer append(boolean b) {
422         toStringCache = null;
423         super.append(b);
424         return this;
425     }
426 
427     @Override
428     @HotSpotIntrinsicCandidate
append(char c)429     public synchronized StringBuffer append(char c) {
430         toStringCache = null;
431         super.append(c);
432         return this;
433     }
434 
435     @Override
436     @HotSpotIntrinsicCandidate
append(int i)437     public synchronized StringBuffer append(int i) {
438         toStringCache = null;
439         super.append(i);
440         return this;
441     }
442 
443     /**
444      * @since 1.5
445      */
446     @Override
appendCodePoint(int codePoint)447     public synchronized StringBuffer appendCodePoint(int codePoint) {
448         toStringCache = null;
449         super.appendCodePoint(codePoint);
450         return this;
451     }
452 
453     @Override
append(long lng)454     public synchronized StringBuffer append(long lng) {
455         toStringCache = null;
456         super.append(lng);
457         return this;
458     }
459 
460     @Override
append(float f)461     public synchronized StringBuffer append(float f) {
462         toStringCache = null;
463         super.append(f);
464         return this;
465     }
466 
467     @Override
append(double d)468     public synchronized StringBuffer append(double d) {
469         toStringCache = null;
470         super.append(d);
471         return this;
472     }
473 
474     /**
475      * @throws StringIndexOutOfBoundsException {@inheritDoc}
476      * @since      1.2
477      */
478     @Override
delete(int start, int end)479     public synchronized StringBuffer delete(int start, int end) {
480         toStringCache = null;
481         super.delete(start, end);
482         return this;
483     }
484 
485     /**
486      * @throws StringIndexOutOfBoundsException {@inheritDoc}
487      * @since      1.2
488      */
489     @Override
deleteCharAt(int index)490     public synchronized StringBuffer deleteCharAt(int index) {
491         toStringCache = null;
492         super.deleteCharAt(index);
493         return this;
494     }
495 
496     /**
497      * @throws StringIndexOutOfBoundsException {@inheritDoc}
498      * @since      1.2
499      */
500     @Override
replace(int start, int end, String str)501     public synchronized StringBuffer replace(int start, int end, String str) {
502         toStringCache = null;
503         super.replace(start, end, str);
504         return this;
505     }
506 
507     /**
508      * @throws StringIndexOutOfBoundsException {@inheritDoc}
509      * @since      1.2
510      */
511     @Override
substring(int start)512     public synchronized String substring(int start) {
513         return substring(start, count);
514     }
515 
516     /**
517      * @throws IndexOutOfBoundsException {@inheritDoc}
518      * @since      1.4
519      */
520     @Override
subSequence(int start, int end)521     public synchronized CharSequence subSequence(int start, int end) {
522         return super.substring(start, end);
523     }
524 
525     /**
526      * @throws StringIndexOutOfBoundsException {@inheritDoc}
527      * @since      1.2
528      */
529     @Override
substring(int start, int end)530     public synchronized String substring(int start, int end) {
531         return super.substring(start, end);
532     }
533 
534     /**
535      * @throws StringIndexOutOfBoundsException {@inheritDoc}
536      * @since      1.2
537      */
538     @Override
insert(int index, char[] str, int offset, int len)539     public synchronized StringBuffer insert(int index, char[] str, int offset,
540                                             int len)
541     {
542         toStringCache = null;
543         super.insert(index, str, offset, len);
544         return this;
545     }
546 
547     /**
548      * @throws StringIndexOutOfBoundsException {@inheritDoc}
549      */
550     @Override
insert(int offset, Object obj)551     public synchronized StringBuffer insert(int offset, Object obj) {
552         toStringCache = null;
553         super.insert(offset, String.valueOf(obj));
554         return this;
555     }
556 
557     /**
558      * @throws StringIndexOutOfBoundsException {@inheritDoc}
559      */
560     @Override
insert(int offset, String str)561     public synchronized StringBuffer insert(int offset, String str) {
562         toStringCache = null;
563         super.insert(offset, str);
564         return this;
565     }
566 
567     /**
568      * @throws StringIndexOutOfBoundsException {@inheritDoc}
569      */
570     @Override
insert(int offset, char[] str)571     public synchronized StringBuffer insert(int offset, char[] str) {
572         toStringCache = null;
573         super.insert(offset, str);
574         return this;
575     }
576 
577     /**
578      * @throws IndexOutOfBoundsException {@inheritDoc}
579      * @since      1.5
580      */
581     @Override
insert(int dstOffset, CharSequence s)582     public StringBuffer insert(int dstOffset, CharSequence s) {
583         // Note, synchronization achieved via invocations of other StringBuffer methods
584         // after narrowing of s to specific type
585         // Ditto for toStringCache clearing
586         super.insert(dstOffset, s);
587         return this;
588     }
589 
590     /**
591      * @throws IndexOutOfBoundsException {@inheritDoc}
592      * @since      1.5
593      */
594     @Override
insert(int dstOffset, CharSequence s, int start, int end)595     public synchronized StringBuffer insert(int dstOffset, CharSequence s,
596             int start, int end)
597     {
598         toStringCache = null;
599         super.insert(dstOffset, s, start, end);
600         return this;
601     }
602 
603     /**
604      * @throws StringIndexOutOfBoundsException {@inheritDoc}
605      */
606     @Override
insert(int offset, boolean b)607     public  StringBuffer insert(int offset, boolean b) {
608         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
609         // after conversion of b to String by super class method
610         // Ditto for toStringCache clearing
611         super.insert(offset, b);
612         return this;
613     }
614 
615     /**
616      * @throws IndexOutOfBoundsException {@inheritDoc}
617      */
618     @Override
insert(int offset, char c)619     public synchronized StringBuffer insert(int offset, char c) {
620         toStringCache = null;
621         super.insert(offset, c);
622         return this;
623     }
624 
625     /**
626      * @throws StringIndexOutOfBoundsException {@inheritDoc}
627      */
628     @Override
insert(int offset, int i)629     public StringBuffer insert(int offset, int i) {
630         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
631         // after conversion of i to String by super class method
632         // Ditto for toStringCache clearing
633         super.insert(offset, i);
634         return this;
635     }
636 
637     /**
638      * @throws StringIndexOutOfBoundsException {@inheritDoc}
639      */
640     @Override
insert(int offset, long l)641     public StringBuffer insert(int offset, long l) {
642         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
643         // after conversion of l to String by super class method
644         // Ditto for toStringCache clearing
645         super.insert(offset, l);
646         return this;
647     }
648 
649     /**
650      * @throws StringIndexOutOfBoundsException {@inheritDoc}
651      */
652     @Override
insert(int offset, float f)653     public StringBuffer insert(int offset, float f) {
654         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
655         // after conversion of f to String by super class method
656         // Ditto for toStringCache clearing
657         super.insert(offset, f);
658         return this;
659     }
660 
661     /**
662      * @throws StringIndexOutOfBoundsException {@inheritDoc}
663      */
664     @Override
insert(int offset, double d)665     public StringBuffer insert(int offset, double d) {
666         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
667         // after conversion of d to String by super class method
668         // Ditto for toStringCache clearing
669         super.insert(offset, d);
670         return this;
671     }
672 
673     /**
674      * @since      1.4
675      */
676     @Override
indexOf(String str)677     public int indexOf(String str) {
678         // Note, synchronization achieved via invocations of other StringBuffer methods
679         return super.indexOf(str);
680     }
681 
682     /**
683      * @since      1.4
684      */
685     @Override
indexOf(String str, int fromIndex)686     public synchronized int indexOf(String str, int fromIndex) {
687         return super.indexOf(str, fromIndex);
688     }
689 
690     /**
691      * @since      1.4
692      */
693     @Override
lastIndexOf(String str)694     public int lastIndexOf(String str) {
695         // Note, synchronization achieved via invocations of other StringBuffer methods
696         return lastIndexOf(str, count);
697     }
698 
699     /**
700      * @since      1.4
701      */
702     @Override
lastIndexOf(String str, int fromIndex)703     public synchronized int lastIndexOf(String str, int fromIndex) {
704         return super.lastIndexOf(str, fromIndex);
705     }
706 
707     /**
708      * @since   1.0.2
709      */
710     @Override
reverse()711     public synchronized StringBuffer reverse() {
712         toStringCache = null;
713         super.reverse();
714         return this;
715     }
716 
717     @Override
718     @HotSpotIntrinsicCandidate
719     @NeverInline
toString()720     public synchronized String toString() {
721         if (toStringCache == null) {
722             return toStringCache =
723                     isLatin1() ? StringLatin1.newString(value, 0, count)
724                                : StringUTF16.newString(value, 0, count);
725         }
726         return new String(toStringCache);
727     }
728 
729     /**
730      * Serializable fields for StringBuffer.
731      *
732      * @serialField value  char[]
733      *              The backing character array of this StringBuffer.
734      * @serialField count int
735      *              The number of characters in this StringBuffer.
736      * @serialField shared  boolean
737      *              A flag indicating whether the backing array is shared.
738      *              The value is ignored upon deserialization.
739      */
740     private static final java.io.ObjectStreamField[] serialPersistentFields =
741     {
742         new java.io.ObjectStreamField("value", char[].class),
743         new java.io.ObjectStreamField("count", Integer.TYPE),
744         new java.io.ObjectStreamField("shared", Boolean.TYPE),
745     };
746 
747     /**
748      * readObject is called to restore the state of the StringBuffer from
749      * a stream.
750      */
writeObject(java.io.ObjectOutputStream s)751     private synchronized void writeObject(java.io.ObjectOutputStream s)
752         throws java.io.IOException {
753         java.io.ObjectOutputStream.PutField fields = s.putFields();
754         char[] val = new char[capacity()];
755         if (isLatin1()) {
756             StringLatin1.getChars(value, 0, count, val, 0);
757         } else {
758             StringUTF16.getChars(value, 0, count, val, 0);
759         }
760         fields.put("value", val);
761         fields.put("count", count);
762         fields.put("shared", false);
763         s.writeFields();
764     }
765 
766     /**
767      * readObject is called to restore the state of the StringBuffer from
768      * a stream.
769      */
readObject(java.io.ObjectInputStream s)770     private void readObject(java.io.ObjectInputStream s)
771         throws java.io.IOException, ClassNotFoundException {
772         java.io.ObjectInputStream.GetField fields = s.readFields();
773         char[] val = (char[])fields.get("value", null);
774         initBytes(val, 0, val.length);
775         count = fields.get("count", 0);
776     }
777 
getBytes(byte dst[], int dstBegin, byte coder)778     synchronized void getBytes(byte dst[], int dstBegin, byte coder) {
779         super.getBytes(dst, dstBegin, coder);
780     }
781 }
782