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 booleanThis is used to determine if the accumulator was closed.private intThis counts the number of bytes that have been accumulated.private ResponseEncoderThis is the transfer object used to transfer the response.private booleanThis is used to determine if the accumulate was flushed. -
Constructor Summary
ConstructorsConstructorDescriptionResponseBuffer(BodyObserver observer, Response response, Conversation support, Entity entity) Constructor for theResponseBufferobject.ResponseBuffer(BodyObserver observer, Response response, Conversation support, Channel channel) Constructor for theResponseBufferobject. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()This will flush the buffer to the underlying transport and close the stream.private voidcommit()This will close the underlying transfer object which will notify the server kernel that the next request is read to be processed.voidexpand(int capacity) This is used to expand the capacity of the internal buffer.voidflush()This is used to flush the contents of the buffer to the underlying transport.private voidflush(boolean flush) This is used to flush the contents of the buffer to the underlying transport.booleanisOpen()This is used to determine if the accumulator is still open.voidreset()This is used to reset the buffer so that it can be written to again.voidwrite(byte[] array, int off, int size) This is used to write the provided array to the buffer.voidwrite(int octet) This is used to write the provided octet to the buffer.intwrite(ByteBuffer source) This is used to write the provided buffer to the buffer.intwrite(ByteBuffer source, int off, int size) This is used to write the provided buffer to the buffer.Methods inherited from class 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 theResponseBufferobject. 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 theResponseBufferobject. 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:
writein 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:
writein 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:
writein 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:
flushin interfaceFlushable- Overrides:
flushin 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein 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
-