source: trunk/gcc/libjava/java/io/BufferedWriter.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.8 KB
Line 
1/* BufferedWriter.java -- Buffer output into large blocks before writing
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.io;
40
41/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
42 * "The Java Language Specification", ISBN 0-201-63451-1
43 * Status: Complete to version 1.1.
44 */
45
46/**
47 * This class accumulates chars written in a buffer instead of immediately
48 * writing the data to the underlying output sink. The chars are instead
49 * as one large block when the buffer is filled, or when the stream is
50 * closed or explicitly flushed. This mode operation can provide a more
51 * efficient mechanism for writing versus doing numerous small unbuffered
52 * writes.
53 *
54 * @version 0.0
55 *
56 * @author Aaron M. Renn (arenn@urbanophile.com)
57 * @author Tom Tromey <tromey@cygnus.com>
58 * @date September 25, 1998
59 */
60
61public class BufferedWriter extends Writer
62{
63 /**
64 * This method initializes a new <code>BufferedWriter</code> instance
65 * that will write to the specified subordinate <code>Writer</code>
66 * and which will use a default buffer size of 512 chars.
67 *
68 * @param out The underlying <code>Writer</code> to write data to
69 */
70 public BufferedWriter (Writer out)
71 {
72 this (out, DEFAULT_BUFFER_SIZE);
73 }
74
75 /**
76 * This method initializes a new <code>BufferedWriter</code> instance
77 * that will write to the specified subordinate <code>Writer</code>
78 * and which will use the specified buffer size
79 *
80 * @param out The underlying <code>Writer</code> to write data to
81 * @param size The size of the internal buffer
82 */
83 public BufferedWriter (Writer ox, int size)
84 {
85 super (ox);
86 out = ox;
87 buffer = new char[size];
88 count = 0;
89 }
90
91 /**
92 * This method flushes any remaining buffered chars then closes the
93 * underlying output stream. Any further attempts to write to this stream
94 * may throw an exception
95 */
96 public void close () throws IOException
97 {
98 synchronized (lock)
99 {
100 // It is safe to call localFlush even if the stream is already
101 // closed.
102 localFlush ();
103 out.close();
104 buffer = null;
105 }
106 }
107
108 /**
109 * This method causes any currently buffered chars to be immediately
110 * written to the underlying output stream.
111 *
112 * @exception IOException If an error occurs
113 */
114 public void flush () throws IOException
115 {
116 synchronized (lock)
117 {
118 if (buffer == null)
119 throw new IOException ("Stream closed");
120 localFlush ();
121 out.flush();
122 }
123 }
124
125 /**
126 * This method writes out a system depedent line separator sequence. The
127 * actual value written is detemined from the <xmp>line.separator</xmp>
128 * system property.
129 *
130 * @exception IOException If an error occurs
131 */
132 public void newLine () throws IOException
133 {
134 write (System.getProperty("line.separator"));
135 }
136
137 /**
138 * This method writes a single char of data. This will be written to the
139 * buffer instead of the underlying data source. However, if the buffer
140 * is filled as a result of this write request, it will be flushed to the
141 * underlying output stream.
142 *
143 * @param b The char of data to be written, passed as an int
144 *
145 * @exception IOException If an error occurs
146 */
147 public void write (int oneChar) throws IOException
148 {
149 synchronized (lock)
150 {
151 if (buffer == null)
152 throw new IOException ("Stream closed");
153 buffer[count++] = (char) oneChar;
154 if (count == buffer.length)
155 localFlush ();
156 }
157 }
158
159 /**
160 * This method writes <code>len</code> chars from the char array
161 * <code>buf</code> starting at position <code>offset</code> in the buffer.
162 * These chars will be written to the internal buffer. However, if this
163 * write operation fills the buffer, the buffer will be flushed to the
164 * underlying output stream.
165 *
166 * @param buf The array of chars to write.
167 * @param offset The index into the char array to start writing from.
168 * @param len The number of chars to write.
169 *
170 * @exception IOException If an error occurs
171 */
172 public void write (char[] buf, int offset, int len) throws IOException
173 {
174 synchronized (lock)
175 {
176 if (buffer == null)
177 throw new IOException ("Stream closed");
178
179 // Bypass buffering if there is too much incoming data.
180 if (count + len > buffer.length)
181 {
182 localFlush ();
183 out.write(buf, offset, len);
184 }
185 else
186 {
187 System.arraycopy(buf, offset, buffer, count, len);
188 count += len;
189 if (count == buffer.length)
190 localFlush ();
191 }
192 }
193 }
194
195 /**
196 * This method writes <code>len</code> chars from the <code>String</code>
197 * <code>str</code> starting at position <code>offset</code> in the string.
198 * These chars will be written to the internal buffer. However, if this
199 * write operation fills the buffer, the buffer will be flushed to the
200 * underlying output stream.
201 *
202 * @param str The <code>String</code> to write.
203 * @param offset The index into the string to start writing from.
204 * @param len The number of chars to write.
205 *
206 * @exception IOException If an error occurs
207 */
208 public void write (String str, int offset, int len) throws IOException
209 {
210 synchronized (lock)
211 {
212 if (buffer == null)
213 throw new IOException ("Stream closed");
214
215 if (count + len > buffer.length)
216 {
217 localFlush ();
218 out.write(str, offset, len);
219 }
220 else
221 {
222 str.getChars(offset, offset + len, buffer, count);
223 count += len;
224 if (count == buffer.length)
225 localFlush ();
226 }
227 }
228 }
229
230 // This should only be called with the lock held.
231 private final void localFlush () throws IOException
232 {
233 if (count > 0)
234 {
235 out.write(buffer, 0, count);
236 count = 0;
237 }
238 }
239
240 /**
241 * This is the underlying <code>Writer</code> to which this object
242 * sends its output.
243 */
244 private Writer out;
245
246 /**
247 * This is the internal char array used for buffering output before
248 * writing it.
249 */
250 char[] buffer;
251
252 /**
253 * This is the number of chars that are currently in the buffer and
254 * are waiting to be written to the underlying stream. It always points to
255 * the index into the buffer where the next char of data will be stored
256 */
257 int count;
258
259 /**
260 * This is the default buffer size
261 */
262 private static final int DEFAULT_BUFFER_SIZE = 8192;
263}
Note: See TracBrowser for help on using the repository browser.