87 * As of release JDK 5, this class has been supplemented with an equivalent
88 * class designed for use by a single thread, {@link StringBuilder}. The
89 * {@code StringBuilder} class should generally be used in preference to
90 * this one, as it supports all of the same operations but it is faster, as
91 * it performs no synchronization.
92 *
93 * @author Arthur van Hoff
94 * @see java.lang.StringBuilder
95 * @see java.lang.String
96 * @since 1.0
97 */
98 public final class StringBuffer
99 extends AbstractStringBuilder
100 implements java.io.Serializable, CharSequence
101 {
102
103 /**
104 * A cache of the last value returned by toString. Cleared
105 * whenever the StringBuffer is modified.
106 */
107 private transient char[] toStringCache;
108
109 /** use serialVersionUID from JDK 1.0.2 for interoperability */
110 static final long serialVersionUID = 3388685877147921107L;
111
112 /**
113 * Constructs a string buffer with no characters in it and an
114 * initial capacity of 16 characters.
115 */
116 @HotSpotIntrinsicCandidate
117 public StringBuffer() {
118 super(16);
119 }
120
121 /**
122 * Constructs a string buffer with no characters in it and
123 * the specified initial capacity.
124 *
125 * @param capacity the initial capacity.
126 * @exception NegativeArraySizeException if the {@code capacity}
127 * argument is less than {@code 0}.
152 * <p>
153 * If the length of the specified {@code CharSequence} is
154 * less than or equal to zero, then an empty buffer of capacity
155 * {@code 16} is returned.
156 *
157 * @param seq the sequence to copy.
158 * @since 1.5
159 */
160 public StringBuffer(CharSequence seq) {
161 this(seq.length() + 16);
162 append(seq);
163 }
164
165 @Override
166 public synchronized int length() {
167 return count;
168 }
169
170 @Override
171 public synchronized int capacity() {
172 return value.length;
173 }
174
175
176 @Override
177 public synchronized void ensureCapacity(int minimumCapacity) {
178 if (minimumCapacity > value.length) {
179 expandCapacity(minimumCapacity);
180 }
181 }
182
183 /**
184 * @since 1.5
185 */
186 @Override
187 public synchronized void trimToSize() {
188 super.trimToSize();
189 }
190
191 /**
192 * @throws IndexOutOfBoundsException {@inheritDoc}
193 * @see #length()
194 */
195 @Override
196 public synchronized void setLength(int newLength) {
197 toStringCache = null;
198 super.setLength(newLength);
199 }
200
201 /**
202 * @throws IndexOutOfBoundsException {@inheritDoc}
203 * @see #length()
204 */
205 @Override
206 public synchronized char charAt(int index) {
207 if ((index < 0) || (index >= count))
208 throw new StringIndexOutOfBoundsException(index);
209 return value[index];
210 }
211
212 /**
213 * @throws IndexOutOfBoundsException {@inheritDoc}
214 * @since 1.5
215 */
216 @Override
217 public synchronized int codePointAt(int index) {
218 return super.codePointAt(index);
219 }
220
221 /**
222 * @throws IndexOutOfBoundsException {@inheritDoc}
223 * @since 1.5
224 */
225 @Override
226 public synchronized int codePointBefore(int index) {
227 return super.codePointBefore(index);
228 }
229
244 public synchronized int offsetByCodePoints(int index, int codePointOffset) {
245 return super.offsetByCodePoints(index, codePointOffset);
246 }
247
248 /**
249 * @throws IndexOutOfBoundsException {@inheritDoc}
250 */
251 @Override
252 public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
253 int dstBegin)
254 {
255 super.getChars(srcBegin, srcEnd, dst, dstBegin);
256 }
257
258 /**
259 * @throws IndexOutOfBoundsException {@inheritDoc}
260 * @see #length()
261 */
262 @Override
263 public synchronized void setCharAt(int index, char ch) {
264 if ((index < 0) || (index >= count))
265 throw new StringIndexOutOfBoundsException(index);
266 toStringCache = null;
267 value[index] = ch;
268 }
269
270 @Override
271 public synchronized StringBuffer append(Object obj) {
272 toStringCache = null;
273 super.append(String.valueOf(obj));
274 return this;
275 }
276
277 @Override
278 @HotSpotIntrinsicCandidate
279 public synchronized StringBuffer append(String str) {
280 toStringCache = null;
281 super.append(str);
282 return this;
283 }
284
285 /**
286 * Appends the specified {@code StringBuffer} to this sequence.
287 * <p>
663 */
664 @Override
665 public synchronized int lastIndexOf(String str, int fromIndex) {
666 return super.lastIndexOf(str, fromIndex);
667 }
668
669 /**
670 * @since 1.0.2
671 */
672 @Override
673 public synchronized StringBuffer reverse() {
674 toStringCache = null;
675 super.reverse();
676 return this;
677 }
678
679 @Override
680 @HotSpotIntrinsicCandidate
681 public synchronized String toString() {
682 if (toStringCache == null) {
683 toStringCache = Arrays.copyOfRange(value, 0, count);
684 }
685 return new String(toStringCache, true);
686 }
687
688 /**
689 * Serializable fields for StringBuffer.
690 *
691 * @serialField value char[]
692 * The backing character array of this StringBuffer.
693 * @serialField count int
694 * The number of characters in this StringBuffer.
695 * @serialField shared boolean
696 * A flag indicating whether the backing array is shared.
697 * The value is ignored upon deserialization.
698 */
699 private static final java.io.ObjectStreamField[] serialPersistentFields =
700 {
701 new java.io.ObjectStreamField("value", char[].class),
702 new java.io.ObjectStreamField("count", Integer.TYPE),
703 new java.io.ObjectStreamField("shared", Boolean.TYPE),
704 };
705
706 /**
707 * readObject is called to restore the state of the StringBuffer from
708 * a stream.
709 */
710 private synchronized void writeObject(java.io.ObjectOutputStream s)
711 throws java.io.IOException {
712 java.io.ObjectOutputStream.PutField fields = s.putFields();
713 fields.put("value", value);
714 fields.put("count", count);
715 fields.put("shared", false);
716 s.writeFields();
717 }
718
719 /**
720 * readObject is called to restore the state of the StringBuffer from
721 * a stream.
722 */
723 private void readObject(java.io.ObjectInputStream s)
724 throws java.io.IOException, ClassNotFoundException {
725 java.io.ObjectInputStream.GetField fields = s.readFields();
726 value = (char[])fields.get("value", null);
727 count = fields.get("count", 0);
728 }
729 }
|
87 * As of release JDK 5, this class has been supplemented with an equivalent
88 * class designed for use by a single thread, {@link StringBuilder}. The
89 * {@code StringBuilder} class should generally be used in preference to
90 * this one, as it supports all of the same operations but it is faster, as
91 * it performs no synchronization.
92 *
93 * @author Arthur van Hoff
94 * @see java.lang.StringBuilder
95 * @see java.lang.String
96 * @since 1.0
97 */
98 public final class StringBuffer
99 extends AbstractStringBuilder
100 implements java.io.Serializable, CharSequence
101 {
102
103 /**
104 * A cache of the last value returned by toString. Cleared
105 * whenever the StringBuffer is modified.
106 */
107 private transient String toStringCache;
108
109 /** use serialVersionUID from JDK 1.0.2 for interoperability */
110 static final long serialVersionUID = 3388685877147921107L;
111
112 /**
113 * Constructs a string buffer with no characters in it and an
114 * initial capacity of 16 characters.
115 */
116 @HotSpotIntrinsicCandidate
117 public StringBuffer() {
118 super(16);
119 }
120
121 /**
122 * Constructs a string buffer with no characters in it and
123 * the specified initial capacity.
124 *
125 * @param capacity the initial capacity.
126 * @exception NegativeArraySizeException if the {@code capacity}
127 * argument is less than {@code 0}.
152 * <p>
153 * If the length of the specified {@code CharSequence} is
154 * less than or equal to zero, then an empty buffer of capacity
155 * {@code 16} is returned.
156 *
157 * @param seq the sequence to copy.
158 * @since 1.5
159 */
160 public StringBuffer(CharSequence seq) {
161 this(seq.length() + 16);
162 append(seq);
163 }
164
165 @Override
166 public synchronized int length() {
167 return count;
168 }
169
170 @Override
171 public synchronized int capacity() {
172 return super.capacity();
173 }
174
175
176 @Override
177 public synchronized void ensureCapacity(int minimumCapacity) {
178 super.ensureCapacity(minimumCapacity);
179 }
180
181 /**
182 * @since 1.5
183 */
184 @Override
185 public synchronized void trimToSize() {
186 super.trimToSize();
187 }
188
189 /**
190 * @throws IndexOutOfBoundsException {@inheritDoc}
191 * @see #length()
192 */
193 @Override
194 public synchronized void setLength(int newLength) {
195 toStringCache = null;
196 super.setLength(newLength);
197 }
198
199 /**
200 * @throws IndexOutOfBoundsException {@inheritDoc}
201 * @see #length()
202 */
203 @Override
204 public synchronized char charAt(int index) {
205 return super.charAt(index);
206 }
207
208 /**
209 * @throws IndexOutOfBoundsException {@inheritDoc}
210 * @since 1.5
211 */
212 @Override
213 public synchronized int codePointAt(int index) {
214 return super.codePointAt(index);
215 }
216
217 /**
218 * @throws IndexOutOfBoundsException {@inheritDoc}
219 * @since 1.5
220 */
221 @Override
222 public synchronized int codePointBefore(int index) {
223 return super.codePointBefore(index);
224 }
225
240 public synchronized int offsetByCodePoints(int index, int codePointOffset) {
241 return super.offsetByCodePoints(index, codePointOffset);
242 }
243
244 /**
245 * @throws IndexOutOfBoundsException {@inheritDoc}
246 */
247 @Override
248 public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
249 int dstBegin)
250 {
251 super.getChars(srcBegin, srcEnd, dst, dstBegin);
252 }
253
254 /**
255 * @throws IndexOutOfBoundsException {@inheritDoc}
256 * @see #length()
257 */
258 @Override
259 public synchronized void setCharAt(int index, char ch) {
260 toStringCache = null;
261 super.setCharAt(index, ch);
262 }
263
264 @Override
265 public synchronized StringBuffer append(Object obj) {
266 toStringCache = null;
267 super.append(String.valueOf(obj));
268 return this;
269 }
270
271 @Override
272 @HotSpotIntrinsicCandidate
273 public synchronized StringBuffer append(String str) {
274 toStringCache = null;
275 super.append(str);
276 return this;
277 }
278
279 /**
280 * Appends the specified {@code StringBuffer} to this sequence.
281 * <p>
657 */
658 @Override
659 public synchronized int lastIndexOf(String str, int fromIndex) {
660 return super.lastIndexOf(str, fromIndex);
661 }
662
663 /**
664 * @since 1.0.2
665 */
666 @Override
667 public synchronized StringBuffer reverse() {
668 toStringCache = null;
669 super.reverse();
670 return this;
671 }
672
673 @Override
674 @HotSpotIntrinsicCandidate
675 public synchronized String toString() {
676 if (toStringCache == null) {
677 return toStringCache =
678 isLatin1() ? StringLatin1.newString(value, 0, count)
679 : StringUTF16.newString(value, 0, count);
680 }
681 return new String(toStringCache);
682 }
683
684 /**
685 * Serializable fields for StringBuffer.
686 *
687 * @serialField value char[]
688 * The backing character array of this StringBuffer.
689 * @serialField count int
690 * The number of characters in this StringBuffer.
691 * @serialField shared boolean
692 * A flag indicating whether the backing array is shared.
693 * The value is ignored upon deserialization.
694 */
695 private static final java.io.ObjectStreamField[] serialPersistentFields =
696 {
697 new java.io.ObjectStreamField("value", char[].class),
698 new java.io.ObjectStreamField("count", Integer.TYPE),
699 new java.io.ObjectStreamField("shared", Boolean.TYPE),
700 };
701
702 /**
703 * readObject is called to restore the state of the StringBuffer from
704 * a stream.
705 */
706 private synchronized void writeObject(java.io.ObjectOutputStream s)
707 throws java.io.IOException {
708 java.io.ObjectOutputStream.PutField fields = s.putFields();
709 char[] val = new char[capacity()];
710 if (isLatin1()) {
711 StringLatin1.getChars(value, 0, count, val, 0);
712 } else {
713 StringUTF16.getChars(value, 0, count, val, 0);
714 }
715 fields.put("value", val);
716 fields.put("count", count);
717 fields.put("shared", false);
718 s.writeFields();
719 }
720
721 /**
722 * readObject is called to restore the state of the StringBuffer from
723 * a stream.
724 */
725 private void readObject(java.io.ObjectInputStream s)
726 throws java.io.IOException, ClassNotFoundException {
727 java.io.ObjectInputStream.GetField fields = s.readFields();
728 char[] val = (char[])fields.get("value", null);
729 initBytes(val, 0, val.length);
730 count = fields.get("count", 0);
731 }
732
733 protected synchronized void getBytes(byte dst[], int dstBegin, byte coder) {
734 super.getBytes(dst, dstBegin, coder);
735 }
736 }
|