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