Class ResponseBuffer

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, java.nio.channels.Channel, java.nio.channels.WritableByteChannel

    class ResponseBuffer
    extends java.io.OutputStream
    implements java.nio.channels.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:
    ResponseEncoder
    • Field Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      This will flush the buffer to the underlying transport and close the stream.
      private void commit()
      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 flush()
      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 isOpen()
      This is used to determine if the accumulator is still open.
      void reset()
      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​(java.nio.ByteBuffer source)
      This is used to write the provided buffer to the buffer.
      int write​(java.nio.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 Detail

      • 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 Detail

      • 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 Detail

      • 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 java.nio.channels.Channel
        Returns:
        this returns true if the accumulator object is open
      • reset

        public void reset()
                   throws java.io.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:
        java.io.IOException
      • write

        public void write​(int octet)
                   throws java.io.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 java.io.OutputStream
        Parameters:
        octet - this is the octet that is to be written
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] array,
                          int off,
                          int size)
                   throws java.io.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 java.io.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:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer source)
                  throws java.io.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 java.nio.channels.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:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer source,
                         int off,
                         int size)
                  throws java.io.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:
        java.io.IOException
      • expand

        public void expand​(int capacity)
                    throws java.io.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:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.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 java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        private void flush​(boolean flush)
                    throws java.io.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:
        java.io.IOException
      • close

        public void close()
                   throws java.io.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 java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • commit

        private void commit()
                     throws java.io.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:
        java.io.IOException