0N/A/*
2362N/A * Copyright (c) 1994, 2003, 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 * The class implements a buffered output stream. By setting up such
0N/A * an output stream, an application can write bytes to the underlying
0N/A * output stream without necessarily causing a call to the underlying
0N/A * system for each byte written.
0N/A *
0N/A * @author Arthur van Hoff
0N/A * @since JDK1.0
0N/A */
0N/Apublic
0N/Aclass BufferedOutputStream extends FilterOutputStream {
0N/A /**
0N/A * The internal buffer where data is stored.
0N/A */
0N/A protected byte buf[];
0N/A
0N/A /**
0N/A * The number of valid bytes in the buffer. This value is always
0N/A * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
0N/A * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
0N/A * byte data.
0N/A */
0N/A protected int count;
0N/A
0N/A /**
0N/A * Creates a new buffered output stream to write data to the
0N/A * specified underlying output stream.
0N/A *
0N/A * @param out the underlying output stream.
0N/A */
0N/A public BufferedOutputStream(OutputStream out) {
0N/A this(out, 8192);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new buffered output stream to write data to the
0N/A * specified underlying output stream with the specified buffer
0N/A * size.
0N/A *
0N/A * @param out the underlying output stream.
0N/A * @param size the buffer size.
0N/A * @exception IllegalArgumentException if size &lt;= 0.
0N/A */
0N/A public BufferedOutputStream(OutputStream out, int size) {
0N/A super(out);
0N/A if (size <= 0) {
0N/A throw new IllegalArgumentException("Buffer size <= 0");
0N/A }
0N/A buf = new byte[size];
0N/A }
0N/A
0N/A /** Flush the internal buffer */
0N/A private void flushBuffer() throws IOException {
0N/A if (count > 0) {
0N/A out.write(buf, 0, count);
0N/A count = 0;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes the specified byte to this buffered output stream.
0N/A *
0N/A * @param b the byte to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public synchronized void write(int b) throws IOException {
0N/A if (count >= buf.length) {
0N/A flushBuffer();
0N/A }
0N/A buf[count++] = (byte)b;
0N/A }
0N/A
0N/A /**
0N/A * Writes <code>len</code> bytes from the specified byte array
0N/A * starting at offset <code>off</code> to this buffered output stream.
0N/A *
0N/A * <p> Ordinarily this method stores bytes from the given array into this
0N/A * stream's buffer, flushing the buffer to the underlying output stream as
0N/A * needed. If the requested length is at least as large as this stream's
0N/A * buffer, however, then this method will flush the buffer and write the
0N/A * bytes directly to the underlying output stream. Thus redundant
0N/A * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
0N/A *
0N/A * @param b the data.
0N/A * @param off the start offset in the data.
0N/A * @param len the number of bytes to write.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public synchronized void write(byte b[], int off, int len) throws IOException {
0N/A if (len >= buf.length) {
0N/A /* If the request length exceeds the size of the output buffer,
0N/A flush the output buffer and then write the data directly.
0N/A In this way buffered streams will cascade harmlessly. */
0N/A flushBuffer();
0N/A out.write(b, off, len);
0N/A return;
0N/A }
0N/A if (len > buf.length - count) {
0N/A flushBuffer();
0N/A }
0N/A System.arraycopy(b, off, buf, count, len);
0N/A count += len;
0N/A }
0N/A
0N/A /**
0N/A * Flushes this buffered output stream. This forces any buffered
0N/A * output bytes to be written out to the underlying output stream.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public synchronized void flush() throws IOException {
0N/A flushBuffer();
0N/A out.flush();
0N/A }
0N/A}