Skip to content

Most visited

Recently visited

navigation
Added in API level 1

BufferedOutputStream

public class BufferedOutputStream
extends FilterOutputStream

java.lang.Object
   ↳ java.io.OutputStream
     ↳ java.io.FilterOutputStream
       ↳ java.io.BufferedOutputStream


Wraps an existing OutputStream and buffers the output. Expensive interaction with the underlying input stream is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that copying takes place when flushing that buffer, but this is usually outweighed by the performance benefits.

A typical application pattern for the class looks like this:

 BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));
 

See also:

Summary

Fields

protected byte[] buf

The buffer containing the bytes to be written to the target stream.

protected int count

The total number of bytes inside the byte array buf.

Inherited fields

From class java.io.FilterOutputStream

Public constructors

BufferedOutputStream(OutputStream out)

Constructs a new BufferedOutputStream, providing out with a buffer of 8192 bytes.

BufferedOutputStream(OutputStream out, int size)

Constructs a new BufferedOutputStream, providing out with size bytes of buffer.

Public methods

void close()

Closes this stream.

void flush()

Flushes this stream to ensure all pending data is written out to the target stream.

void write(byte[] buffer, int offset, int length)

Writes count bytes from the byte array buffer starting at offset to this stream.

void write(int oneByte)

Writes one byte to this stream.

Inherited methods

From class java.io.FilterOutputStream
From class java.io.OutputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Fields

buf

Added in API level 1
byte[] buf

The buffer containing the bytes to be written to the target stream.

count

Added in API level 1
int count

The total number of bytes inside the byte array buf.

Public constructors

BufferedOutputStream

Added in API level 1
BufferedOutputStream (OutputStream out)

Constructs a new BufferedOutputStream, providing out with a buffer of 8192 bytes.

Parameters
out OutputStream: the OutputStream the buffer writes to.

BufferedOutputStream

Added in API level 1
BufferedOutputStream (OutputStream out, 
                int size)

Constructs a new BufferedOutputStream, providing out with size bytes of buffer.

Parameters
out OutputStream: the OutputStream the buffer writes to.
size int: the size of buffer in bytes.
Throws
IllegalArgumentException if size <= 0.

Public methods

close

Added in API level 1
void close ()

Closes this stream. This implementation closes the target stream.

Throws
IOException

flush

Added in API level 1
void flush ()

Flushes this stream to ensure all pending data is written out to the target stream. In addition, the target stream is flushed.

Throws
IOException if an error occurs attempting to flush this stream.

write

Added in API level 1
void write (byte[] buffer, 
                int offset, 
                int length)

Writes count bytes from the byte array buffer starting at offset to this stream. If there is room in the buffer to hold the bytes, they are copied in. If not, the buffered bytes plus the bytes in buffer are written to the target stream, the target is flushed, and the buffer is cleared.

Parameters
buffer byte: the buffer to be written.
offset int: the start position in buffer from where to get bytes.
length int: the number of bytes from buffer to write to this stream.
Throws
IndexOutOfBoundsException if offset < 0 or length < 0, or if offset + length is greater than the size of buffer.
IOException if an error occurs attempting to write to this stream.
NullPointerException if buffer is null.
ArrayIndexOutOfBoundsException If offset or count is outside of bounds.

write

Added in API level 1
void write (int oneByte)

Writes one byte to this stream. Only the low order byte of the integer oneByte is written. If there is room in the buffer, the byte is copied into the buffer and the count incremented. Otherwise, the buffer plus oneByte are written to the target stream, the target is flushed, and the buffer is reset.

Parameters
oneByte int: the byte to be written.
Throws
IOException if an error occurs attempting to write to this stream.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.