Class SocketBufferAppender


  • class SocketBufferAppender
    extends java.lang.Object
    The SocketBufferAppender represents a buffer fragment collector. This provides write access to a direct byte buffer which is used to collect fragments. Once a sufficient amount of data has been collected by this then can be written out to a channel.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.ByteBuffer buffer
      This is the buffer used to store the contents of the buffer.
      private int chunk
      This represents the the initial size of the buffer to use.
      private int limit
      This represents the largest this appender can grow to.
      private Trace trace
      This is the trace used to watch the buffering events.
    • Constructor Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int append​(java.nio.ByteBuffer data)
      This will append bytes within the given buffer to the buffer.
      int append​(java.nio.ByteBuffer data, int count)
      This will append bytes within the given buffer to the buffer.
      int capacity()
      This represents the capacity of the backing store.
      java.lang.String encode()
      This is used to encode the underlying byte sequence to text.
      java.lang.String encode​(java.lang.String encoding)
      This is used to encode the underlying byte sequence to text.
      private java.lang.String encode​(java.lang.String encoding, java.nio.ByteBuffer segment)
      This is used to encode the underlying byte sequence to text.
      int length()
      This is used to determine how mnay bytes remain within this buffer.
      int space()
      This is used to determine how much space is left to append data to this buffer.
      int write​(java.nio.channels.ByteChannel channel)
      This write method will write the contents of the buffer to the provided byte channel.
      int write​(java.nio.channels.ByteChannel channel, int count)
      This write method will write the contents of the buffer to the provided byte channel.
      private int write​(java.nio.channels.ByteChannel channel, java.nio.ByteBuffer segment)
      This write method will write the contents of the buffer to the provided byte channel.
      • Methods inherited from class java.lang.Object

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

      • buffer

        private java.nio.ByteBuffer buffer
        This is the buffer used to store the contents of the buffer.
      • trace

        private Trace trace
        This is the trace used to watch the buffering events.
      • chunk

        private int chunk
        This represents the the initial size of the buffer to use.
      • limit

        private int limit
        This represents the largest this appender can grow to.
    • Constructor Detail

      • SocketBufferAppender

        public SocketBufferAppender​(Socket socket,
                                    int chunk,
                                    int limit)
        Constructor for the SocketBufferAppender object. This is used to create an appender that can collect smaller fragments in to a larger buffer so that it can be delivered more efficiently.
        Parameters:
        socket - this is the socket to append data for
        chunk - this is the initial size of the buffer
        limit - this is the maximum size of the buffer
    • Method Detail

      • space

        public int space()
        This is used to determine how much space is left to append data to this buffer. This is typically equivalent to capacity minus the length. However in the event that the buffer uses a private memory store that can not be written to then this can return zero regardless of the capacity and length.
        Returns:
        the space left within the buffer to append data to
      • capacity

        public int capacity()
        This represents the capacity of the backing store. The buffer is full when length is equal to capacity and it can typically be appended to when the length is less than the capacity. The only exception is when space returns zero, which means that the buffer can not have bytes appended to it.
        Returns:
        this is the capacity of other backing byte storage
      • length

        public int length()
        This is used to determine how mnay bytes remain within this buffer. It represents the number of write ready bytes, so if the length is greater than zero the buffer can be written to a byte channel. When length is zero the buffer can be closed.
        Returns:
        this is the number of bytes remaining in this buffer
      • encode

        public java.lang.String encode()
                                throws java.io.IOException
        This is used to encode the underlying byte sequence to text. Converting the byte sequence to text can be useful when either debugging what exactly is being sent. Also, for transports that require string delivery of buffers this can be used.
        Returns:
        this returns the bytes sequence as a string object
        Throws:
        java.io.IOException
      • encode

        public java.lang.String encode​(java.lang.String encoding)
                                throws java.io.IOException
        This is used to encode the underlying byte sequence to text. Converting the byte sequence to text can be useful when either debugging what exactly is being sent. Also, for transports that require string delivery of buffers this can be used.
        Parameters:
        encoding - this is the character set to use for encoding
        Returns:
        this returns the bytes sequence as a string object
        Throws:
        java.io.IOException
      • encode

        private java.lang.String encode​(java.lang.String encoding,
                                        java.nio.ByteBuffer segment)
                                 throws java.io.IOException
        This is used to encode the underlying byte sequence to text. Converting the byte sequence to text can be useful when either debugging what exactly is being sent. Also, for transports that require string delivery of buffers this can be used.
        Parameters:
        encoding - this is the character set to use for encoding
        segment - this is the buffer that is to be encoded
        Returns:
        this returns the bytes sequence as a string object
        Throws:
        java.io.IOException
      • append

        public int append​(java.nio.ByteBuffer data)
                   throws java.io.IOException
        This will append bytes within the given buffer to the buffer. Once invoked the buffer will contain the buffer bytes, which will have been drained from the buffer. This effectively moves the bytes in the buffer to the end of the buffer instance.
        Parameters:
        data - this is the buffer containing the bytes
        Returns:
        returns the number of bytes that have been moved
        Throws:
        java.io.IOException
      • append

        public int append​(java.nio.ByteBuffer data,
                          int count)
                   throws java.io.IOException
        This will append bytes within the given buffer to the buffer. Once invoked the buffer will contain the buffer bytes, which will have been drained from the buffer. This effectively moves the bytes in the buffer to the end of the buffer instance.
        Parameters:
        data - this is the buffer containing the bytes
        count - this is the number of bytes that should be used
        Returns:
        returns the number of bytes that have been moved
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.channels.ByteChannel channel)
                  throws java.io.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 buffer after a write can be acquired from the length method. Once all of the bytes are written the buffer must be closed.
        Parameters:
        channel - this is the channel to write the buffer to
        Returns:
        this returns the number of bytes that were written
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.channels.ByteChannel channel,
                         int count)
                  throws java.io.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 buffer after a write can be acquired from the length method. Once all of the bytes are written the buffer must be closed.
        Parameters:
        channel - this is the channel to write the buffer to
        count - the number of bytes to write to the channel
        Returns:
        this returns the number of bytes that were written
        Throws:
        java.io.IOException
      • write

        private int write​(java.nio.channels.ByteChannel channel,
                          java.nio.ByteBuffer segment)
                   throws java.io.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 buffer after a write can be acquired from the length method. Once all of the bytes are written the buffer must be closed.
        Parameters:
        channel - this is the channel to write the buffer to
        segment - this is the buffer that is to be written
        Returns:
        this returns the number of bytes that were written
        Throws:
        java.io.IOException