Class ResponseBuffer

java.lang.Object
java.io.OutputStream
org.simpleframework.http.core.ResponseBuffer
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Channel, WritableByteChannel

class ResponseBuffer extends OutputStream implements WritableByteChannel
The ResponseBuffer object is an output stream that can buffer bytes written up to a given size. This is used if a buffer is requested for the response output. Such a mechanism allows the response to be written without committing the response. Also it enables content that has been written to be reset, by simply clearing the response buffer. If the response buffer overflows then the response is committed.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private byte[]
    This is the buffer used to accumulate the response bytes.
    private boolean
    This is used to determine if the accumulator was closed.
    private int
    This counts the number of bytes that have been accumulated.
    This is the transfer object used to transfer the response.
    private boolean
    This is used to determine if the accumulate was flushed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ResponseBuffer(BodyObserver observer, Response response, Conversation support, Entity entity)
    Constructor for the ResponseBuffer object.
    ResponseBuffer(BodyObserver observer, Response response, Conversation support, Channel channel)
    Constructor for the ResponseBuffer object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This will flush the buffer to the underlying transport and close the stream.
    private void
    This will close the underlying transfer object which will notify the server kernel that the next request is read to be processed.
    void
    expand(int capacity)
    This is used to expand the capacity of the internal buffer.
    void
    This is used to flush the contents of the buffer to the underlying transport.
    private void
    flush(boolean flush)
    This is used to flush the contents of the buffer to the underlying transport.
    boolean
    This is used to determine if the accumulator is still open.
    void
    This is used to reset the buffer so that it can be written to again.
    void
    write(byte[] array, int off, int size)
    This is used to write the provided array to the buffer.
    void
    write(int octet)
    This is used to write the provided octet to the buffer.
    int
    write(ByteBuffer source)
    This is used to write the provided buffer to the buffer.
    int
    write(ByteBuffer source, int off, int size)
    This is used to write the provided buffer to the buffer.

    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
  • Field Details

    • encoder

      private ResponseEncoder encoder
      This is the transfer object used to transfer the response.
    • buffer

      private byte[] buffer
      This is the buffer used to accumulate the response bytes.
    • flushed

      private boolean flushed
      This is used to determine if the accumulate was flushed.
    • closed

      private boolean closed
      This is used to determine if the accumulator was closed.
    • count

      private int count
      This counts the number of bytes that have been accumulated.
  • Constructor Details

    • ResponseBuffer

      public ResponseBuffer(BodyObserver observer, Response response, Conversation support, Entity entity)
      Constructor for the ResponseBuffer object. This will create a buffering output stream which will flush data to the underlying transport provided with the entity. All I/O events are reported to the monitor so the server can process other requests within the pipeline when the current one is finished.
      Parameters:
      observer - this is used to notify of response completion
      response - this is the response header for this buffer
      support - this is used to determine the response semantics
      entity - this is used to acquire the underlying transport
    • ResponseBuffer

      public ResponseBuffer(BodyObserver observer, Response response, Conversation support, Channel channel)
      Constructor for the ResponseBuffer object. This will create a buffering output stream which will flush data to the underlying transport provided with the channel. All I/O events are reported to the monitor so the server can process other requests within the pipeline when the current one is finished.
      Parameters:
      observer - this is used to notify of response completion
      response - this is the response header for this buffer
      support - this is used to determine the response semantics
      channel - this is the channel used to write the data to
  • Method Details

    • isOpen

      public boolean isOpen()
      This is used to determine if the accumulator is still open. If the accumulator is still open then data can still be written to it and this transmitted to the client. When the accumulator is closed the data is committed and this can not be used.
      Specified by:
      isOpen in interface Channel
      Returns:
      this returns true if the accumulator object is open
    • reset

      public void reset() throws IOException
      This is used to reset the buffer so that it can be written to again. If the accumulator has already been flushed then the stream can not be reset. Resetting the stream is typically done if there is an error in writing the response and an error message is generated to replaced the partial response.
      Throws:
      IOException
    • write

      public void write(int octet) throws IOException
      This is used to write the provided octet to the buffer. If the buffer is full it will be flushed and the octet is appended to the start of the buffer. If however the buffer is zero length then this will write directly to the underlying transport.
      Specified by:
      write in class OutputStream
      Parameters:
      octet - this is the octet that is to be written
      Throws:
      IOException
    • write

      public void write(byte[] array, int off, int size) throws IOException
      This is used to write the provided array to the buffer. If the buffer is full it will be flushed and the array is appended to the start of the buffer. If however the buffer is zero length then this will write directly to the underlying transport.
      Overrides:
      write in class OutputStream
      Parameters:
      array - this is the array of bytes to send to the client
      off - this is the offset within the array to send from
      size - this is the number of bytes that are to be sent
      Throws:
      IOException
    • write

      public int write(ByteBuffer source) throws IOException
      This is used to write the provided buffer to the buffer. If the buffer is full it will be flushed and the buffer is appended to the start of the buffer. If however the buffer is zero length then this will write directly to the underlying transport.
      Specified by:
      write in interface WritableByteChannel
      Parameters:
      source - this is the byte buffer to send to the client
      Returns:
      this returns the number of bytes that have been sent
      Throws:
      IOException
    • write

      public int write(ByteBuffer source, int off, int size) throws IOException
      This is used to write the provided buffer to the buffer. If the buffer is full it will be flushed and the buffer is appended to the start of the buffer. If however the buffer is zero length then this will write directly to the underlying transport.
      Parameters:
      source - this is the byte buffer to send to the client
      off - this is the offset within the array to send from
      size - this is the number of bytes that are to be sent
      Returns:
      this returns the number of bytes that have been sent
      Throws:
      IOException
    • expand

      public void expand(int capacity) throws IOException
      This is used to expand the capacity of the internal buffer. If there is already content that has been appended to the buffer this will copy that data to the newly created buffer. This will not decrease the size of the buffer if it is larger than the requested capacity.
      Parameters:
      capacity - this is the capacity to expand the buffer to
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      This is used to flush the contents of the buffer to the underlying transport. Once the accumulator is flushed the HTTP headers are written such that the semantics of the connection match the protocol version and the existing response headers.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • flush

      private void flush(boolean flush) throws IOException
      This is used to flush the contents of the buffer to the underlying transport. Once the accumulator is flushed the HTTP headers are written such that the semantics of the connection match the protocol version and the existing response headers.
      Parameters:
      flush - indicates whether the transport should be flushed
      Throws:
      IOException
    • close

      public void close() throws IOException
      This will flush the buffer to the underlying transport and close the stream. Once the accumulator is flushed the HTTP headers are written such that the semantics of the connection match the protocol version and the existing response headers. Closing this stream does not mean the connection is closed.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • commit

      private void commit() throws IOException
      This will close the underlying transfer object which will notify the server kernel that the next request is read to be processed. If the accumulator is unflushed then this will set a Content-Length header such that it matches the number of bytes that are buffered within the internal buffer.
      Throws:
      IOException