Class SocketBuffer

java.lang.Object
org.simpleframework.transport.SocketBuffer

class SocketBuffer extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    This is a small internal buffer to collect fragments.
    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

    Constructors
    Constructor
    Description
    SocketBuffer(Socket socket, int chunk, int limit)
    Constructor for the SocketBuffer object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This is used to close the writer and the underlying socket.
    private void
    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
    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
    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.

    Methods inherited from class java.lang.Object

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

    • appender

      private SocketBufferAppender appender
      This is a small internal buffer to collect fragments.
    • channel

      private SocketChannel channel
      This is the underlying socket to sent to the data over.
    • reference

      private ByteBuffer reference
      This is a reference to the last buffer to be sent.
    • trace

      private Trace trace
      This is used to trace various events that occur.
    • chunk

      private int chunk
      This is the recommended minimum packet size to send.
    • closed

      private boolean closed
      This is used to determine if the buffer was closed.
  • Constructor Details

    • SocketBuffer

      public SocketBuffer(Socket socket, int chunk, int limit)
      Constructor for the SocketBuffer 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 to
      chunk - this is the minimum packet size to used
      limit - this is the maximum size of the output buffer
  • Method Details

    • ready

      public boolean ready() throws IOException
      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 the flush 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

      public boolean write(ByteBuffer duplicate) throws IOException
      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 the flush 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

      private boolean merge(ByteBuffer duplicate) throws IOException
      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

      public boolean flush() throws IOException
      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 the ready 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

      private boolean flush(ByteBuffer segment) throws IOException
      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 the length method. Once all of the bytes are written the packet must be closed.
      Parameters:
      segment - this is the segment that is to be written
      channel - this is the channel to write the packet to
      Returns:
      this returns the number of bytes that were written
      Throws:
      IOException
    • compact

      private void compact() throws IOException
      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

      public void close() throws IOException
      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