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 . |
Public Methods |
synchronized
void
|
flush()
Flushes this stream to ensure all pending data is written out to the
target stream.
|
synchronized
void
|
write(int oneByte)
Writes one byte to this stream.
|
synchronized
void
|
write(byte[] buffer, int offset, int length)
Writes count bytes from the byte array buffer starting at
offset to this stream.
|
[Expand]
Inherited Methods |
From class java.io.FilterOutputStream
void
|
close()
Closes this stream.
|
void
|
flush()
Ensures that all pending data is sent out to the target stream.
|
void
|
write(byte[] buffer)
Writes the entire contents of the byte array buffer to this
stream.
|
void
|
write(int oneByte)
Writes one byte to the target stream.
|
void
|
write(byte[] buffer, int offset, int count)
Writes count bytes from the byte array buffer starting at
offset to the target stream.
|
|
From class java.io.OutputStream
void
|
close()
Closes this stream.
|
void
|
flush()
Flushes this stream.
|
void
|
write(byte[] buffer)
Writes the entire contents of the byte array buffer to this
stream.
|
abstract
void
|
write(int oneByte)
Writes a single byte to this stream.
|
void
|
write(byte[] buffer, int offset, int count)
Writes count bytes from the byte array buffer starting at
position offset to this stream.
|
|
From class java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Is called before the object's memory is being reclaimed by the VM.
|
final
Class<? extends Object>
|
getClass()
Returns the unique instance of Class which represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
|
From interface java.io.Closeable
abstract
void
|
close()
Closes the object and release any system resources it holds.
|
|
From interface java.io.Flushable
abstract
void
|
flush()
Flushes the object by writing out any buffered data to the underlying
output.
|
|
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
.
Public Constructors
public
BufferedOutputStream
(OutputStream out)
Constructs a new BufferedOutputStream
on the OutputStream
out
. The buffer size is set to the default value of 8 KB.
Parameters
out
| the OutputStream for which write operations are
buffered.
|
public
BufferedOutputStream
(OutputStream out, int size)
Constructs a new BufferedOutputStream
on the OutputStream
out
. The buffer size is set to size
.
Parameters
out
| the output stream for which write operations are buffered. |
size
| the size of the buffer in bytes. |
Public Methods
public
synchronized
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.
|
public
synchronized
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
| the byte to be written. |
Throws
IOException
| if an error occurs attempting to write to this stream.
|
public
synchronized
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
| 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. |