98 * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
99 * {@link java.util.SortedSet SortedSet} for more information.
100 *
101 * @author Arthur van Hoff
102 * @see java.lang.StringBuilder
103 * @see java.lang.String
104 * @since 1.0
105 */
106 public final class StringBuffer
107 extends AbstractStringBuilder
108 implements java.io.Serializable, Comparable<StringBuffer>, CharSequence
109 {
110
111 /**
112 * A cache of the last value returned by toString. Cleared
113 * whenever the StringBuffer is modified.
114 */
115 private transient String toStringCache;
116
117 /** use serialVersionUID from JDK 1.0.2 for interoperability */
118 static final long serialVersionUID = 3388685877147921107L;
119
120 /**
121 * Constructs a string buffer with no characters in it and an
122 * initial capacity of 16 characters.
123 */
124 @HotSpotIntrinsicCandidate
125 public StringBuffer() {
126 super(16);
127 }
128
129 /**
130 * Constructs a string buffer with no characters in it and
131 * the specified initial capacity.
132 *
133 * @param capacity the initial capacity.
134 * @throws NegativeArraySizeException if the {@code capacity}
135 * argument is less than {@code 0}.
136 */
137 @HotSpotIntrinsicCandidate
706 public synchronized String toString() {
707 if (toStringCache == null) {
708 return toStringCache =
709 isLatin1() ? StringLatin1.newString(value, 0, count)
710 : StringUTF16.newString(value, 0, count);
711 }
712 return new String(toStringCache);
713 }
714
715 /**
716 * Serializable fields for StringBuffer.
717 *
718 * @serialField value char[]
719 * The backing character array of this StringBuffer.
720 * @serialField count int
721 * The number of characters in this StringBuffer.
722 * @serialField shared boolean
723 * A flag indicating whether the backing array is shared.
724 * The value is ignored upon deserialization.
725 */
726 private static final java.io.ObjectStreamField[] serialPersistentFields =
727 {
728 new java.io.ObjectStreamField("value", char[].class),
729 new java.io.ObjectStreamField("count", Integer.TYPE),
730 new java.io.ObjectStreamField("shared", Boolean.TYPE),
731 };
732
733 /**
734 * readObject is called to restore the state of the StringBuffer from
735 * a stream.
736 */
737 private synchronized void writeObject(java.io.ObjectOutputStream s)
738 throws java.io.IOException {
739 java.io.ObjectOutputStream.PutField fields = s.putFields();
740 char[] val = new char[capacity()];
741 if (isLatin1()) {
742 StringLatin1.getChars(value, 0, count, val, 0);
743 } else {
744 StringUTF16.getChars(value, 0, count, val, 0);
745 }
746 fields.put("value", val);
747 fields.put("count", count);
748 fields.put("shared", false);
749 s.writeFields();
750 }
751
752 /**
753 * readObject is called to restore the state of the StringBuffer from
754 * a stream.
755 */
756 private void readObject(java.io.ObjectInputStream s)
757 throws java.io.IOException, ClassNotFoundException {
758 java.io.ObjectInputStream.GetField fields = s.readFields();
759 char[] val = (char[])fields.get("value", null);
760 initBytes(val, 0, val.length);
761 count = fields.get("count", 0);
762 }
763
764 synchronized void getBytes(byte dst[], int dstBegin, byte coder) {
765 super.getBytes(dst, dstBegin, coder);
766 }
767 }
|
98 * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
99 * {@link java.util.SortedSet SortedSet} for more information.
100 *
101 * @author Arthur van Hoff
102 * @see java.lang.StringBuilder
103 * @see java.lang.String
104 * @since 1.0
105 */
106 public final class StringBuffer
107 extends AbstractStringBuilder
108 implements java.io.Serializable, Comparable<StringBuffer>, CharSequence
109 {
110
111 /**
112 * A cache of the last value returned by toString. Cleared
113 * whenever the StringBuffer is modified.
114 */
115 private transient String toStringCache;
116
117 /** use serialVersionUID from JDK 1.0.2 for interoperability */
118 @java.io.Serial
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
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
707 public synchronized String toString() {
708 if (toStringCache == null) {
709 return toStringCache =
710 isLatin1() ? StringLatin1.newString(value, 0, count)
711 : StringUTF16.newString(value, 0, count);
712 }
713 return new String(toStringCache);
714 }
715
716 /**
717 * Serializable fields for StringBuffer.
718 *
719 * @serialField value char[]
720 * The backing character array of this StringBuffer.
721 * @serialField count int
722 * The number of characters in this StringBuffer.
723 * @serialField shared boolean
724 * A flag indicating whether the backing array is shared.
725 * The value is ignored upon deserialization.
726 */
727 @java.io.Serial
728 private static final java.io.ObjectStreamField[] serialPersistentFields =
729 {
730 new java.io.ObjectStreamField("value", char[].class),
731 new java.io.ObjectStreamField("count", Integer.TYPE),
732 new java.io.ObjectStreamField("shared", Boolean.TYPE),
733 };
734
735 /**
736 * readObject is called to restore the state of the StringBuffer from
737 * a stream.
738 */
739 @java.io.Serial
740 private synchronized void writeObject(java.io.ObjectOutputStream s)
741 throws java.io.IOException {
742 java.io.ObjectOutputStream.PutField fields = s.putFields();
743 char[] val = new char[capacity()];
744 if (isLatin1()) {
745 StringLatin1.getChars(value, 0, count, val, 0);
746 } else {
747 StringUTF16.getChars(value, 0, count, val, 0);
748 }
749 fields.put("value", val);
750 fields.put("count", count);
751 fields.put("shared", false);
752 s.writeFields();
753 }
754
755 /**
756 * readObject is called to restore the state of the StringBuffer from
757 * a stream.
758 */
759 @java.io.Serial
760 private void readObject(java.io.ObjectInputStream s)
761 throws java.io.IOException, ClassNotFoundException {
762 java.io.ObjectInputStream.GetField fields = s.readFields();
763 char[] val = (char[])fields.get("value", null);
764 initBytes(val, 0, val.length);
765 count = fields.get("count", 0);
766 }
767
768 synchronized void getBytes(byte dst[], int dstBegin, byte coder) {
769 super.getBytes(dst, dstBegin, coder);
770 }
771 }
|