Class StreamBuffer


  • @ThreadSafe
    public class StreamBuffer
    extends java.lang.Object
    A temporary in-memory structure to hold in-transit data. Provides an InputStream for reading, reading blocks until data becomes available or the buffer is closed.

    To avoid in-memory data growing excessively, this buffer stores a single "chunk" at the time. Only after a chunk is consumed, a new one can be appended.

    To support range requests in a memory-efficient way, StreamBuffer.StreamBufferInputStream also allows skipping the first skipOffset - 1 bytes without allocating memory (or worse: downloading the actual bytes only to have them thrown away to get to the actual range)

    Since:
    4.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamBuffer​(long skipOffset)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void closeForCompleted()
      Close this buffer because all expected data has been written Reading will return the end of stream marker after all data has been consumed.
      void closeForError​(java.lang.Throwable t)
      Close this buffer before all data is written due to an error.
      java.io.InputStream getInputStream()
      Obtain the input stream to read this data.
      boolean tryWrite​(com.google.protobuf.ByteString data)
      Try to append a chunk of data for consumption.
      void write​(com.google.protobuf.ByteString data)
      Append a chunk of data for consumption.
      • Methods inherited from class java.lang.Object

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

      • StreamBuffer

        public StreamBuffer​(long skipOffset)
        Constructor.
        Parameters:
        skipOffset - index of the first actual byte to return (
    • Method Detail

      • closeForError

        public void closeForError​(java.lang.Throwable t)
        Close this buffer before all data is written due to an error. Reading will return the end of stream marker after the current chunk (if any) has been consumed.
        Parameters:
        t - the cause for the buffer to be closed.
      • closeForCompleted

        public void closeForCompleted()
        Close this buffer because all expected data has been written Reading will return the end of stream marker after all data has been consumed.
      • write

        public void write​(com.google.protobuf.ByteString data)
        Append a chunk of data for consumption. This call may block and not return until some data is read/consumed.
        Parameters:
        data - the data to write into the buffer
        Throws:
        java.lang.IllegalStateException - if writing is attempted after the buffer has been closed
      • tryWrite

        public boolean tryWrite​(com.google.protobuf.ByteString data)
        Try to append a chunk of data for consumption. If the previous buffer is still not drained, then does not block and returns false.
        Parameters:
        data - the data to write into the buffer
        Returns:
        true if the data was added to the buffer, false otherwise
        Throws:
        java.lang.IllegalStateException - if writing is attempted after the buffer has been closed
      • getInputStream

        public java.io.InputStream getInputStream()
        Obtain the input stream to read this data.
        Returns:
        the input stream
        Throws:
        java.lang.IllegalStateException - if invoked multiple times