public class BufferedOutputStream extends FilterOutputStream
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"));
BufferedInputStream| Modifier and Type | Field and Description |
|---|---|
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. |
out| Constructor and Description |
|---|
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. |
| Modifier and Type | Method and Description |
|---|---|
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.
|
nullOutputStream, writeprotected byte[] buf
protected int count
buf.public BufferedOutputStream(OutputStream out)
BufferedOutputStream, providing out with a buffer
of 8192 bytes.out - the OutputStream the buffer writes to.public BufferedOutputStream(OutputStream out, int size)
BufferedOutputStream, providing out with size bytes
of buffer.out - the OutputStream the buffer writes to.size - the size of buffer in bytes.IllegalArgumentException - if size <= 0.public void flush()
throws IOException
flush in interface Flushableflush in class FilterOutputStreamIOException - if an error occurs attempting to flush this stream.public void write(byte[] buffer,
int offset,
int length)
throws IOException
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.write in class FilterOutputStreambuffer - 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.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 void close()
throws IOException
FilterOutputStreamclose in interface Closeableclose in interface AutoCloseableclose in class FilterOutputStreamIOException - if an error occurs attempting to close this stream.public void write(int oneByte)
throws IOException
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.write in class FilterOutputStreamoneByte - the byte to be written.IOException - if an error occurs attempting to write to this stream.