Package io.pravega.client.byteStream
Class ByteStreamReader
- java.lang.Object
-
- java.io.InputStream
-
- io.pravega.client.byteStream.ByteStreamReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.AsynchronousChannel
,java.nio.channels.Channel
@ThreadSafe public abstract class ByteStreamReader extends java.io.InputStream implements java.nio.channels.AsynchronousChannel, java.lang.AutoCloseable
Allows for reading raw bytes from a segment. This class is designed such that it can be used with or without blocking. To avoid blocking use theonDataAvailable()
method to make sure to only callread(byte[])
when there is dataavailable()
. It is safe to invoke methods on this class from multiple threads, but doing so will not increase performance.
-
-
Constructor Summary
Constructors Constructor Description ByteStreamReader()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method 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 callingread(b, 0, b.length)
Will only block ifavailable()
is 0.abstract int
read(byte[] b, int off, int len)
Ifavailable()
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 toread(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.-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, transferTo
-
-
-
-
Method Detail
-
getOffset
public abstract long getOffset()
Returns the current byte offset in the segment. This call does not block.- Returns:
- the current byte offset in the segment.
-
seekToOffset
public abstract void seekToOffset(long offset)
Seeks to the provided offset (It can be anywhere in the segment). Future read calls will read from this offset. Future reads will proceed from this offset.- Parameters:
offset
- The offset to seek to.
-
available
public abstract int available()
Returns the number of bytes that can be read without blocking. If the number returned is greater than 0 then a call toread(byte[])
will return data from memory without blocking. If the number returned is 0 thenread(byte[])
will block. If -1 is returned this indicates the end of the stream has been reached and a call toread(byte[])
will return -1.- Overrides:
available
in classjava.io.InputStream
- Returns:
- the number of bytes that can be read without blocking.
- See Also:
InputStream.available()
-
fetchTailOffset
public abstract long fetchTailOffset()
This make an RPC to the server to fetch the offset at which new bytes would be written. This is the same as the length of the segment (assuming no truncation). This offset can also be passed toseekToOffset(long)
to only read bytes from this point forward.- Returns:
- The tail offset.
-
read
public abstract int read() throws java.io.IOException
Reads a single byte. Avoid this API if possible as it is very wasteful. SeeInputStream.read()
.- Specified by:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public abstract int read(byte[] b) throws java.io.IOException
This is equivalent to callingread(b, 0, b.length)
Will only block ifavailable()
is 0. SeeInputStream.read(byte[])
.- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public abstract int read(byte[] b, int off, int len) throws java.io.IOException
Ifavailable()
is non-zero, this method will read bytes from an in-memory buffer into the provided array. Ifavailable()
is zero will wait for additional data to arrive and then fill the provided array. This method will only block ifavailable()
is 0. In which case it will block until some data arrives and return that. (Which may or may not fill the provided buffer) SeeInputStream.read(byte[], int, int)
- Overrides:
read
in classjava.io.InputStream
- Returns:
- The number of bytes copied into the provided buffer. Or -1 if the segment is sealed and there are no more bytes to read.
- Throws:
java.io.IOException
-
read
public abstract int read(java.nio.ByteBuffer dst) throws java.io.IOException
Similar toread(byte[], int, int)
but takes a byteBuffer.- Parameters:
dst
- the destination buffer to read into.- Returns:
- The number of bytes copied into the provided buffer. Or -1 if the segment is sealed and there are no more bytes to read.
- Throws:
java.io.IOException
- If the stream cannot be read from for any reason including if truncation has deleted the data.
-
skip
public abstract long skip(long n) throws java.io.IOException
This method attempts to skip forward by the provided number of bytes. If it is not possible to skip forward `n` bytes (because there are less than `n` bytes remaining, it will skip as many as possible and return the number skipped. This method is not affected by truncation.- Overrides:
skip
in classjava.io.InputStream
- Parameters:
n
- number of bytes to skip.- Returns:
- number of bytes skipped.
- Throws:
java.io.IOException
- Thrown if an IOError occurs while attempting to obtain the length of the stream.
-
close
public abstract void close()
Closes the reader. This may block on an ongoingread()
request if there is one. SeeInputStream.close()
- Specified by:
close
in interfacejava.nio.channels.AsynchronousChannel
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
-
onDataAvailable
public abstract java.util.concurrent.CompletableFuture<java.lang.Integer> onDataAvailable()
Returns a future that will be completed when there is data available to be read. The Integer in the result will be the number of bytesavailable()
or -1 if the reader has reached the end of a sealed segment.- Returns:
- A the number of bytes
available()
-
-