< prev index next >

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

Print this page

      1 /*
      2  * Copyright (c) 1994, 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 java.util.Arrays;
     29 import jdk.internal.HotSpotIntrinsicCandidate;
     30
     31 /**
     32  * A thread-safe, mutable sequence of characters.
     33  * A string buffer is like a {@link String}, but can be modified. At any
     34  * point in time it contains some particular sequence of characters, but
     35  * the length and content of the sequence can be changed through certain
     36  * method calls.
     37  * <p>
     38  * String buffers are safe for use by multiple threads. The methods
     39  * are synchronized where necessary so that all the operations on any
     40  * particular instance behave as if they occur in some serial order
     41  * that is consistent with the order of the method calls made by each of
     42  * the individual threads involved.
     43  * <p>
     44  * The principal operations on a {@code StringBuffer} are the
     45  * {@code append} and {@code insert} methods, which are
     46  * overloaded so as to accept data of any type. Each effectively
     47  * converts a given datum to a string and then appends or inserts the
    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 }
      1 /*
      2  * Copyright (c) 1994, 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 java.io.IOException;
     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
    717     /**
    718      * Serializable fields for StringBuffer.
    719      *
    720      * @serialField value  char[]
    721      *              The backing character array of this StringBuffer.
    722      * @serialField count int
    723      *              The number of characters in this StringBuffer.
    724      * @serialField shared  boolean
    725      *              A flag indicating whether the backing array is shared.
    726      *              The value is ignored upon deserialization.
    727      */
    728     @java.io.Serial
    729     private static final java.io.ObjectStreamField[] serialPersistentFields =
    730     {
    731         new java.io.ObjectStreamField("value", char[].class),
    732         new java.io.ObjectStreamField("count", Integer.TYPE),
    733         new java.io.ObjectStreamField("shared", Boolean.TYPE),
    734     };
    735
    736     /**
    737      * The {@code writeObject} method is called to write the state of the {@code StringBuffer} to
    738      * a stream.
    739      *
    740      * @param  s the {@code ObjectOutputStream} to which data is written
    741      * @throws IOException if an I/O error occurs
    742      */
    743     @java.io.Serial
    744     private synchronized void writeObject(java.io.ObjectOutputStream s)
    745         throws java.io.IOException {
    746         java.io.ObjectOutputStream.PutField fields = s.putFields();
    747         char[] val = new char[capacity()];
    748         if (isLatin1()) {
    749             StringLatin1.getChars(value, 0, count, val, 0);
    750         } else {
    751             StringUTF16.getChars(value, 0, count, val, 0);
    752         }
    753         fields.put("value", val);
    754         fields.put("count", count);
    755         fields.put("shared", false);
    756         s.writeFields();
    757     }
    758
    759     /**
    760      * The {@code readObject} method is called to restore the state of the {@code StringBuffer} from
    761      * a stream.
    762      *
    763      * @param  s the {@code ObjectInputStream} from which data is read
    764      * @throws IOException if an I/O error occurs
    765      * @throws ClassNotFoundException if a serialized class cannot be loaded
    766      */
    767     @java.io.Serial
    768     private void readObject(java.io.ObjectInputStream s)
    769         throws java.io.IOException, ClassNotFoundException {
    770         java.io.ObjectInputStream.GetField fields = s.readFields();
    771         char[] val = (char[])fields.get("value", null);
    772         initBytes(val, 0, val.length);
    773         count = fields.get("count", 0);
    774     }
    775
    776     synchronized void getBytes(byte dst[], int dstBegin, byte coder) {
    777         super.getBytes(dst, dstBegin, coder);
    778     }
    779 }

< prev index next >