Package org.simpleframework.transport
Class SocketBuffer
java.lang.Object
org.simpleframework.transport.SocketBuffer
The
SocketBuffer
represents a buffer that aggregates
small fragments in to a single buffer before sending them. This
is primarily used as a means to avoid sending many small packets
rather than reasonable size ones for performance. This also
enables a higher level of concurrency, as it will allow data
that can't be sent over the socket to be buffered until it gets
the signal that says it can be sent on.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate SocketBufferAppender
This is a small internal buffer to collect fragments.private SocketChannel
This is the underlying socket to sent to the data over.private int
This is the recommended minimum packet size to send.private boolean
This is used to determine if the buffer was closed.private ByteBuffer
This is a reference to the last buffer to be sent.private Trace
This is used to trace various events that occur. -
Constructor Summary
ConstructorsConstructorDescriptionSocketBuffer
(Socket socket, int chunk, int limit) Constructor for theSocketBuffer
object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
This is used to close the writer and the underlying socket.private void
compact()
To ensure that we can release any references and thus avoid a blocking thread this method will attempt to merge references in to the internal buffer.boolean
flush()
This method is used to fully flush the contents of the buffer to the underlying output stream.private boolean
flush
(ByteBuffer segment) This write method will write the contents of the buffer to the provided byte channel.private boolean
merge
(ByteBuffer duplicate) This method is used to perform a merge of the buffer to be sent with the current buffer.boolean
ready()
This is used to determine if the buffer is ready to be written to.boolean
write
(ByteBuffer duplicate) This will write the bytes to underlying channel if the data is greater than the minimum buffer size.
-
Field Details
-
appender
This is a small internal buffer to collect fragments. -
channel
This is the underlying socket to sent to the data over. -
reference
This is a reference to the last buffer to be sent. -
trace
This is used to trace various events that occur. -
chunk
private int chunkThis is the recommended minimum packet size to send. -
closed
private boolean closedThis is used to determine if the buffer was closed.
-
-
Constructor Details
-
SocketBuffer
Constructor for theSocketBuffer
object. This is used to create a buffer that will collect small fragments sent in to a more reasonably sized packet.- Parameters:
socket
- this is the socket to write the data tochunk
- this is the minimum packet size to usedlimit
- this is the maximum size of the output buffer
-
-
Method Details
-
ready
This is used to determine if the buffer is ready to be written to. A buffer is ready when it does not hold a reference to any other buffer internally. The theflush
method must return true for a buffer to be considered ready.- Returns:
- returns true if the buffer is ready to write to
- Throws:
IOException
-
write
This will write the bytes to underlying channel if the data is greater than the minimum buffer size. If it is less than the minimum size then it will be appended to the internal buffer. If it is larger than the maximum size of the internal buffer a reference is kept to it. This reference can only be cleared with theflush
method, which will attempt to write the data to the channel, and buffer any remaining data if the underly connection is busy.- Parameters:
data
- this is the data to write the the channel.- Returns:
- this returns true if no reference was held
- Throws:
IOException
-
merge
This method is used to perform a merge of the buffer to be sent with the current buffer. If the internal buffer is large enough to send after the merge then it will be sent. Also, if the remaining bytes in the buffer are large enough for a packet then that too will be sent over the socket.- Parameters:
duplicate
- this is the buffer to be merged- Returns:
- this returns true if no reference was held
- Throws:
IOException
-
flush
This method is used to fully flush the contents of the buffer to the underlying output stream. This will only ever return true if there are no references held and no data internally buffered. If before this method is invoked a reference to a byte buffer is held then this will attempt to merge it with the internal buffer so that theready
method can return true. This ensures that the writing thread does not need to block.- Returns:
- this returns true if all of the bytes are sent
- Throws:
IOException
-
flush
This write method will write the contents of the buffer to the provided byte channel. If the whole buffer can be be written then this will simply return the number of bytes that have. The number of bytes remaining within the packet after a write can be acquired from thelength
method. Once all of the bytes are written the packet must be closed.- Parameters:
segment
- this is the segment that is to be writtenchannel
- this is the channel to write the packet to- Returns:
- this returns the number of bytes that were written
- Throws:
IOException
-
compact
To ensure that we can release any references and thus avoid a blocking thread this method will attempt to merge references in to the internal buffer. Compacting in this manner is done only if the full reference can fit in to the available space.- Throws:
IOException
-
close
This is used to close the writer and the underlying socket. If a close is performed on the writer then no more bytes can be read from or written to the writer and the client will receive a connection close on their side. This also ensures that the TCP FIN ACK is sent before the actual channel is closed. This is required for a clean shutdown.- Throws:
IOException
-