2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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).
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.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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.
33 * <p> The buffer size may be specified, or the default size may be accepted.
34 * The default is large enough for most purposes.
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.
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,
49 * = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
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
59 * @see OutputStreamWriter
60 * @see java.nio.file.Files#newBufferedWriter
62 * @author Mark Reinhold
66 public class BufferedWriter extends Writer {
71 private int nChars, nextChar;
73 private static int defaultCharBufferSize = 8192;
76 * Line separator string. This is the value of the line.separator
77 * property at the moment that the stream was created.
79 private String lineSeparator;
82 * Creates a buffered character-output stream that uses a default-sized
87 public BufferedWriter(Writer out) {
88 this(out, defaultCharBufferSize);
92 * Creates a new buffered character-output stream that uses an output
93 * buffer of the given size.
96 * @param sz Output-buffer size, a positive integer
98 * @exception IllegalArgumentException If sz is <= 0
100 public BufferedWriter(Writer out, int sz) {
103 throw new IllegalArgumentException("Buffer size <= 0");
109 lineSeparator = java.security.AccessController.doPrivileged(
110 new sun.security.action.GetPropertyAction("line.separator"));
113 /** Checks to make sure that the stream has not been closed */
114 private void ensureOpen() throws IOException {
116 throw new IOException("Stream closed");
120 * Flushes the output buffer to the underlying character stream, without
121 * flushing the stream itself. This method is non-private only so that it
122 * may be invoked by PrintStream.
124 void flushBuffer() throws IOException {
125 synchronized (lock) {
129 out.write(cb, 0, nextChar);
135 * Writes a single character.
137 * @exception IOException If an I/O error occurs
139 public void write(int c) throws IOException {
140 synchronized (lock) {
142 if (nextChar >= nChars)
144 cb[nextChar++] = (char) c;
149 * Our own little min method, to avoid loading java.lang.Math if we've run
150 * out of file descriptors and we're trying to print a stack trace.
152 private int min(int a, int b) {
158 * Writes a portion of an array of characters.
160 * <p> Ordinarily this method stores characters from the given array into
161 * this stream's buffer, flushing the buffer to the underlying stream as
162 * needed. If the requested length is at least as large as the buffer,
163 * however, then this method will flush the buffer and write the characters
164 * directly to the underlying stream. Thus redundant
165 * <code>BufferedWriter</code>s will not copy data unnecessarily.
167 * @param cbuf A character array
168 * @param off Offset from which to start reading characters
169 * @param len Number of characters to write
171 * @exception IOException If an I/O error occurs
173 public void write(char cbuf[], int off, int len) throws IOException {
174 synchronized (lock) {
176 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
177 ((off + len) > cbuf.length) || ((off + len) < 0)) {
178 throw new IndexOutOfBoundsException();
179 } else if (len == 0) {
184 /* If the request length exceeds the size of the output buffer,
185 flush the buffer and then write the data directly. In this
186 way buffered streams will cascade harmlessly. */
188 out.write(cbuf, off, len);
192 int b = off, t = off + len;
194 int d = min(nChars - nextChar, t - b);
195 System.arraycopy(cbuf, b, cb, nextChar, d);
198 if (nextChar >= nChars)
205 * Writes a portion of a String.
207 * <p> If the value of the <tt>len</tt> parameter is negative then no
208 * characters are written. This is contrary to the specification of this
209 * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
210 * superclass}, which requires that an {@link IndexOutOfBoundsException} be
213 * @param s String to be written
214 * @param off Offset from which to start reading characters
215 * @param len Number of characters to be written
217 * @exception IOException If an I/O error occurs
219 public void write(String s, int off, int len) throws IOException {
220 synchronized (lock) {
223 int b = off, t = off + len;
225 int d = min(nChars - nextChar, t - b);
226 s.getChars(b, b + d, cb, nextChar);
229 if (nextChar >= nChars)
236 * Writes a line separator. The line separator string is defined by the
237 * system property <tt>line.separator</tt>, and is not necessarily a single
238 * newline ('\n') character.
240 * @exception IOException If an I/O error occurs
242 public void newLine() throws IOException {
243 write(lineSeparator);
247 * Flushes the stream.
249 * @exception IOException If an I/O error occurs
251 public void flush() throws IOException {
252 synchronized (lock) {
258 public void close() throws IOException {
259 synchronized (lock) {