Class StreamBuffer

java.lang.Object
com.netflix.genie.web.util.StreamBuffer

@ThreadSafe public class StreamBuffer extends 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

    Modifier and Type
    Method
    Description
    void
    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
    Close this buffer before all data is written due to an error.
    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 Details

    • StreamBuffer

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

    • closeForError

      public void closeForError(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:
      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:
      IllegalStateException - if writing is attempted after the buffer has been closed
    • getInputStream

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