jdk/src/java.base/share/classes/java/io/StringWriter.java
author igerasim
Thu, 02 Jul 2015 00:26:35 +0300
changeset 31471 ae27c6f1d8bf
parent 25859 3317bb8137f4
child 32033 bf24e33c7919
permissions -rw-r--r--
8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A character stream that collects its output in a string buffer, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * then be used to construct a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * can be called after the stream has been closed without generating an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <tt>IOException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      Mark Reinhold
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    38
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class StringWriter extends Writer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private StringBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Create a new string writer using the default initial string-buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public StringWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        lock = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Create a new string writer using the specified initial string-buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param initialSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *        The number of <tt>char</tt> values that will fit into this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *        before it is automatically expanded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *         If <tt>initialSize</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public StringWriter(int initialSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (initialSize < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new IllegalArgumentException("Negative buffer size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        buf = new StringBuffer(initialSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        lock = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Write a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void write(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        buf.append((char) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Write a portion of an array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param  cbuf  Array of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param  off   Offset from which to start writing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param  len   Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void write(char cbuf[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        buf.append(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Write a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void write(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        buf.append(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Write a portion of a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param  str  String to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param  off  Offset from which to start writing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param  len  Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void write(String str, int off, int len)  {
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 25859
diff changeset
   112
        buf.append(str, off, off + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Appends the specified character sequence to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *     out.write(csq.toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p> Depending on the specification of <tt>toString</tt> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * character sequence <tt>csq</tt>, the entire sequence may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * appended. For instance, invoking the <tt>toString</tt> method of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * character buffer will return a subsequence whose content depends upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * the buffer's position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *         The character sequence to append.  If <tt>csq</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         appended to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return  This writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public StringWriter append(CharSequence csq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (csq == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            write("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            write(csq.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Appends a subsequence of the specified character sequence to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p> An invocation of this method of the form <tt>out.append(csq, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *     out.write(csq.subSequence(start, end).toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         The character sequence from which a subsequence will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *         will be appended as if <tt>csq</tt> contained the four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *         characters <tt>"null"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *         The index of the first character in the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         The index of the character following the last character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *         subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return  This writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *          <tt>csq.length()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public StringWriter append(CharSequence csq, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        CharSequence cs = (csq == null ? "null" : csq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        write(cs.subSequence(start, end).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Appends the specified character to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *     out.write(c) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *         The 16-bit character to append
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return  This writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public StringWriter append(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Return the buffer's current value as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Return the string buffer itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return StringBuffer holding the current buffer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public StringBuffer getBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Flush the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Closing a <tt>StringWriter</tt> has no effect. The methods in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * class can be called after the stream has been closed without generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * an <tt>IOException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}