2 * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
30 * A character stream that collects its output in a string buffer, which can
31 * then be used to construct a string.
33 * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
34 * can be called after the stream has been closed without generating an
35 * <tt>IOException</tt>.
37 * @author Mark Reinhold
41 public class StringWriter extends Writer {
43 private StringBuffer buf;
46 * Create a new string writer using the default initial string-buffer
49 public StringWriter() {
50 buf = new StringBuffer();
55 * Create a new string writer using the specified initial string-buffer
59 * The number of <tt>char</tt> values that will fit into this buffer
60 * before it is automatically expanded
62 * @throws IllegalArgumentException
63 * If <tt>initialSize</tt> is negative
65 public StringWriter(int initialSize) {
66 if (initialSize < 0) {
67 throw new IllegalArgumentException("Negative buffer size");
69 buf = new StringBuffer(initialSize);
74 * Write a single character.
76 public void write(int c) {
81 * Write a portion of an array of characters.
83 * @param cbuf Array of characters
84 * @param off Offset from which to start writing characters
85 * @param len Number of characters to write
87 public void write(char cbuf[], int off, int len) {
88 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
89 ((off + len) > cbuf.length) || ((off + len) < 0)) {
90 throw new IndexOutOfBoundsException();
91 } else if (len == 0) {
94 buf.append(cbuf, off, len);
100 public void write(String str) {
105 * Write a portion of a string.
107 * @param str String to be written
108 * @param off Offset from which to start writing characters
109 * @param len Number of characters to write
111 public void write(String str, int off, int len) {
112 buf.append(str.substring(off, off + len));
116 * Appends the specified character sequence to this writer.
118 * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
119 * behaves in exactly the same way as the invocation
122 * out.write(csq.toString()) </pre>
124 * <p> Depending on the specification of <tt>toString</tt> for the
125 * character sequence <tt>csq</tt>, the entire sequence may not be
126 * appended. For instance, invoking the <tt>toString</tt> method of a
127 * character buffer will return a subsequence whose content depends upon
128 * the buffer's position and limit.
131 * The character sequence to append. If <tt>csq</tt> is
132 * <tt>null</tt>, then the four characters <tt>"null"</tt> are
133 * appended to this writer.
135 * @return This writer
139 public StringWriter append(CharSequence csq) {
143 write(csq.toString());
148 * Appends a subsequence of the specified character sequence to this writer.
150 * <p> An invocation of this method of the form <tt>out.append(csq, start,
151 * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
152 * exactly the same way as the invocation
155 * out.write(csq.subSequence(start, end).toString()) </pre>
158 * The character sequence from which a subsequence will be
159 * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
160 * will be appended as if <tt>csq</tt> contained the four
161 * characters <tt>"null"</tt>.
164 * The index of the first character in the subsequence
167 * The index of the character following the last character in the
170 * @return This writer
172 * @throws IndexOutOfBoundsException
173 * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
174 * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
175 * <tt>csq.length()</tt>
179 public StringWriter append(CharSequence csq, int start, int end) {
180 CharSequence cs = (csq == null ? "null" : csq);
181 write(cs.subSequence(start, end).toString());
186 * Appends the specified character to this writer.
188 * <p> An invocation of this method of the form <tt>out.append(c)</tt>
189 * behaves in exactly the same way as the invocation
192 * out.write(c) </pre>
195 * The 16-bit character to append
197 * @return This writer
201 public StringWriter append(char c) {
207 * Return the buffer's current value as a string.
209 public String toString() {
210 return buf.toString();
214 * Return the string buffer itself.
216 * @return StringBuffer holding the current buffer value.
218 public StringBuffer getBuffer() {
225 public void flush() {
229 * Closing a <tt>StringWriter</tt> has no effect. The methods in this
230 * class can be called after the stream has been closed without generating
231 * an <tt>IOException</tt>.
233 public void close() throws IOException {