public class ThreadSafeByteSource extends Object implements ByteSource
A ByteSource
is usually not thread-safe.
This class provides a thread-safe wrapper - it's safe to call read()/skip()/close()
on any thread at any time.
The main purpose of this class is to allow close() to be called on any thread at any time.
Particularly, close() can be invoked while a read() is pending (which is usually illegal for a ByteSource). The pending read will be cancelled by close().
However, it is still considered a programming error to invoke read()/skip() while a read() is pending. This class checks for such invocations and throws IllegalStateException.
Constructor and Description |
---|
ThreadSafeByteSource(ByteSource origin)
Creates a thread-safe wrapper of the `origin` ByteSource.
|
Instance Methods | |
---|---|
Async<ByteBuffer> |
read()
Read the next chunk of bytes.
|
long |
skip(long n)
Try to skip forward `n` bytes.
|
Async<Void> |
close()
Close this source.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
asString, asString, readAll, toSink
public ThreadSafeByteSource(ByteSource origin)
public Async<ByteBuffer> read() throws IllegalStateException
If this source is closed, this action fails with an
AsynchronousCloseException
. Note: that means the returned Async
will be a failure; not that this method will throw the exception.
read
in interface ByteSource
IllegalStateException
- if there is already a pending read.public long skip(long n) throws IllegalArgumentException, IllegalStateException
This method throws IllegalStateException if there is a read pending.
skip
in interface ByteSource
n
- number of bytes to skip; n >= 0
.0 <= m <= n
.IllegalStateException
- if there is a read pending.IllegalArgumentException
- if n < 0
.public Async<Void> close()
It is OK to call this method while a read is pending; the pending read will be cancelled.
close
in interface ByteSource