Class ByteStreamReaderImpl
- java.lang.Object
-
- java.io.InputStream
-
- io.pravega.client.byteStream.ByteStreamReader
-
- io.pravega.client.byteStream.impl.ByteStreamReaderImpl
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.AsynchronousChannel,java.nio.channels.Channel
public class ByteStreamReaderImpl extends ByteStreamReader
-
-
Constructor Summary
Constructors Constructor Description ByteStreamReaderImpl(@NonNull SegmentInputStream input, @NonNull SegmentMetadataClient meta)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns the number of bytes that can be read without blocking.voidclose()Closes the reader.longfetchTailOffset()This make an RPC to the server to fetch the offset at which new bytes would be written.longgetOffset()Returns the current byte offset in the segment.booleanisOpen()java.util.concurrent.CompletableFuture<java.lang.Integer>onDataAvailable()Returns a future that will be completed when there is data available to be read.intread()Reads a single byte.intread(byte[] b)This is equivalent to callingread(b, 0, b.length)Will only block ifByteStreamReader.available()is 0.intread(byte[] b, int off, int len)IfByteStreamReader.available()is non-zero, this method will read bytes from an in-memory buffer into the provided array.intread(java.nio.ByteBuffer dst)Similar toByteStreamReader.read(byte[], int, int)but takes a byteBuffer.voidseekToOffset(long offset)Seeks to the provided offset (It can be anywhere in the segment).longskip(long toSkip)This method attempts to skip forward by the provided number of bytes.java.lang.StringtoString()
-
-
-
Constructor Detail
-
ByteStreamReaderImpl
@ConstructorProperties({"input","meta"}) public ByteStreamReaderImpl(@NonNull @NonNull SegmentInputStream input, @NonNull @NonNull SegmentMetadataClient meta)
-
-
Method Detail
-
isOpen
public boolean isOpen()
-
getOffset
public long getOffset()
Description copied from class:ByteStreamReaderReturns the current byte offset in the segment. This call does not block.- Specified by:
getOffsetin classByteStreamReader- Returns:
- the current byte offset in the segment.
-
seekToOffset
public void seekToOffset(long offset)
Description copied from class:ByteStreamReaderSeeks 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.- Specified by:
seekToOffsetin classByteStreamReader- Parameters:
offset- The offset to seek to.
-
available
public int available()
Description copied from class:ByteStreamReaderReturns the number of bytes that can be read without blocking. If the number returned is greater than 0 then a call toByteStreamReader.read(byte[])will return data from memory without blocking. If the number returned is 0 thenByteStreamReader.read(byte[])will block. If -1 is returned this indicates the end of the stream has been reached and a call toByteStreamReader.read(byte[])will return -1.- Specified by:
availablein classByteStreamReader- Returns:
- the number of bytes that can be read without blocking.
- See Also:
InputStream.available()
-
close
public void close()
Description copied from class:ByteStreamReaderCloses the reader. This may block on an ongoingByteStreamReader.read()request if there is one. SeeInputStream.close()- Specified by:
closein interfacejava.nio.channels.AsynchronousChannel- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classByteStreamReader
-
fetchTailOffset
public long fetchTailOffset()
Description copied from class:ByteStreamReaderThis 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 toByteStreamReader.seekToOffset(long)to only read bytes from this point forward.- Specified by:
fetchTailOffsetin classByteStreamReader- Returns:
- The tail offset.
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOExceptionDescription copied from class:ByteStreamReaderSimilar toByteStreamReader.read(byte[], int, int)but takes a byteBuffer.- Specified by:
readin classByteStreamReader- 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.
-
read
public int read() throws java.io.IOExceptionDescription copied from class:ByteStreamReaderReads a single byte. Avoid this API if possible as it is very wasteful. SeeInputStream.read().- Specified by:
readin classByteStreamReader- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOExceptionDescription copied from class:ByteStreamReaderThis is equivalent to callingread(b, 0, b.length)Will only block ifByteStreamReader.available()is 0. SeeInputStream.read(byte[]).- Specified by:
readin classByteStreamReader- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionDescription copied from class:ByteStreamReaderIfByteStreamReader.available()is non-zero, this method will read bytes from an in-memory buffer into the provided array. IfByteStreamReader.available()is zero will wait for additional data to arrive and then fill the provided array. This method will only block ifByteStreamReader.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) SeeInputStream.read(byte[], int, int)- Specified by:
readin classByteStreamReader- 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
-
skip
public long skip(long toSkip)
Description copied from class:ByteStreamReaderThis 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.- Specified by:
skipin classByteStreamReader- Parameters:
toSkip- number of bytes to skip.- Returns:
- number of bytes skipped.
-
onDataAvailable
public java.util.concurrent.CompletableFuture<java.lang.Integer> onDataAvailable()
Description copied from class:ByteStreamReaderReturns a future that will be completed when there is data available to be read. The Integer in the result will be the number of bytesByteStreamReader.available()or -1 if the reader has reached the end of a sealed segment.- Specified by:
onDataAvailablein classByteStreamReader- Returns:
- A the number of bytes
ByteStreamReader.available()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-