KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > CharArrayWriter


1 /*
2  * @(#)CharArrayWriter.java 1.23 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  * This class implements a character buffer that can be used as an Writer.
12  * The buffer automatically grows when data is written to the stream. The data
13  * can be retrieved using toCharArray() and toString().
14  * <P>
15  * Note: Invoking close() on this class has no effect, and methods
16  * of this class can be called after the stream has closed
17  * without generating an IOException.
18  *
19  * @author Herb Jellinek
20  * @version 1.23, 07/16/04
21  * @since JDK1.1
22  */

23 public
24 class CharArrayWriter extends Writer JavaDoc {
25     /**
26      * The buffer where data is stored.
27      */

28     protected char buf[];
29
30     /**
31      * The number of chars in the buffer.
32      */

33     protected int count;
34
35     /**
36      * Creates a new CharArrayWriter.
37      */

38     public CharArrayWriter() {
39     this(32);
40     }
41
42     /**
43      * Creates a new CharArrayWriter with the specified initial size.
44      *
45      * @param initialSize an int specifying the initial buffer size.
46      * @exception IllegalArgumentException if initialSize is negative
47      */

48     public CharArrayWriter(int initialSize) {
49         if (initialSize < 0) {
50             throw new IllegalArgumentException JavaDoc("Negative initial size: "
51                            + initialSize);
52         }
53     buf = new char[initialSize];
54     }
55
56     /**
57      * Writes a character to the buffer.
58      */

59     public void write(int c) {
60     synchronized (lock) {
61         int newcount = count + 1;
62         if (newcount > buf.length) {
63         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
64         System.arraycopy(buf, 0, newbuf, 0, count);
65         buf = newbuf;
66         }
67         buf[count] = (char)c;
68         count = newcount;
69     }
70     }
71
72     /**
73      * Writes characters to the buffer.
74      * @param c the data to be written
75      * @param off the start offset in the data
76      * @param len the number of chars that are written
77      */

78     public void write(char c[], int off, int len) {
79     if ((off < 0) || (off > c.length) || (len < 0) ||
80             ((off + len) > c.length) || ((off + len) < 0)) {
81         throw new IndexOutOfBoundsException JavaDoc();
82     } else if (len == 0) {
83         return;
84     }
85     synchronized (lock) {
86         int newcount = count + len;
87         if (newcount > buf.length) {
88         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
89         System.arraycopy(buf, 0, newbuf, 0, count);
90         buf = newbuf;
91         }
92         System.arraycopy(c, off, buf, count, len);
93         count = newcount;
94     }
95     }
96
97     /**
98      * Write a portion of a string to the buffer.
99      * @param str String to be written from
100      * @param off Offset from which to start reading characters
101      * @param len Number of characters to be written
102      */

103     public void write(String JavaDoc str, int off, int len) {
104     synchronized (lock) {
105         int newcount = count + len;
106         if (newcount > buf.length) {
107         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
108         System.arraycopy(buf, 0, newbuf, 0, count);
109         buf = newbuf;
110         }
111         str.getChars(off, off + len, buf, count);
112         count = newcount;
113     }
114     }
115
116     /**
117      * Writes the contents of the buffer to another character stream.
118      *
119      * @param out the output stream to write to
120      * @throws IOException If an I/O error occurs.
121      */

122     public void writeTo(Writer JavaDoc out) throws IOException JavaDoc {
123     synchronized (lock) {
124         out.write(buf, 0, count);
125     }
126     }
127
128     /**
129      * Appends the specified character sequence to this writer.
130      *
131      * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
132      * behaves in exactly the same way as the invocation
133      *
134      * <pre>
135      * out.write(csq.toString()) </pre>
136      *
137      * <p> Depending on the specification of <tt>toString</tt> for the
138      * character sequence <tt>csq</tt>, the entire sequence may not be
139      * appended. For instance, invoking the <tt>toString</tt> method of a
140      * character buffer will return a subsequence whose content depends upon
141      * the buffer's position and limit.
142      *
143      * @param csq
144      * The character sequence to append. If <tt>csq</tt> is
145      * <tt>null</tt>, then the four characters <tt>"null"</tt> are
146      * appended to this writer.
147      *
148      * @return This writer
149      *
150      * @since 1.5
151      */

152     public CharArrayWriter JavaDoc append(CharSequence JavaDoc csq) {
153     String JavaDoc s = (csq == null ? "null" : csq.toString());
154     write(s, 0, s.length());
155     return this;
156     }
157
158     /**
159      * Appends a subsequence of the specified character sequence to this writer.
160      *
161      * <p> An invocation of this method of the form <tt>out.append(csq, start,
162      * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
163      * exactly the same way as the invocation
164      *
165      * <pre>
166      * out.write(csq.subSequence(start, end).toString()) </pre>
167      *
168      * @param csq
169      * The character sequence from which a subsequence will be
170      * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
171      * will be appended as if <tt>csq</tt> contained the four
172      * characters <tt>"null"</tt>.
173      *
174      * @param start
175      * The index of the first character in the subsequence
176      *
177      * @param end
178      * The index of the character following the last character in the
179      * subsequence
180      *
181      * @return This writer
182      *
183      * @throws IndexOutOfBoundsException
184      * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
185      * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
186      * <tt>csq.length()</tt>
187      *
188      * @since 1.5
189      */

190     public CharArrayWriter JavaDoc append(CharSequence JavaDoc csq, int start, int end) {
191     String JavaDoc s = (csq == null ? "null" : csq).subSequence(start, end).toString();
192     write(s, 0, s.length());
193     return this;
194     }
195
196     /**
197      * Appends the specified character to this writer.
198      *
199      * <p> An invocation of this method of the form <tt>out.append(c)</tt>
200      * behaves in exactly the same way as the invocation
201      *
202      * <pre>
203      * out.write(c) </pre>
204      *
205      * @param c
206      * The 16-bit character to append
207      *
208      * @return This writer
209      *
210      * @since 1.5
211      */

212     public CharArrayWriter JavaDoc append(char c) {
213     write(c);
214     return this;
215     }
216
217     /**
218      * Resets the buffer so that you can use it again without
219      * throwing away the already allocated buffer.
220      */

221     public void reset() {
222     count = 0;
223     }
224
225     /**
226      * Returns a copy of the input data.
227      *
228      * @return an array of chars copied from the input data.
229      */

230     public char toCharArray()[] {
231     synchronized (lock) {
232         char newbuf[] = new char[count];
233         System.arraycopy(buf, 0, newbuf, 0, count);
234         return newbuf;
235     }
236     }
237
238     /**
239      * Returns the current size of the buffer.
240      *
241      * @return an int representing the current size of the buffer.
242      */

243     public int size() {
244     return count;
245     }
246
247     /**
248      * Converts input data to a string.
249      * @return the string.
250      */

251     public String JavaDoc toString() {
252     synchronized (lock) {
253         return new String JavaDoc(buf, 0, count);
254     }
255     }
256
257     /**
258      * Flush the stream.
259      */

260     public void flush() { }
261
262     /**
263      * Close the stream. This method does not release the buffer, since its
264      * contents might still be required. Note: Invoking this method in this class
265      * will have no effect.
266      */

267     public void close() { }
268
269 }
270
Popular Tags