@ThreadSafe
public abstract class ByteStreamReader
extends java.io.InputStream
implements java.nio.channels.AsynchronousChannel, java.lang.AutoCloseable
onDataAvailable()
method to make sure to
only call read(byte[])
when there is data available()
.
It is safe to invoke methods on this class from multiple threads, but doing so will not increase
performance.Constructor and Description |
---|
ByteStreamReader() |
Modifier and Type | Method and Description |
---|---|
abstract int |
available()
Returns the number of bytes that can be read without blocking.
|
abstract void |
close()
Closes the reader.
|
abstract long |
fetchTailOffset()
This make an RPC to the server to fetch the offset at which new bytes would be written.
|
abstract long |
getOffset()
Returns the current byte offset in the segment.
|
abstract java.util.concurrent.CompletableFuture<java.lang.Integer> |
onDataAvailable()
Returns a future that will be completed when there is data available to be read.
|
abstract int |
read()
Reads a single byte.
|
abstract int |
read(byte[] b)
This is equivalent to calling
read(b, 0, b.length)
Will only block if available() is 0. |
abstract int |
read(byte[] b,
int off,
int len)
If
available() is non-zero, this method will read bytes from an in-memory buffer into the
provided array. |
abstract int |
read(java.nio.ByteBuffer dst)
Similar to
read(byte[], int, int) but takes a byteBuffer. |
abstract void |
seekToOffset(long offset)
Seeks to the provided offset (It can be anywhere in the segment).
|
abstract long |
skip(long n)
This method attempts to skip forward by the provided number of bytes.
|
public abstract long getOffset()
public abstract void seekToOffset(long offset)
offset
- The offset to seek to.public abstract int available()
read(byte[])
will return data from memory without blocking. If the
number returned is 0 then read(byte[])
will block. If -1 is returned this indicates
the end of the stream has been reached and a call to read(byte[])
will return -1.available
in class java.io.InputStream
InputStream.available()
public abstract long fetchTailOffset()
seekToOffset(long)
to only read bytes from this point forward.public abstract int read() throws java.io.IOException
InputStream.read()
.read
in class java.io.InputStream
java.io.IOException
public abstract int read(byte[] b) throws java.io.IOException
read(b, 0, b.length)
Will only block if available()
is 0.
See InputStream.read(byte[])
.read
in class java.io.InputStream
java.io.IOException
public abstract int read(byte[] b, int off, int len) throws java.io.IOException
available()
is non-zero, this method will read bytes from an in-memory buffer into the
provided array. If available()
is zero will wait for additional data to arrive and
then fill the provided array. This method will only block if available()
is 0. In
which case it will block until some data arrives and return that. (Which may or may not fill
the provided buffer)
See InputStream.read(byte[], int, int)
read
in class java.io.InputStream
java.io.IOException
public abstract int read(java.nio.ByteBuffer dst) throws java.io.IOException
read(byte[], int, int)
but takes a byteBuffer.dst
- the destination buffer to read into.java.io.IOException
- If the stream cannot be read from for any reason including if truncation
has deleted the data.public abstract long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
n
- number of bytes to skip.java.io.IOException
- Thrown if an IOError occurs while attempting to obtain the length of the
stream.public abstract void close()
read()
request if there is one.
See InputStream.close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in interface java.nio.channels.AsynchronousChannel
close
in interface java.nio.channels.Channel
close
in class java.io.InputStream
public abstract java.util.concurrent.CompletableFuture<java.lang.Integer> onDataAvailable()
available()
or -1 if the reader has
reached the end of a sealed segment.available()