Class 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 the 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.
    • 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 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.
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.nio.channels.Channel

        isOpen
    • Constructor Detail

      • ByteStreamReader

        public ByteStreamReader()
    • 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 to 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.
        Overrides:
        available in class java.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 to seekToOffset(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. See InputStream.read().
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public abstract int read​(byte[] b)
                          throws java.io.IOException
        This is equivalent to calling read(b, 0, b.length) Will only block if available() is 0. See InputStream.read(byte[]).
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public abstract int read​(byte[] b,
                                 int off,
                                 int len)
                          throws java.io.IOException
        If 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)
        Overrides:
        read in class java.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 to read(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 class java.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 ongoing read() request if there is one. See InputStream.close()
        Specified by:
        close in interface java.nio.channels.AsynchronousChannel
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.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 bytes available() or -1 if the reader has reached the end of a sealed segment.
        Returns:
        A the number of bytes available()