Class BufferedByteStreamWriterImpl

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class BufferedByteStreamWriterImpl
    extends ByteStreamWriter
    This class buffers individual calls to write(int) so that we don't make a separate RPC per byte. It attempts to do this in a Lazy way, not allocating buffer space unless it is needed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Flushes the buffer and closes the writer.
      void closeAndSeal()
      Closes the writer similar to ByteStreamWriter.close() but also seals it so that no future writes can ever be made.
      long fetchHeadOffset()
      This makes a synchronous RPC call to the server to obtain the current head of the stream.
      long fetchTailOffset()
      This makes a synchronous RPC call to the server to obtain the total number of bytes written to the segment in its history.
      void flush()
      Blocks until all data written has been durably persisted.
      java.util.concurrent.CompletableFuture<java.lang.Void> flushAsync()
      The future will complete successfully when all data which was passed to the write prior to calling this method has been persisted, and will complete exceptionally if that is not possible such as for example if the segment is sealed.
      void truncateDataBefore​(long offset)
      This makes a synchronous RPC call to the server to truncate the segment at the provided offset.
      void write​(byte[] b, int off, int len)
      Writes the provided data to the segment.
      void write​(int b)  
      void write​(java.nio.ByteBuffer src)
      Similar to ByteStreamWriter.write(byte[], int, int) Writes the provided data to the segment.
      • Methods inherited from class java.io.OutputStream

        nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BufferedByteStreamWriterImpl

        @ConstructorProperties("out")
        public BufferedByteStreamWriterImpl​(@NonNull
                                            @NonNull ByteStreamWriterImpl out)
    • Method Detail

      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(java.nio.ByteBuffer src)
                   throws java.io.IOException
        Description copied from class: ByteStreamWriter
        Similar to ByteStreamWriter.write(byte[], int, int) Writes the provided data to the segment. The data is buffered internally to avoid blocking. As such it cannot be assumed to be durably stored until a flush completes. It is intended that this method not block, but it may in the event that the server becomes disconnected for sufficiently long or is sufficiently slow that that backlog of data to be written becomes a memory issue.
        Specified by:
        write in class ByteStreamWriter
        Parameters:
        src - The bytes to write.
        Throws:
        java.io.IOException - If for any reason an error occurs writing the data, including if the stream is sealed.
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Description copied from class: ByteStreamWriter
        Writes the provided data to the segment. The data is buffered internally to avoid blocking. As such it cannot be assumed to be durably stored until a flush completes. It is intended that this method not block, but it may in the event that the server becomes disconnected for sufficiently long or is sufficiently slow that that backlog of data to be written becomes a memory issue.
        Specified by:
        write in class ByteStreamWriter
        Throws:
        java.io.IOException
        See Also:
        OutputStream.write(byte[], int, int)
      • close

        public void close()
                   throws java.io.IOException
        Description copied from class: ByteStreamWriter
        Flushes the buffer and closes the writer. If there is data to flush, this is a blocking method.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class ByteStreamWriter
        Throws:
        java.io.IOException
        See Also:
        OutputStream.close()
      • flush

        public void flush()
                   throws java.io.IOException
        Description copied from class: ByteStreamWriter
        Blocks until all data written has been durably persisted.
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class ByteStreamWriter
        Throws:
        java.io.IOException - If for any reason the flush fails including if the stream is sealed.
        See Also:
        OutputStream.flush()
      • flushAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> flushAsync()
                                                                          throws java.io.IOException
        Description copied from class: ByteStreamWriter
        The future will complete successfully when all data which was passed to the write prior to calling this method has been persisted, and will complete exceptionally if that is not possible such as for example if the segment is sealed.
        Specified by:
        flushAsync in class ByteStreamWriter
        Returns:
        The future related to last write
        Throws:
        java.io.IOException - If for any reason the flush fails including if the stream is sealed.
        See Also:
        OutputStream.flush()
      • closeAndSeal

        public void closeAndSeal()
                          throws java.io.IOException
        Description copied from class: ByteStreamWriter
        Closes the writer similar to ByteStreamWriter.close() but also seals it so that no future writes can ever be made.
        Specified by:
        closeAndSeal in class ByteStreamWriter
        Throws:
        java.io.IOException - If for any reason the flush fails including if the stream is sealed.
      • fetchHeadOffset

        public long fetchHeadOffset()
        Description copied from class: ByteStreamWriter
        This makes a synchronous RPC call to the server to obtain the current head of the stream.
        Specified by:
        fetchHeadOffset in class ByteStreamWriter
        Returns:
        The current head offset
      • fetchTailOffset

        public long fetchTailOffset()
        Description copied from class: ByteStreamWriter
        This makes a synchronous RPC call to the server to obtain the total number of bytes written to the segment in its history. This is the sum total of the bytes written in all calls to OutputStream.write(byte[]) that have been flushed. It does not include data that was passed to OutputStream.write(byte[]) but which has not yet been persisted.
        Specified by:
        fetchTailOffset in class ByteStreamWriter
        Returns:
        The tail offset
      • truncateDataBefore

        public void truncateDataBefore​(long offset)
        Description copied from class: ByteStreamWriter
        This makes a synchronous RPC call to the server to truncate the segment at the provided offset.
        Specified by:
        truncateDataBefore in class ByteStreamWriter
        Parameters:
        offset - The truncation offset.