Class ResponseEncoder


  • class ResponseEncoder
    extends java.lang.Object
    The ResponseEncoder object acts as a means to determine the transfer encoding for the response body. This will ensure that the correct HTTP headers are used when the transfer of the body begins. In order to determine what headers to use this can be provided with a content length value. If the start method is provided with the content length then the HTTP headers will use a Content-Length header as the message delimiter. If there is no content length provided then the chunked encoding is used for HTTP/1.1 and connection close is used for HTTP/1.0.
    See Also:
    BodyEncoder
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private BodyEncoder encoder
      Once the header is committed this is used to produce data.
      private BodyEncoderFactory factory
      This is used to create a encoder based on the HTTP headers.
      private Response response
      This is the response message that is to be committed.
      private Conversation support
      This is used to determine the type of transfer required.
      private Trace trace
      This is the trace used to monitor events in the data transfer.
    • Constructor Summary

      Constructors 
      Constructor Description
      ResponseEncoder​(BodyObserver observer, Response response, Conversation support, Channel channel)
      Constructor for the ResponseEncoder object, this is used to create an object used to transfer a response body.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void clear()
      This is used to clear any previous encoding that has been set in the event that content length may be used instead.
      void close()
      This is used to signal to the encoder that all content has been written and the user no longer needs to write.
      private void commit()
      This is used to compose the HTTP header and send it over the transport to the client.
      private void configure()
      This method is used to set the required HTTP headers on the response.
      private void configure​(long count)
      This method is used to set the required HTTP headers on the response.
      private void configure​(long count, long length)
      This method is used to set the required HTTP headers on the response.
      void flush()
      This method is used to flush the contents of the buffer to the client.
      boolean isStarted()
      This is used to determine if the transfer has started.
      void start()
      This starts the transfer with no specific content length set.
      void start​(int length)
      This starts the transfer with a known content length.
      void write​(byte[] array)
      This method is used to write content to the underlying socket.
      void write​(byte[] array, int off, int len)
      This method is used to write content to the underlying socket.
      void write​(java.nio.ByteBuffer buffer)
      This method is used to write content to the underlying socket.
      void write​(java.nio.ByteBuffer buffer, int off, int len)
      This method is used to write content to the underlying socket.
      • Methods inherited from class java.lang.Object

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

      • factory

        private BodyEncoderFactory factory
        This is used to create a encoder based on the HTTP headers.
      • support

        private Conversation support
        This is used to determine the type of transfer required.
      • response

        private Response response
        This is the response message that is to be committed.
      • encoder

        private BodyEncoder encoder
        Once the header is committed this is used to produce data.
      • trace

        private Trace trace
        This is the trace used to monitor events in the data transfer.
    • Constructor Detail

      • ResponseEncoder

        public ResponseEncoder​(BodyObserver observer,
                               Response response,
                               Conversation support,
                               Channel channel)
        Constructor for the ResponseEncoder object, this is used to create an object used to transfer a response body. This must be given a Conversation that can be used to set and get information regarding the type of transfer required.
        Parameters:
        observer - this is used to signal for response completion
        response - this is the actual response message
        support - this is used to determine the semantics
        channel - this is the connected TCP channel for the response
    • Method Detail

      • isStarted

        public boolean isStarted()
        This is used to determine if the transfer has started. It has started when a encoder is created and the HTTP headers have been sent, or at least handed to the underlying transport. Once started the semantics of the connection can not change.
        Returns:
        this returns whether the transfer has started
      • start

        public void start()
                   throws java.io.IOException
        This starts the transfer with no specific content length set. This is typically used when dynamic data is emitted ans will require chunked encoding for HTTP/1.1 and connection close for HTTP/1.0. Once invoked the HTTP headers are committed.
        Throws:
        java.io.IOException
      • start

        public void start​(int length)
                   throws java.io.IOException
        This starts the transfer with a known content length. This is used when there is a Content-Length header set. This will not encode the content for HTTP/1.1 however, HTTP/1.0 may need a connection close if it does not have keep alive semantics.
        Parameters:
        length - this is the length of the response body
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] array)
                   throws java.io.IOException
        This method is used to write content to the underlying socket. This will make use of the Producer object to encode the response body as required. If the encoder has not been created then this will throw an exception.
        Parameters:
        array - this is the array of bytes to send to the client
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] array,
                          int off,
                          int len)
                   throws java.io.IOException
        This method is used to write content to the underlying socket. This will make use of the Producer object to encode the response body as required. If the encoder has not been created then this will throw an exception.
        Parameters:
        array - this is the array of bytes to send to the client
        off - this is the offset within the array to send from
        len - this is the number of bytes that are to be sent
        Throws:
        java.io.IOException
      • write

        public void write​(java.nio.ByteBuffer buffer)
                   throws java.io.IOException
        This method is used to write content to the underlying socket. This will make use of the Producer object to encode the response body as required. If the encoder has not been created then this will throw an exception.
        Parameters:
        buffer - this is the buffer of bytes to send to the client
        Throws:
        java.io.IOException
      • write

        public void write​(java.nio.ByteBuffer buffer,
                          int off,
                          int len)
                   throws java.io.IOException
        This method is used to write content to the underlying socket. This will make use of the Producer object to encode the response body as required. If the encoder has not been created then this will throw an exception.
        Parameters:
        buffer - this is the buffer of bytes to send to the client
        off - this is the offset within the buffer to send from
        len - this is the number of bytes that are to be sent
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        This method is used to flush the contents of the buffer to the client. This method will block until such time as all of the data has been sent to the client. If at any point there is an error sending the content an exception is thrown.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        This is used to signal to the encoder that all content has been written and the user no longer needs to write. This will either close the underlying transport or it will notify the monitor that the response has completed and the next request can begin. This ensures the content is flushed to the client.
        Throws:
        java.io.IOException
      • configure

        private void configure()
                        throws java.io.IOException
        This method is used to set the required HTTP headers on the response. This will check the existing HTTP headers, and if there is insufficient data chunked encoding will be used for HTTP/1.1 and connection close will be used for HTTP/1.0.
        Throws:
        java.io.IOException
      • configure

        private void configure​(long count)
                        throws java.io.IOException
        This method is used to set the required HTTP headers on the response. This will check the existing HTTP headers, and if there is insufficient data chunked encoding will be used for HTTP/1.1 and connection close will be used for HTTP/1.0.
        Parameters:
        count - this is the number of bytes to be transferred
        Throws:
        java.io.IOException
      • configure

        private void configure​(long count,
                               long length)
                        throws java.io.IOException
        This method is used to set the required HTTP headers on the response. This will check the existing HTTP headers, and if there is insufficient data chunked encoding will be used for HTTP/1.1 and connection close will be used for HTTP/1.0.
        Parameters:
        count - this is the number of bytes to be transferred
        length - this is the actual length value to be used
        Throws:
        java.io.IOException
      • clear

        private void clear()
                    throws java.io.IOException
        This is used to clear any previous encoding that has been set in the event that content length may be used instead. This is used so that an override can be made to the transfer encoding such that content length can be used instead.
        Throws:
        java.io.IOException
      • commit

        private void commit()
                     throws java.io.IOException
        This is used to compose the HTTP header and send it over the transport to the client. Once done the response is committed and no more headers can be set, also the semantics of the response have been committed and the encoder is created.
        Throws:
        java.io.IOException