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