< prev index next >

## src/java.base/share/classes/java/lang/StringBuilder.java

Print this page

      1 /*
      2  * Copyright (c) 2003, 2019, 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 jdk.internal.HotSpotIntrinsicCandidate;
     29

     30 /**
     31  * A mutable sequence of characters.  This class provides an API compatible
     32  * with {@code StringBuffer}, but with no guarantee of synchronization.
     33  * This class is designed for use as a drop-in replacement for
     34  * {@code StringBuffer} in places where the string buffer was being
     35  * used by a single thread (as is generally the case).   Where possible,
     36  * it is recommended that this class be used in preference to
     37  * {@code StringBuffer} as it will be faster under most implementations.
     38  *
     39  * <p>The principal operations on a {@code StringBuilder} are the
     40  * {@code append} and {@code insert} methods, which are
     41  * overloaded so as to accept data of any type. Each effectively
     42  * converts a given datum to a string and then appends or inserts the
     43  * characters of that string to the string builder. The
     44  * {@code append} method always adds these characters at the end
     45  * of the builder; the {@code insert} method adds the characters at
     46  * a specified point.
     47  * <p>
     48  * For example, if {@code z} refers to a string builder object
     49  * whose current contents are "{@code start}", then
    441     }
    442
    443     @Override
    444     @HotSpotIntrinsicCandidate
    445     public String toString() {
    446         // Create a copy, don't share the array
    447         return isLatin1() ? StringLatin1.newString(value, 0, count)
    448                           : StringUTF16.newString(value, 0, count);
    449     }
    450
    451     /**
    452      * Save the state of the {@code StringBuilder} instance to a stream
    453      * (that is, serialize it).
    454      *
    455      * @serialData the number of characters currently stored in the string
    456      *             builder ({@code int}), followed by the characters in the
    457      *             string builder ({@code char[]}).   The length of the
    458      *             {@code char} array may be greater than the number of
    459      *             characters currently stored in the string builder, in which
    460      *             case extra characters are ignored.

    461      */
    462     @java.io.Serial
    463     private void writeObject(java.io.ObjectOutputStream s)
    464         throws java.io.IOException {
    465         s.defaultWriteObject();
    466         s.writeInt(count);
    467         char[] val = new char[capacity()];
    468         if (isLatin1()) {
    469             StringLatin1.getChars(value, 0, count, val, 0);
    470         } else {
    471             StringUTF16.getChars(value, 0, count, val, 0);
    472         }
    473         s.writeObject(val);
    474     }
    475
    476     /**
    477      * readObject is called to restore the state of the StringBuffer from
    478      * a stream.

    479      */
    480     @java.io.Serial
    481     private void readObject(java.io.ObjectInputStream s)
    482         throws java.io.IOException, ClassNotFoundException {
    483         s.defaultReadObject();
    484         count = s.readInt();
    485         char[] val = (char[]) s.readObject();
    486         initBytes(val, 0, val.length);
    487     }
    488
    489 }
      1 /*
      2  * Copyright (c) 2003, 2020, 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 jdk.internal.HotSpotIntrinsicCandidate;
     29
     30 import java.io.IOException;
     31
     32 /**
     33  * A mutable sequence of characters.  This class provides an API compatible
     34  * with {@code StringBuffer}, but with no guarantee of synchronization.
     35  * This class is designed for use as a drop-in replacement for
     36  * {@code StringBuffer} in places where the string buffer was being
     37  * used by a single thread (as is generally the case).   Where possible,
     38  * it is recommended that this class be used in preference to
     39  * {@code StringBuffer} as it will be faster under most implementations.
     40  *
     41  * <p>The principal operations on a {@code StringBuilder} are the
     42  * {@code append} and {@code insert} methods, which are
     43  * overloaded so as to accept data of any type. Each effectively
     44  * converts a given datum to a string and then appends or inserts the
     45  * characters of that string to the string builder. The
     46  * {@code append} method always adds these characters at the end
     47  * of the builder; the {@code insert} method adds the characters at
     48  * a specified point.
     49  * <p>
     50  * For example, if {@code z} refers to a string builder object
     51  * whose current contents are "{@code start}", then
    443     }
    444
    445     @Override
    446     @HotSpotIntrinsicCandidate
    447     public String toString() {
    448         // Create a copy, don't share the array
    449         return isLatin1() ? StringLatin1.newString(value, 0, count)
    450                           : StringUTF16.newString(value, 0, count);
    451     }
    452
    453     /**
    454      * Save the state of the {@code StringBuilder} instance to a stream
    455      * (that is, serialize it).
    456      *
    457      * @serialData the number of characters currently stored in the string
    458      *             builder ({@code int}), followed by the characters in the
    459      *             string builder ({@code char[]}).   The length of the
    460      *             {@code char} array may be greater than the number of
    461      *             characters currently stored in the string builder, in which
    462      *             case extra characters are ignored.
    463      *
    464      * @param  s the {@code ObjectOutputStream} to which data is written
    465      * @throws IOException if an I/O error occurs
    466      */
    467     @java.io.Serial
    468     private void writeObject(java.io.ObjectOutputStream s)
    469         throws java.io.IOException {
    470         s.defaultWriteObject();
    471         s.writeInt(count);
    472         char[] val = new char[capacity()];
    473         if (isLatin1()) {
    474             StringLatin1.getChars(value, 0, count, val, 0);
    475         } else {
    476             StringUTF16.getChars(value, 0, count, val, 0);
    477         }
    478         s.writeObject(val);
    479     }
    480
    481     /**
    482      * readObject is called to restore the state of the StringBuffer from
    483      * a stream.
    484      *
    485      * @param  s the {@code ObjectInputStream} from which data is read
    486      * @throws IOException if an I/O error occurs
    487      * @throws ClassNotFoundException if a serialized class cannot be loaded
    488      */
    489     @java.io.Serial
    490     private void readObject(java.io.ObjectInputStream s)
    491         throws IOException, ClassNotFoundException {
    492         s.defaultReadObject();
    493         count = s.readInt();
    494         char[] val = (char[]) s.readObject();
    495         initBytes(val, 0, val.length);
    496     }
    497
    498 }

< prev index next >