Package org.simpleframework.http.core
Class ResponseEncoder
java.lang.Object
org.simpleframework.http.core.ResponseEncoder
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BodyEncoder
Once the header is committed this is used to produce data.private BodyEncoderFactory
This is used to create a encoder based on the HTTP headers.private Response
This is the response message that is to be committed.private Conversation
This is used to determine the type of transfer required.private Trace
This is the trace used to monitor events in the data transfer. -
Constructor Summary
ConstructorsConstructorDescriptionResponseEncoder
(BodyObserver observer, Response response, Conversation support, Channel channel) Constructor for theResponseEncoder
object, this is used to create an object used to transfer a response body. -
Method Summary
Modifier and TypeMethodDescriptionprivate 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
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
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
(ByteBuffer buffer) This method is used to write content to the underlying socket.void
write
(ByteBuffer buffer, int off, int len) This method is used to write content to the underlying socket.
-
Field Details
-
factory
This is used to create a encoder based on the HTTP headers. -
support
This is used to determine the type of transfer required. -
response
This is the response message that is to be committed. -
encoder
Once the header is committed this is used to produce data. -
trace
This is the trace used to monitor events in the data transfer.
-
-
Constructor Details
-
ResponseEncoder
public ResponseEncoder(BodyObserver observer, Response response, Conversation support, Channel channel) Constructor for theResponseEncoder
object, this is used to create an object used to transfer a response body. This must be given aConversation
that can be used to set and get information regarding the type of transfer required.- Parameters:
observer
- this is used to signal for response completionresponse
- this is the actual response messagesupport
- this is used to determine the semanticschannel
- this is the connected TCP channel for the response
-
-
Method Details
-
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
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:
IOException
-
start
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:
IOException
-
write
This method is used to write content to the underlying socket. This will make use of theProducer
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:
IOException
-
write
This method is used to write content to the underlying socket. This will make use of theProducer
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 clientoff
- this is the offset within the array to send fromlen
- this is the number of bytes that are to be sent- Throws:
IOException
-
write
This method is used to write content to the underlying socket. This will make use of theProducer
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:
IOException
-
write
This method is used to write content to the underlying socket. This will make use of theProducer
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 clientoff
- this is the offset within the buffer to send fromlen
- this is the number of bytes that are to be sent- Throws:
IOException
-
flush
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:
IOException
-
close
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:
IOException
-
configure
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:
IOException
-
configure
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:
IOException
-
configure
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 transferredlength
- this is the actual length value to be used- Throws:
IOException
-
clear
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:
IOException
-
commit
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:
IOException
-