BufferedOutputStream.java revision 0
7321N/A/*
7321N/A * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
7321N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7321N/A *
7321N/A * This code is free software; you can redistribute it and/or modify it
7321N/A * under the terms of the GNU General Public License version 2 only, as
7321N/A * published by the Free Software Foundation. Sun designates this
7321N/A * particular file as subject to the "Classpath" exception as provided
7321N/A * by Sun in the LICENSE file that accompanied this code.
7321N/A *
7321N/A * This code is distributed in the hope that it will be useful, but WITHOUT
7321N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7321N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
7321N/A * version 2 for more details (a copy is included in the LICENSE file that
7321N/A * accompanied this code).
7321N/A *
7321N/A * You should have received a copy of the GNU General Public License version
7321N/A * 2 along with this work; if not, write to the Free Software Foundation,
7321N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7321N/A *
7321N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
7321N/A * CA 95054 USA or visit www.sun.com if you need additional information or
7321N/A * have any questions.
7321N/A */
7321N/A
7321N/Apackage java.io;
7321N/A
7321N/A/**
7321N/A * The class implements a buffered output stream. By setting up such
7321N/A * an output stream, an application can write bytes to the underlying
7321N/A * output stream without necessarily causing a call to the underlying
7321N/A * system for each byte written.
7321N/A *
7321N/A * @author Arthur van Hoff
7321N/A * @since JDK1.0
7321N/A */
7321N/Apublic
7321N/Aclass BufferedOutputStream extends FilterOutputStream {
7321N/A /**
7321N/A * The internal buffer where data is stored.
7321N/A */
7321N/A protected byte buf[];
7321N/A
7321N/A /**
7321N/A * The number of valid bytes in the buffer. This value is always
7321N/A * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
7321N/A * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
7321N/A * byte data.
7321N/A */
7321N/A protected int count;
7321N/A
7321N/A /**
7321N/A * Creates a new buffered output stream to write data to the
7321N/A * specified underlying output stream.
7321N/A *
7321N/A * @param out the underlying output stream.
7321N/A */
7321N/A public BufferedOutputStream(OutputStream out) {
7321N/A this(out, 8192);
7321N/A }
7321N/A
7321N/A /**
7321N/A * Creates a new buffered output stream to write data to the
7321N/A * specified underlying output stream with the specified buffer
7321N/A * size.
7321N/A *
7321N/A * @param out the underlying output stream.
7321N/A * @param size the buffer size.
7321N/A * @exception IllegalArgumentException if size &lt;= 0.
7321N/A */
7321N/A public BufferedOutputStream(OutputStream out, int size) {
7321N/A super(out);
7321N/A if (size <= 0) {
7321N/A throw new IllegalArgumentException("Buffer size <= 0");
7321N/A }
7321N/A buf = new byte[size];
7321N/A }
7321N/A
7321N/A /** Flush the internal buffer */
7321N/A private void flushBuffer() throws IOException {
7321N/A if (count > 0) {
7321N/A out.write(buf, 0, count);
7321N/A count = 0;
7321N/A }
7321N/A }
7321N/A
7321N/A /**
7321N/A * Writes the specified byte to this buffered output stream.
7321N/A *
7321N/A * @param b the byte to be written.
7321N/A * @exception IOException if an I/O error occurs.
7321N/A */
7321N/A public synchronized void write(int b) throws IOException {
7321N/A if (count >= buf.length) {
7321N/A flushBuffer();
7321N/A }
7321N/A buf[count++] = (byte)b;
7321N/A }
7321N/A
7321N/A /**
7321N/A * Writes <code>len</code> bytes from the specified byte array
7321N/A * starting at offset <code>off</code> to this buffered output stream.
7321N/A *
7321N/A * <p> Ordinarily this method stores bytes from the given array into this
7321N/A * stream's buffer, flushing the buffer to the underlying output stream as
7321N/A * needed. If the requested length is at least as large as this stream's
7321N/A * buffer, however, then this method will flush the buffer and write the
7321N/A * bytes directly to the underlying output stream. Thus redundant
7321N/A * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
7321N/A *
7321N/A * @param b the data.
7321N/A * @param off the start offset in the data.
7321N/A * @param len the number of bytes to write.
7321N/A * @exception IOException if an I/O error occurs.
7321N/A */
7321N/A public synchronized void write(byte b[], int off, int len) throws IOException {
7321N/A if (len >= buf.length) {
7321N/A /* If the request length exceeds the size of the output buffer,
7321N/A flush the output buffer and then write the data directly.
7321N/A In this way buffered streams will cascade harmlessly. */
7321N/A flushBuffer();
7321N/A out.write(b, off, len);
7321N/A return;
7321N/A }
7321N/A if (len > buf.length - count) {
7321N/A flushBuffer();
7321N/A }
7321N/A System.arraycopy(b, off, buf, count, len);
7321N/A count += len;
7321N/A }
7321N/A
7321N/A /**
7321N/A * Flushes this buffered output stream. This forces any buffered
7321N/A * output bytes to be written out to the underlying output stream.
7321N/A *
7321N/A * @exception IOException if an I/O error occurs.
7321N/A * @see java.io.FilterOutputStream#out
7321N/A */
7321N/A public synchronized void flush() throws IOException {
7321N/A flushBuffer();
7321N/A out.flush();
7321N/A }
7321N/A}
7321N/A