Class BlockingByteBufferOutputStream

java.lang.Object
java.io.OutputStream
io.fusionauth.http.io.BlockingByteBufferOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BlockingByteBufferOutputStream extends OutputStream
This InputStream uses ByteBuffers read by the Server processor and piped into this class. Those ByteBuffers are then fed to the reader of the InputStream. The pushing of ByteBuffers blocks based on the configuration and the reading will block if there are no more bytes to read.

In order to handle chunking and other considerations, this class can be sub-classed to preprocess bytes.

Author:
Brian Pontarelli
  • Constructor Details

    • BlockingByteBufferOutputStream

      public BlockingByteBufferOutputStream(Notifier notifier, int bufferSize, int maximumQueueSize)
  • Method Details

    • clear

      public void clear()
    • close

      public void close()
      Flushes and then marks the stream closed. The flush must occur first so that the readers have access to the buffers before they are aware of the stream's closure.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
    • flush

      public void flush()
      Flushes the current stream contents if the current ByteBuffer has less than 10% remaining space. It flushes by putting the current ByteBuffer into the Queue that the reader thread is reading from. Then it sets the current ByteBuffer to null so that a new one is created. And finally, this notifies the selector to wake up.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
    • hasReadableBuffer

      public boolean hasReadableBuffer()
    • isClosed

      public boolean isClosed()
    • isEmpty

      public boolean isEmpty()
    • readableBuffer

      public ByteBuffer readableBuffer()
      Used by the reader side (the selector/processor) so that bytes can be read from the worker thread and written back to the client. This method should only be called once per buffer. It pops the buffer off the queue.
      Returns:
      A ByteBuffer that is used to read bytes that will be written back to the client or null if there aren't any buffers ready yet.
    • write

      public void write(int b)
      Specified by:
      write in class OutputStream
    • write

      public void write(byte[] b, int off, int len)
      Overrides:
      write in class OutputStream