KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > StringWriter


1 /*
2  * @(#)StringWriter.java 1.24 04/07/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10
11 /**
12  * A character stream that collects its output in a string buffer, which can
13  * then be used to construct a string.
14  * <p>
15  * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
16  * can be called after the stream has been closed without generating an
17  * <tt>IOException</tt>.
18  *
19  * @version 1.24, 04/07/16
20  * @author Mark Reinhold
21  * @since JDK1.1
22  */

23
24 public class StringWriter extends Writer JavaDoc {
25
26     private StringBuffer JavaDoc buf;
27
28     /**
29      * Create a new string writer, using the default initial string-buffer
30      * size.
31      */

32     public StringWriter() {
33     buf = new StringBuffer JavaDoc();
34     lock = buf;
35     }
36
37     /**
38      * Create a new string writer, using the specified initial string-buffer
39      * size.
40      *
41      * @param initialSize an int specifying the initial size of the buffer.
42      */

43     public StringWriter(int initialSize) {
44     if (initialSize < 0) {
45         throw new IllegalArgumentException JavaDoc("Negative buffer size");
46     }
47     buf = new StringBuffer JavaDoc(initialSize);
48     lock = buf;
49     }
50
51     /**
52      * Write a single character.
53      */

54     public void write(int c) {
55     buf.append((char) c);
56     }
57
58     /**
59      * Write a portion of an array of characters.
60      *
61      * @param cbuf Array of characters
62      * @param off Offset from which to start writing characters
63      * @param len Number of characters to write
64      */

65     public void write(char cbuf[], int off, int len) {
66         if ((off < 0) || (off > cbuf.length) || (len < 0) ||
67             ((off + len) > cbuf.length) || ((off + len) < 0)) {
68             throw new IndexOutOfBoundsException JavaDoc();
69         } else if (len == 0) {
70             return;
71         }
72         buf.append(cbuf, off, len);
73     }
74
75     /**
76      * Write a string.
77      */

78     public void write(String JavaDoc str) {
79     buf.append(str);
80     }
81
82     /**
83      * Write a portion of a string.
84      *
85      * @param str String to be written
86      * @param off Offset from which to start writing characters
87      * @param len Number of characters to write
88      */

89     public void write(String JavaDoc str, int off, int len) {
90     buf.append(str.substring(off, off + len));
91     }
92
93     /**
94      * Appends the specified character sequence to this writer.
95      *
96      * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
97      * behaves in exactly the same way as the invocation
98      *
99      * <pre>
100      * out.write(csq.toString()) </pre>
101      *
102      * <p> Depending on the specification of <tt>toString</tt> for the
103      * character sequence <tt>csq</tt>, the entire sequence may not be
104      * appended. For instance, invoking the <tt>toString</tt> method of a
105      * character buffer will return a subsequence whose content depends upon
106      * the buffer's position and limit.
107      *
108      * @param csq
109      * The character sequence to append. If <tt>csq</tt> is
110      * <tt>null</tt>, then the four characters <tt>"null"</tt> are
111      * appended to this writer.
112      *
113      * @return This writer
114      *
115      * @since 1.5
116      */

117     public StringWriter JavaDoc append(CharSequence JavaDoc csq) {
118     if (csq == null)
119         write("null");
120     else
121         write(csq.toString());
122         return this;
123     }
124
125     /**
126      * Appends a subsequence of the specified character sequence to this writer.
127      *
128      * <p> An invocation of this method of the form <tt>out.append(csq, start,
129      * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
130      * exactly the same way as the invocation
131      *
132      * <pre>
133      * out.write(csq.subSequence(start, end).toString()) </pre>
134      *
135      * @param csq
136      * The character sequence from which a subsequence will be
137      * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
138      * will be appended as if <tt>csq</tt> contained the four
139      * characters <tt>"null"</tt>.
140      *
141      * @param start
142      * The index of the first character in the subsequence
143      *
144      * @param end
145      * The index of the character following the last character in the
146      * subsequence
147      *
148      * @return This writer
149      *
150      * @throws IndexOutOfBoundsException
151      * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
152      * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
153      * <tt>csq.length()</tt>
154      *
155      * @since 1.5
156      */

157     public StringWriter JavaDoc append(CharSequence JavaDoc csq, int start, int end) {
158     CharSequence JavaDoc cs = (csq == null ? "null" : csq);
159     write(cs.subSequence(start, end).toString());
160         return this;
161     }
162
163     /**
164      * Appends the specified character to this writer.
165      *
166      * <p> An invocation of this method of the form <tt>out.append(c)</tt>
167      * behaves in exactly the same way as the invocation
168      *
169      * <pre>
170      * out.write(c) </pre>
171      *
172      * @param c
173      * The 16-bit character to append
174      *
175      * @return This writer
176      *
177      * @since 1.5
178      */

179     public StringWriter JavaDoc append(char c) {
180     write(c);
181     return this;
182     }
183
184     /**
185      * Return the buffer's current value as a string.
186      */

187     public String JavaDoc toString() {
188     return buf.toString();
189     }
190
191     /**
192      * Return the string buffer itself.
193      *
194      * @return StringBuffer holding the current buffer value.
195      */

196     public StringBuffer JavaDoc getBuffer() {
197     return buf;
198     }
199
200     /**
201      * Flush the stream.
202      */

203     public void flush() {
204     }
205
206     /**
207      * Closing a <tt>StringWriter</tt> has no effect. The methods in this
208      * class can be called after the stream has been closed without generating
209      * an <tt>IOException</tt>.
210      */

211     public void close() throws IOException JavaDoc {
212     }
213
214 }
215
Popular Tags