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 = "\n";
112 /** Checks to make sure that the stream has not been closed */
113 private void ensureOpen() throws IOException {
115 throw new IOException("Stream closed");
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.
123 void flushBuffer() throws IOException {
124 synchronized (lock) {
128 out.write(cb, 0, nextChar);
134 * Writes a single character.
136 * @exception IOException If an I/O error occurs
138 public void write(int c) throws IOException {
139 synchronized (lock) {
141 if (nextChar >= nChars)
143 cb[nextChar++] = (char) c;
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.
151 private int min(int a, int b) {
157 * Writes a portion of an array of characters.
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.
166 * @param cbuf A character array
167 * @param off Offset from which to start reading characters
168 * @param len Number of characters to write
170 * @exception IOException If an I/O error occurs
172 public void write(char cbuf[], int off, int len) throws IOException {
173 synchronized (lock) {
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) {
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. */
187 out.write(cbuf, off, len);
191 int b = off, t = off + len;
193 int d = min(nChars - nextChar, t - b);
194 System.arraycopy(cbuf, b, cb, nextChar, d);
197 if (nextChar >= nChars)
204 * Writes a portion of a String.
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
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
216 * @exception IOException If an I/O error occurs
218 public void write(String s, int off, int len) throws IOException {
219 synchronized (lock) {
222 int b = off, t = off + len;
224 int d = min(nChars - nextChar, t - b);
225 s.getChars(b, b + d, cb, nextChar);
228 if (nextChar >= nChars)
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.
239 * @exception IOException If an I/O error occurs
241 public void newLine() throws IOException {
242 write(lineSeparator);
246 * Flushes the stream.
248 * @exception IOException If an I/O error occurs
250 public void flush() throws IOException {
251 synchronized (lock) {
257 public void close() throws IOException {
258 synchronized (lock) {