The class serves as a mediator for a stream writer and stream reader. Any data that is written to the stream is buffered in an internal buffer. Reading from the buffer will remove the previously written bytes from the buffer.
The above behaviour means that a stream instance can be concurrently used by a producer and consumer thread. Any read operations will read the currently available data if any, or block until some data is written to it. When data is written, the consumer threads are notified about this and the bytes may be read.
Closing the stream will not discard previously written data from it, they may still be readable.
This class extends OutputStream, and implements ByteSource and ByteSink. To retrieve an
InputStream instance, use ByteSource.toInputStream(
public | Creates a new instance. |
public | ReadWriteBufferOutputStream( Creates a new instance with the argument buffer size. |
public int | Gets the number of bytes that can be read without blocking. |
public void | close() Closes this output stream and releases any system resources
associated with this stream. |
public int | read() Reads a single byte from this byte source. |
public ByteArrayRegion | read( Reads a given number of bytes from this byte source. |
public int | read( Reads bytes from this byte source and writes them to the argument buffer. |
public int | read( Reads bytes into the argument byte array, waiting for some if not available. |
public int | read( Reads bytes into the argument range of the specified byte array, waiting for some if not available. |
public long | readFrom( Same as readFrom( |
public long | readFrom( Reads bytes from the argument byte source and writes it to this byte sink. |
public void | write( Writes the specified byte to this output stream. |
public void | write( Writes the bytes contained in the argument byte array to the byte sink. |
public void | write( Writes b.length bytes from the specified byte array
to this output stream. |
public void | write( Writes len bytes from the specified byte array
starting at offset off to this output stream. |
public long | writeTo( Same as writeTo( |
public long | Writes the remaining bytes in this byte source to the specified byte sink.
|
close
is that it closes the output stream. A closed stream cannot perform
output operations and cannot be reopened.
The close
method of OutputStream
does nothing.
This method works similarly to InputStream.read().
This method reads at most the specified number of bytes from this byte source and returns it as a byte array region. The resulting buffer may contain less than the requested number of bytes. This doesn't mean that the end of the stream has been reached, but only that reading more bytes will most likely block.
If a byte array of zero length is returned, that means that the end of the stream is reached (or the requested count was less or equal to 0).
It is recommended that the number of bytes to read is not too large, so a buffer allocated for that size will not cause OutOfMemoryError.
The number of bytes read is based on the buffer length.
This method works similarly to InputStream.read(
RMI method calls to this method is redirected to ByteSource.redirectReadCall(
If there are any available bytes in the stream, they will be copied into the buffer, and returned immediately.
Else the method will wait for bytes to be written to the stream, and will return them accordingly.
null
.If there are any available bytes in the stream, they will be copied into the buffer, and returned immediately.
Else the method will wait for bytes to be written to the stream, and will return them accordingly.
null
.null
.this
byte sink. This method will possibly read all bytes from the argument byte source and all the read bytes will be written to this byte sink. If the argument is a blocking source, then this method will block too.
Calling this method instead of copying the bytes externally can have advantages, as implementations can read the bytes into an internal buffer more efficiently, therefore avoiding unnecessary copying and allocations.
The default implementation calls ByteSource.writeTo(
null
.write
is that one byte is written
to the output stream. The byte to be written is the eight
low-order bits of the argument b
. The 24
high-order bits of b
are ignored.
Subclasses of OutputStream
must provide an
implementation for this method.
byte
.IOException
may be thrown if the
output stream has been closed.
This method works similarly to OutputStream.write(
null
.b.length
bytes from the specified byte array
to this output stream. The general contract for write(b)
is that it should have exactly the same effect as the call
write(b, 0, b.length)
.len
bytes from the specified byte array
starting at offset off
to this output stream. The general contract for write(b, off, len)
is that
some of the bytes in the array b
are written to the
output stream in order; element b[off]
is the first
byte written and b[off+len-1]
is the last byte written
by this operation.
The write
method of OutputStream
calls
the write method of one argument on each of the bytes to be
written out. Subclasses are encouraged to override this method and
provide a more efficient implementation.
If b
is null
, a
NullPointerException
is thrown.
If off
is negative, or len
is negative, or
off+len
is greater than the length of the array
b
, then an IndexOutOfBoundsException is thrown.
IOException
is thrown if the output
stream is closed.null
.This method will take the remaining bytes in this byte source and write it to the specified byte sink. It is expected that after this method finishes, reading from this byte source will not return any bytes.
Warning: This method doesn't return until the stream is closed or the current thread is interrupted, which is signaled by a thrown InterruptedIOException.
null
.