Class ArrayBuffer.Segment

  • All Implemented Interfaces:
    Buffer
    Enclosing class:
    ArrayBuffer

    private class ArrayBuffer.Segment
    extends java.lang.Object
    implements Buffer
    A Segment represents a segment within a buffer. It is used to allow a buffer to be split in to several logical parts without the need to create several separate buffers. This means that the buffer can be represented in a single memory space, as both a single large buffer and as several individual buffers.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean closed
      This is used to determine if the buffer has closed or not.
      private int length
      This represents the number of bytes this segment contains.
      private Buffer parent
      This is the parent buffer which is used for collecting data.
      private int start
      This represents the start of the segment within the buffer.
    • Constructor Summary

      Constructors 
      Constructor Description
      Segment​(Buffer parent, int start)
      Constructor for the Segment object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Buffer allocate()
      This method is used to allocate a segment of this buffer as a separate buffer object.
      Buffer append​(byte[] array)
      This method is used to append bytes to the end of the buffer.
      Buffer append​(byte[] array, int off, int size)
      This method is used to append bytes to the end of the buffer.
      void clear()
      This will clear all data from the buffer.
      void close()
      This method is used to ensure the buffer can be closed.
      java.lang.String encode()
      This method is used to acquire the buffered bytes as a string.
      java.lang.String encode​(java.lang.String charset)
      This method is used to acquire the buffered bytes as a string.
      long length()
      This is used to provide the number of bytes that have been written to the buffer.
      java.io.InputStream open()
      This method is used so that the buffer can be represented as a stream of bytes.
      • Methods inherited from class java.lang.Object

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

      • parent

        private Buffer parent
        This is the parent buffer which is used for collecting data.
      • closed

        private boolean closed
        This is used to determine if the buffer has closed or not.
      • start

        private int start
        This represents the start of the segment within the buffer.
      • length

        private int length
        This represents the number of bytes this segment contains.
    • Constructor Detail

      • Segment

        public Segment​(Buffer parent,
                       int start)
        Constructor for the Segment object. This is used to create a buffer within a buffer. A segment is a region of bytes within the original buffer. It allows the buffer to be split in to several logical parts of a single buffer.
        Parameters:
        parent - this is the parent buffer used to append to
        start - this is the start within the buffer to read
    • Method Detail

      • open

        public java.io.InputStream open()
                                 throws java.io.IOException
        This method is used so that the buffer can be represented as a stream of bytes. This provides a quick means to access the data that has been written to the buffer. It wraps the buffer within an input stream so that it can be read directly.
        Specified by:
        open in interface Buffer
        Returns:
        a stream that can be used to read the buffered bytes
        Throws:
        java.io.IOException
      • allocate

        public Buffer allocate()
                        throws java.io.IOException
        This method is used to allocate a segment of this buffer as a separate buffer object. This allows the buffer to be sliced in to several smaller independent buffers, while still allowing the parent buffer to manage a single buffer. This is useful if the parent is split in to logically smaller segments.
        Specified by:
        allocate in interface Buffer
        Returns:
        this returns a buffer which is a segment of this buffer
        Throws:
        java.io.IOException
      • encode

        public java.lang.String encode()
                                throws java.io.IOException
        This method is used to acquire the buffered bytes as a string. This is useful if the contents need to be manipulated as a string or transferred into another encoding. If the UTF-8 content encoding is not supported the platform default is used, however this is unlikely as UTF-8 should be supported.
        Specified by:
        encode in interface Buffer
        Returns:
        this returns a UTF-8 encoding of the buffer contents
        Throws:
        java.io.IOException
      • encode

        public java.lang.String encode​(java.lang.String charset)
                                throws java.io.IOException
        This method is used to acquire the buffered bytes as a string. This is useful if the contents need to be manipulated as a string or transferred into another encoding. This will convert the bytes using the specified character encoding format.
        Specified by:
        encode in interface Buffer
        Parameters:
        charset - this is the charset to encode the data with
        Returns:
        this returns the encoding of the buffer contents
        Throws:
        java.io.IOException
      • append

        public Buffer append​(byte[] array)
                      throws java.io.IOException
        This method is used to append bytes to the end of the buffer. This will expand the capacity of the buffer if there is not enough space to accommodate the extra bytes.
        Specified by:
        append in interface Buffer
        Parameters:
        array - this is the byte array to append to this buffer
        Returns:
        this returns this buffer for another operation
        Throws:
        java.io.IOException
      • append

        public Buffer append​(byte[] array,
                             int off,
                             int size)
                      throws java.io.IOException
        This method is used to append bytes to the end of the buffer. This will expand the capacity of the buffer if there is not enough space to accommodate the extra bytes.
        Specified by:
        append in interface Buffer
        Parameters:
        array - this is the byte array to append to this buffer
        off - this is the offset to begin reading the bytes from
        size - the number of bytes to be read from the array
        Returns:
        this returns this buffer for another operation
        Throws:
        java.io.IOException
      • clear

        public void clear()
                   throws java.io.IOException
        This will clear all data from the buffer. This simply sets the count to be zero, it will not clear the memory occupied by the instance as the internal buffer will remain. This allows the memory occupied to be reused as many times as is required.
        Specified by:
        clear in interface Buffer
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        This method is used to ensure the buffer can be closed. Once the buffer is closed it is an immutable collection of bytes and can not longer be modified. This ensures that it can be passed by value without the risk of modification of the bytes.
        Specified by:
        close in interface Buffer
        Throws:
        java.io.IOException
      • length

        public long length()
        This is used to provide the number of bytes that have been written to the buffer. This increases as bytes are appended to the buffer. if the buffer is cleared this resets to zero.
        Specified by:
        length in interface Buffer
        Returns:
        this returns the number of bytes within the buffer