to top
Android APIs
public class

BufferedOutputStream

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

Class Overview

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"));
 

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.
[Expand]
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
synchronized void close()
Closes this stream.
synchronized void flush()
Flushes this stream to ensure all pending data is written out to the target stream.
synchronized void write(byte[] buffer, int offset, int length)
Writes count bytes from the byte array buffer starting at offset to this stream.
synchronized void write(int oneByte)
Writes one byte to this stream.
[Expand]
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

protected byte[] buf

Added in API level 1

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

protected int count

Added in API level 1

The total number of bytes inside the byte array buf.

Public Constructors

public BufferedOutputStream (OutputStream out)

Added in API level 1

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

Parameters
out the OutputStream the buffer writes to.

public BufferedOutputStream (OutputStream out, int size)

Added in API level 1

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

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

Public Methods

public synchronized void close ()

Added in API level 1

Closes this stream. This implementation closes the target stream.

Throws
IOException

public synchronized void flush ()

Added in API level 1

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.

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

Added in API level 1

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 the buffer to be written.
offset the start position in buffer from where to get bytes.
length 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.

public synchronized void write (int oneByte)

Added in API level 1

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 the byte to be written.
Throws
IOException if an error occurs attempting to write to this stream.