java.io

Class BufferedOutputStream

Implemented Interfaces:
Closeable, Flushable

public class BufferedOutputStream
extends FilterOutputStream

This class accumulates bytes written in a buffer instead of immediately writing the data to the underlying output sink. The bytes are instead as one large block when the buffer is filled, or when the stream is closed or explicitly flushed. This mode operation can provide a more efficient mechanism for writing versus doing numerous small unbuffered writes.

Field Summary

protected byte[]
buf
This is the internal byte array used for buffering output before writing it.
protected int
count
This is the number of bytes that are currently in the buffer and are waiting to be written to the underlying stream.

Fields inherited from class java.io.FilterOutputStream

out

Constructor Summary

BufferedOutputStream(OutputStream out)
This method initializes a new BufferedOutputStream instance that will write to the specified subordinate OutputStream and which will use a default buffer size of 512 bytes.
BufferedOutputStream(OutputStream out, int size)
This method initializes a new BufferedOutputStream instance that will write to the specified subordinate OutputStream and which will use the specified buffer size

Method Summary

void
flush()
This method causes any currently buffered bytes to be immediately written to the underlying output stream.
void
write(byte[] buf, int offset, int len)
This method writes len bytes from the byte array buf starting at position offset in the buffer.
void
write(int b)
This method writes a single byte of data.

Methods inherited from class java.io.FilterOutputStream

close, flush, write, write, write

Methods inherited from class java.io.OutputStream

close, flush, write, write, write

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

buf

protected byte[] buf
This is the internal byte array used for buffering output before writing it.

count

protected int count
This is the number of bytes that are currently in the buffer and are waiting to be written to the underlying stream. It always points to the index into the buffer where the next byte of data will be stored

Constructor Details

BufferedOutputStream

public BufferedOutputStream(OutputStream out)
This method initializes a new BufferedOutputStream instance that will write to the specified subordinate OutputStream and which will use a default buffer size of 512 bytes.
Parameters:
out - The underlying OutputStream to write data to

BufferedOutputStream

public BufferedOutputStream(OutputStream out,
                            int size)
This method initializes a new BufferedOutputStream instance that will write to the specified subordinate OutputStream and which will use the specified buffer size
Parameters:
out - The underlying OutputStream to write data to
size - The size of the internal buffer

Method Details

flush

public void flush()
            throws IOException
This method causes any currently buffered bytes to be immediately written to the underlying output stream.
Specified by:
flush in interface Flushable
Overrides:
flush in interface FilterOutputStream
Throws:
IOException - If an error occurs

write

public void write(byte[] buf,
                  int offset,
                  int len)
            throws IOException
This method writes len bytes from the byte array buf starting at position offset in the buffer. These bytes will be written to the internal buffer. However, if this write operation fills the buffer, the buffer will be flushed to the underlying output stream.
Overrides:
write in interface FilterOutputStream
Parameters:
buf - The array of bytes to write.
offset - The index into the byte array to start writing from.
len - The number of bytes to write.
Throws:
IOException - If an error occurs

write

public void write(int b)
            throws IOException
This method writes a single byte of data. This will be written to the buffer instead of the underlying data source. However, if the buffer is filled as a result of this write request, it will be flushed to the underlying output stream.
Overrides:
write in interface FilterOutputStream
Parameters:
b - The byte of data to be written, passed as an int
Throws:
IOException - If an error occurs

BufferedOutputStream.java -- Buffer output into large blocks before writing Copyright (C) 1998, 2000, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.