0N/A/*
3909N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/A
0N/A/**
0N/A * Writes text to a character-output stream, buffering characters so as to
0N/A * provide for the efficient writing of single characters, arrays, and strings.
0N/A *
0N/A * <p> The buffer size may be specified, or the default size may be accepted.
0N/A * The default is large enough for most purposes.
0N/A *
0N/A * <p> A newLine() method is provided, which uses the platform's own notion of
0N/A * line separator as defined by the system property <tt>line.separator</tt>.
0N/A * Not all platforms use the newline character ('\n') to terminate lines.
0N/A * Calling this method to terminate each output line is therefore preferred to
0N/A * writing a newline character directly.
0N/A *
0N/A * <p> In general, a Writer sends its output immediately to the underlying
0N/A * character or byte stream. Unless prompt output is required, it is advisable
0N/A * to wrap a BufferedWriter around any Writer whose write() operations may be
0N/A * costly, such as FileWriters and OutputStreamWriters. For example,
0N/A *
0N/A * <pre>
0N/A * PrintWriter out
0N/A * = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
0N/A * </pre>
0N/A *
0N/A * will buffer the PrintWriter's output to the file. Without buffering, each
0N/A * invocation of a print() method would cause characters to be converted into
0N/A * bytes that would then be written immediately to the file, which can be very
0N/A * inefficient.
0N/A *
0N/A * @see PrintWriter
0N/A * @see FileWriter
0N/A * @see OutputStreamWriter
3471N/A * @see java.nio.file.Files#newBufferedWriter
0N/A *
0N/A * @author Mark Reinhold
0N/A * @since JDK1.1
0N/A */
0N/A
0N/Apublic class BufferedWriter extends Writer {
0N/A
0N/A private Writer out;
0N/A
0N/A private char cb[];
0N/A private int nChars, nextChar;
0N/A
0N/A private static int defaultCharBufferSize = 8192;
0N/A
0N/A /**
0N/A * Line separator string. This is the value of the line.separator
0N/A * property at the moment that the stream was created.
0N/A */
0N/A private String lineSeparator;
0N/A
0N/A /**
0N/A * Creates a buffered character-output stream that uses a default-sized
0N/A * output buffer.
0N/A *
0N/A * @param out A Writer
0N/A */
0N/A public BufferedWriter(Writer out) {
0N/A this(out, defaultCharBufferSize);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new buffered character-output stream that uses an output
0N/A * buffer of the given size.
0N/A *
0N/A * @param out A Writer
0N/A * @param sz Output-buffer size, a positive integer
0N/A *
0N/A * @exception IllegalArgumentException If sz is <= 0
0N/A */
0N/A public BufferedWriter(Writer out, int sz) {
0N/A super(out);
0N/A if (sz <= 0)
0N/A throw new IllegalArgumentException("Buffer size <= 0");
0N/A this.out = out;
0N/A cb = new char[sz];
0N/A nChars = sz;
0N/A nextChar = 0;
0N/A
0N/A lineSeparator = java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("line.separator"));
0N/A }
0N/A
0N/A /** Checks to make sure that the stream has not been closed */
0N/A private void ensureOpen() throws IOException {
0N/A if (out == null)
0N/A throw new IOException("Stream closed");
0N/A }
0N/A
0N/A /**
0N/A * Flushes the output buffer to the underlying character stream, without
0N/A * flushing the stream itself. This method is non-private only so that it
0N/A * may be invoked by PrintStream.
0N/A */
0N/A void flushBuffer() throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A if (nextChar == 0)
0N/A return;
0N/A out.write(cb, 0, nextChar);
0N/A nextChar = 0;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes a single character.
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public void write(int c) throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A if (nextChar >= nChars)
0N/A flushBuffer();
0N/A cb[nextChar++] = (char) c;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Our own little min method, to avoid loading java.lang.Math if we've run
0N/A * out of file descriptors and we're trying to print a stack trace.
0N/A */
0N/A private int min(int a, int b) {
0N/A if (a < b) return a;
0N/A return b;
0N/A }
0N/A
0N/A /**
0N/A * Writes a portion of an array of characters.
0N/A *
0N/A * <p> Ordinarily this method stores characters from the given array into
0N/A * this stream's buffer, flushing the buffer to the underlying stream as
0N/A * needed. If the requested length is at least as large as the buffer,
0N/A * however, then this method will flush the buffer and write the characters
0N/A * directly to the underlying stream. Thus redundant
0N/A * <code>BufferedWriter</code>s will not copy data unnecessarily.
0N/A *
0N/A * @param cbuf A character array
0N/A * @param off Offset from which to start reading characters
0N/A * @param len Number of characters to write
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public void write(char cbuf[], int off, int len) throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A if ((off < 0) || (off > cbuf.length) || (len < 0) ||
0N/A ((off + len) > cbuf.length) || ((off + len) < 0)) {
0N/A throw new IndexOutOfBoundsException();
0N/A } else if (len == 0) {
0N/A return;
0N/A }
0N/A
0N/A if (len >= nChars) {
0N/A /* If the request length exceeds the size of the output buffer,
0N/A flush the buffer and then write the data directly. In this
0N/A way buffered streams will cascade harmlessly. */
0N/A flushBuffer();
0N/A out.write(cbuf, off, len);
0N/A return;
0N/A }
0N/A
0N/A int b = off, t = off + len;
0N/A while (b < t) {
0N/A int d = min(nChars - nextChar, t - b);
0N/A System.arraycopy(cbuf, b, cb, nextChar, d);
0N/A b += d;
0N/A nextChar += d;
0N/A if (nextChar >= nChars)
0N/A flushBuffer();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes a portion of a String.
0N/A *
0N/A * <p> If the value of the <tt>len</tt> parameter is negative then no
0N/A * characters are written. This is contrary to the specification of this
0N/A * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
0N/A * superclass}, which requires that an {@link IndexOutOfBoundsException} be
0N/A * thrown.
0N/A *
0N/A * @param s String to be written
0N/A * @param off Offset from which to start reading characters
0N/A * @param len Number of characters to be written
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public void write(String s, int off, int len) throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A
0N/A int b = off, t = off + len;
0N/A while (b < t) {
0N/A int d = min(nChars - nextChar, t - b);
0N/A s.getChars(b, b + d, cb, nextChar);
0N/A b += d;
0N/A nextChar += d;
0N/A if (nextChar >= nChars)
0N/A flushBuffer();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes a line separator. The line separator string is defined by the
0N/A * system property <tt>line.separator</tt>, and is not necessarily a single
0N/A * newline ('\n') character.
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public void newLine() throws IOException {
0N/A write(lineSeparator);
0N/A }
0N/A
0N/A /**
0N/A * Flushes the stream.
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public void flush() throws IOException {
0N/A synchronized (lock) {
0N/A flushBuffer();
0N/A out.flush();
0N/A }
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A synchronized (lock) {
3643N/A if (out == null) {
3643N/A return;
3643N/A }
3643N/A try {
3643N/A flushBuffer();
3643N/A } finally {
3643N/A out.close();
3643N/A out = null;
3643N/A cb = null;
0N/A }
0N/A }
0N/A }
0N/A}