2
|
1 |
/*
|
|
2 |
* Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
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).
|
|
16 |
*
|
|
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.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.io;
|
|
27 |
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Writes text to a character-output stream, buffering characters so as to
|
|
31 |
* provide for the efficient writing of single characters, arrays, and strings.
|
|
32 |
*
|
|
33 |
* <p> The buffer size may be specified, or the default size may be accepted.
|
|
34 |
* The default is large enough for most purposes.
|
|
35 |
*
|
|
36 |
* <p> A newLine() method is provided, which uses the platform's own notion of
|
|
37 |
* line separator as defined by the system property <tt>line.separator</tt>.
|
|
38 |
* Not all platforms use the newline character ('\n') to terminate lines.
|
|
39 |
* Calling this method to terminate each output line is therefore preferred to
|
|
40 |
* writing a newline character directly.
|
|
41 |
*
|
|
42 |
* <p> In general, a Writer sends its output immediately to the underlying
|
|
43 |
* character or byte stream. Unless prompt output is required, it is advisable
|
|
44 |
* to wrap a BufferedWriter around any Writer whose write() operations may be
|
|
45 |
* costly, such as FileWriters and OutputStreamWriters. For example,
|
|
46 |
*
|
|
47 |
* <pre>
|
|
48 |
* PrintWriter out
|
|
49 |
* = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
|
|
50 |
* </pre>
|
|
51 |
*
|
|
52 |
* will buffer the PrintWriter's output to the file. Without buffering, each
|
|
53 |
* invocation of a print() method would cause characters to be converted into
|
|
54 |
* bytes that would then be written immediately to the file, which can be very
|
|
55 |
* inefficient.
|
|
56 |
*
|
|
57 |
* @see PrintWriter
|
|
58 |
* @see FileWriter
|
|
59 |
* @see OutputStreamWriter
|
|
60 |
*
|
|
61 |
* @author Mark Reinhold
|
|
62 |
* @since JDK1.1
|
|
63 |
*/
|
|
64 |
|
|
65 |
public class BufferedWriter extends Writer {
|
|
66 |
|
|
67 |
private Writer out;
|
|
68 |
|
|
69 |
private char cb[];
|
|
70 |
private int nChars, nextChar;
|
|
71 |
|
|
72 |
private static int defaultCharBufferSize = 8192;
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Line separator string. This is the value of the line.separator
|
|
76 |
* property at the moment that the stream was created.
|
|
77 |
*/
|
|
78 |
private String lineSeparator;
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Creates a buffered character-output stream that uses a default-sized
|
|
82 |
* output buffer.
|
|
83 |
*
|
|
84 |
* @param out A Writer
|
|
85 |
*/
|
|
86 |
public BufferedWriter(Writer out) {
|
|
87 |
this(out, defaultCharBufferSize);
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Creates a new buffered character-output stream that uses an output
|
|
92 |
* buffer of the given size.
|
|
93 |
*
|
|
94 |
* @param out A Writer
|
|
95 |
* @param sz Output-buffer size, a positive integer
|
|
96 |
*
|
|
97 |
* @exception IllegalArgumentException If sz is <= 0
|
|
98 |
*/
|
|
99 |
public BufferedWriter(Writer out, int sz) {
|
|
100 |
super(out);
|
|
101 |
if (sz <= 0)
|
|
102 |
throw new IllegalArgumentException("Buffer size <= 0");
|
|
103 |
this.out = out;
|
|
104 |
cb = new char[sz];
|
|
105 |
nChars = sz;
|
|
106 |
nextChar = 0;
|
|
107 |
|
|
108 |
lineSeparator = java.security.AccessController.doPrivileged(
|
|
109 |
new sun.security.action.GetPropertyAction("line.separator"));
|
|
110 |
}
|
|
111 |
|
|
112 |
/** Checks to make sure that the stream has not been closed */
|
|
113 |
private void ensureOpen() throws IOException {
|
|
114 |
if (out == null)
|
|
115 |
throw new IOException("Stream closed");
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Flushes the output buffer to the underlying character stream, without
|
|
120 |
* flushing the stream itself. This method is non-private only so that it
|
|
121 |
* may be invoked by PrintStream.
|
|
122 |
*/
|
|
123 |
void flushBuffer() throws IOException {
|
|
124 |
synchronized (lock) {
|
|
125 |
ensureOpen();
|
|
126 |
if (nextChar == 0)
|
|
127 |
return;
|
|
128 |
out.write(cb, 0, nextChar);
|
|
129 |
nextChar = 0;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Writes a single character.
|
|
135 |
*
|
|
136 |
* @exception IOException If an I/O error occurs
|
|
137 |
*/
|
|
138 |
public void write(int c) throws IOException {
|
|
139 |
synchronized (lock) {
|
|
140 |
ensureOpen();
|
|
141 |
if (nextChar >= nChars)
|
|
142 |
flushBuffer();
|
|
143 |
cb[nextChar++] = (char) c;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Our own little min method, to avoid loading java.lang.Math if we've run
|
|
149 |
* out of file descriptors and we're trying to print a stack trace.
|
|
150 |
*/
|
|
151 |
private int min(int a, int b) {
|
|
152 |
if (a < b) return a;
|
|
153 |
return b;
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Writes a portion of an array of characters.
|
|
158 |
*
|
|
159 |
* <p> Ordinarily this method stores characters from the given array into
|
|
160 |
* this stream's buffer, flushing the buffer to the underlying stream as
|
|
161 |
* needed. If the requested length is at least as large as the buffer,
|
|
162 |
* however, then this method will flush the buffer and write the characters
|
|
163 |
* directly to the underlying stream. Thus redundant
|
|
164 |
* <code>BufferedWriter</code>s will not copy data unnecessarily.
|
|
165 |
*
|
|
166 |
* @param cbuf A character array
|
|
167 |
* @param off Offset from which to start reading characters
|
|
168 |
* @param len Number of characters to write
|
|
169 |
*
|
|
170 |
* @exception IOException If an I/O error occurs
|
|
171 |
*/
|
|
172 |
public void write(char cbuf[], int off, int len) throws IOException {
|
|
173 |
synchronized (lock) {
|
|
174 |
ensureOpen();
|
|
175 |
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
|
|
176 |
((off + len) > cbuf.length) || ((off + len) < 0)) {
|
|
177 |
throw new IndexOutOfBoundsException();
|
|
178 |
} else if (len == 0) {
|
|
179 |
return;
|
|
180 |
}
|
|
181 |
|
|
182 |
if (len >= nChars) {
|
|
183 |
/* If the request length exceeds the size of the output buffer,
|
|
184 |
flush the buffer and then write the data directly. In this
|
|
185 |
way buffered streams will cascade harmlessly. */
|
|
186 |
flushBuffer();
|
|
187 |
out.write(cbuf, off, len);
|
|
188 |
return;
|
|
189 |
}
|
|
190 |
|
|
191 |
int b = off, t = off + len;
|
|
192 |
while (b < t) {
|
|
193 |
int d = min(nChars - nextChar, t - b);
|
|
194 |
System.arraycopy(cbuf, b, cb, nextChar, d);
|
|
195 |
b += d;
|
|
196 |
nextChar += d;
|
|
197 |
if (nextChar >= nChars)
|
|
198 |
flushBuffer();
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
/**
|
|
204 |
* Writes a portion of a String.
|
|
205 |
*
|
|
206 |
* <p> If the value of the <tt>len</tt> parameter is negative then no
|
|
207 |
* characters are written. This is contrary to the specification of this
|
|
208 |
* method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
|
|
209 |
* superclass}, which requires that an {@link IndexOutOfBoundsException} be
|
|
210 |
* thrown.
|
|
211 |
*
|
|
212 |
* @param s String to be written
|
|
213 |
* @param off Offset from which to start reading characters
|
|
214 |
* @param len Number of characters to be written
|
|
215 |
*
|
|
216 |
* @exception IOException If an I/O error occurs
|
|
217 |
*/
|
|
218 |
public void write(String s, int off, int len) throws IOException {
|
|
219 |
synchronized (lock) {
|
|
220 |
ensureOpen();
|
|
221 |
|
|
222 |
int b = off, t = off + len;
|
|
223 |
while (b < t) {
|
|
224 |
int d = min(nChars - nextChar, t - b);
|
|
225 |
s.getChars(b, b + d, cb, nextChar);
|
|
226 |
b += d;
|
|
227 |
nextChar += d;
|
|
228 |
if (nextChar >= nChars)
|
|
229 |
flushBuffer();
|
|
230 |
}
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Writes a line separator. The line separator string is defined by the
|
|
236 |
* system property <tt>line.separator</tt>, and is not necessarily a single
|
|
237 |
* newline ('\n') character.
|
|
238 |
*
|
|
239 |
* @exception IOException If an I/O error occurs
|
|
240 |
*/
|
|
241 |
public void newLine() throws IOException {
|
|
242 |
write(lineSeparator);
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* Flushes the stream.
|
|
247 |
*
|
|
248 |
* @exception IOException If an I/O error occurs
|
|
249 |
*/
|
|
250 |
public void flush() throws IOException {
|
|
251 |
synchronized (lock) {
|
|
252 |
flushBuffer();
|
|
253 |
out.flush();
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
public void close() throws IOException {
|
|
258 |
synchronized (lock) {
|
|
259 |
if (out == null) {
|
|
260 |
return;
|
|
261 |
}
|
|
262 |
try {
|
|
263 |
flushBuffer();
|
|
264 |
} finally {
|
|
265 |
out.close();
|
|
266 |
out = null;
|
|
267 |
cb = null;
|
|
268 |
}
|
|
269 |
}
|
|
270 |
}
|
|
271 |
}
|