Package org.simpleframework.http.core
Class ResponseBuffer
java.lang.Object
java.io.OutputStream
org.simpleframework.http.core.ResponseBuffer
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
,Channel
,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
FieldsModifier and TypeFieldDescriptionprivate 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.private ResponseEncoder
This is the transfer object used to transfer the response.private boolean
This is used to determine if the accumulate was flushed. -
Constructor Summary
ConstructorsConstructorDescriptionResponseBuffer
(BodyObserver observer, Response response, Conversation support, Entity entity) Constructor for theResponseBuffer
object.ResponseBuffer
(BodyObserver observer, Response response, Conversation support, Channel channel) Constructor for theResponseBuffer
object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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
(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
-
Field Details
-
encoder
This is the transfer object used to transfer the response. -
buffer
private byte[] bufferThis is the buffer used to accumulate the response bytes. -
flushed
private boolean flushedThis is used to determine if the accumulate was flushed. -
closed
private boolean closedThis is used to determine if the accumulator was closed. -
count
private int countThis counts the number of bytes that have been accumulated.
-
-
Constructor Details
-
ResponseBuffer
public ResponseBuffer(BodyObserver observer, Response response, Conversation support, Entity entity) Constructor for theResponseBuffer
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 completionresponse
- this is the response header for this buffersupport
- this is used to determine the response semanticsentity
- this is used to acquire the underlying transport
-
ResponseBuffer
public ResponseBuffer(BodyObserver observer, Response response, Conversation support, Channel channel) Constructor for theResponseBuffer
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 completionresponse
- this is the response header for this buffersupport
- this is used to determine the response semanticschannel
- 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. -
reset
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
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 classOutputStream
- Parameters:
octet
- this is the octet that is to be written- Throws:
IOException
-
write
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 classOutputStream
- Parameters:
array
- this is the array of bytes to send to the clientoff
- this is the offset within the array to send fromsize
- this is the number of bytes that are to be sent- Throws:
IOException
-
write
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 interfaceWritableByteChannel
- 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
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 clientoff
- this is the offset within the array to send fromsize
- 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
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
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 interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
flush
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
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 interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
commit
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
-