Class AbstractIoBuffer

  • All Implemented Interfaces:
    Comparable<IoBuffer>

    public abstract class AbstractIoBuffer
    extends IoBuffer
    A base implementation of IoBuffer. This implementation assumes that IoBuffer.buf() always returns a correct NIO ByteBuffer instance. Most implementations could extend this class and implement their own buffer management mechanism.
    Version:
    $Rev: 748210 $, $Date: 2009-02-26 18:05:40 +0100 (Thu, 26 Feb 2009) $
    Author:
    The Apache MINA Project (dev@mina.apache.org)
    See Also:
    IoBufferAllocator
    • Constructor Detail

      • AbstractIoBuffer

        protected AbstractIoBuffer​(IoBufferAllocator allocator,
                                   int initialCapacity)
        Creates a new parent buffer.
        Parameters:
        allocator - The allocator to use to create new buffers
        initialCapacity - The initial buffer capacity when created
      • AbstractIoBuffer

        protected AbstractIoBuffer​(AbstractIoBuffer parent)
        Creates a new derived buffer. A derived buffer uses an existing buffer properties - the allocator and capacity -.
        Parameters:
        parent - The buffer we get the properties from
    • Method Detail

      • buf

        protected abstract void buf​(ByteBuffer newBuf)
        Sets the underlying NIO buffer instance.
        Parameters:
        newBuf - The buffer to store within this IoBuffer
      • minimumCapacity

        public final int minimumCapacity()
        Returns the minimum capacity of this buffer which is used to determine the new capacity of the buffer shrunk by IoBuffer.compact() and IoBuffer.shrink() operation. The default value is the initial capacity of the buffer.
        Specified by:
        minimumCapacity in class IoBuffer
      • minimumCapacity

        public final IoBuffer minimumCapacity​(int minimumCapacity)
        Sets the minimum capacity of this buffer which is used to determine the new capacity of the buffer shrunk by IoBuffer.compact() and IoBuffer.shrink() operation. The default value is the initial capacity of the buffer.
        Specified by:
        minimumCapacity in class IoBuffer
      • capacity

        public final IoBuffer capacity​(int newCapacity)
        Increases the capacity of this buffer. If the new capacity is less than or equal to the current capacity, this method returns silently. If the new capacity is greater than the current capacity, the buffer is reallocated while retaining the position, limit, mark and the content of the buffer.
        Specified by:
        capacity in class IoBuffer
      • isAutoExpand

        public final boolean isAutoExpand()
        Returns true if and only if autoExpand is turned on.
        Specified by:
        isAutoExpand in class IoBuffer
      • isAutoShrink

        public final boolean isAutoShrink()
        Returns true if and only if autoShrink is turned on.
        Specified by:
        isAutoShrink in class IoBuffer
      • setAutoExpand

        public final IoBuffer setAutoExpand​(boolean autoExpand)
        Turns on or off autoExpand.
        Specified by:
        setAutoExpand in class IoBuffer
      • setAutoShrink

        public final IoBuffer setAutoShrink​(boolean autoShrink)
        Turns on or off autoShrink.
        Specified by:
        setAutoShrink in class IoBuffer
      • expand

        public final IoBuffer expand​(int expectedRemaining)
        Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the current position. This method works even if you didn't set autoExpand to true.
        Specified by:
        expand in class IoBuffer
      • expand

        public final IoBuffer expand​(int pos,
                                     int expectedRemaining)
        Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the specified position. This method works even if you didn't set autoExpand to true.
        Specified by:
        expand in class IoBuffer
      • shrink

        public final IoBuffer shrink()
        Changes the capacity of this buffer so this buffer occupies as less memory as possible while retaining the position, limit and the buffer content between the position and limit. The capacity of the buffer never becomes less than IoBuffer.minimumCapacity(). The mark is discarded once the capacity changes.
        Specified by:
        shrink in class IoBuffer
      • markValue

        public final int markValue()
        Returns the position of the current mark. This method returns -1 if no mark is set.
        Specified by:
        markValue in class IoBuffer
      • sweep

        public final IoBuffer sweep()
        Clears this buffer and fills its content with NUL. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
        Specified by:
        sweep in class IoBuffer
      • sweep

        public final IoBuffer sweep​(byte value)
        double Clears this buffer and fills its content with value. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
        Specified by:
        sweep in class IoBuffer
      • getUnsigned

        public final short getUnsigned()
        Reads one unsigned byte as a short integer.
        Specified by:
        getUnsigned in class IoBuffer
      • getUnsigned

        public final short getUnsigned​(int index)
        Reads one byte as an unsigned short integer.
        Specified by:
        getUnsigned in class IoBuffer
      • put

        public final IoBuffer put​(ByteBuffer src)
        Writes the content of the specified src into this buffer.
        Specified by:
        put in class IoBuffer
      • asReadOnlyBuffer0

        protected abstract IoBuffer asReadOnlyBuffer0()
        Implement this method to return the unexpandable read only version of this buffer.
      • duplicate0

        protected abstract IoBuffer duplicate0()
        Implement this method to return the unexpandable duplicate of this buffer.
      • getSlice

        public final IoBuffer getSlice​(int index,
                                       int length)
        TODO document me.
        Specified by:
        getSlice in class IoBuffer
      • getSlice

        public final IoBuffer getSlice​(int length)
        TODO document me.
        Specified by:
        getSlice in class IoBuffer
      • slice0

        protected abstract IoBuffer slice0()
        Implement this method to return the unexpandable slice of this buffer.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • compareTo

        public int compareTo​(IoBuffer that)
      • put

        public IoBuffer put​(IoBuffer src)
        Writes the content of the specified src into this buffer.
        Specified by:
        put in class IoBuffer
      • getUnsignedShort

        public int getUnsignedShort()
        Reads two bytes unsigned integer.
        Specified by:
        getUnsignedShort in class IoBuffer
      • getUnsignedShort

        public int getUnsignedShort​(int index)
        Reads two bytes unsigned integer.
        Specified by:
        getUnsignedShort in class IoBuffer
      • getUnsignedInt

        public long getUnsignedInt()
        Reads four bytes unsigned integer.
        Specified by:
        getUnsignedInt in class IoBuffer
      • getMediumInt

        public int getMediumInt()
        Relative get method for reading a medium int value.

        Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by three.

        Specified by:
        getMediumInt in class IoBuffer
        Returns:
        The medium int value at the buffer's current position
      • getUnsignedMediumInt

        public int getUnsignedMediumInt()
        Relative get method for reading an unsigned medium int value.

        Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by three.

        Specified by:
        getUnsignedMediumInt in class IoBuffer
        Returns:
        The unsigned medium int value at the buffer's current position
      • getMediumInt

        public int getMediumInt​(int index)
        Absolute get method for reading a medium int value.

        Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order.

        Specified by:
        getMediumInt in class IoBuffer
        Parameters:
        index - The index from which the medium int will be read
        Returns:
        The medium int value at the given index
      • getUnsignedMediumInt

        public int getUnsignedMediumInt​(int index)
        Absolute get method for reading an unsigned medium int value.

        Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order.

        Specified by:
        getUnsignedMediumInt in class IoBuffer
        Parameters:
        index - The index from which the unsigned medium int will be read
        Returns:
        The unsigned medium int value at the given index
      • putMediumInt

        public IoBuffer putMediumInt​(int value)
        Relative put method for writing a medium int value.

        Writes three bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by three.

        Specified by:
        putMediumInt in class IoBuffer
        Parameters:
        value - The medium int value to be written
        Returns:
        This buffer
      • putMediumInt

        public IoBuffer putMediumInt​(int index,
                                     int value)
        Absolute put method for writing a medium int value.

        Writes three bytes containing the given int value, in the current byte order, into this buffer at the given index.

        Specified by:
        putMediumInt in class IoBuffer
        Parameters:
        index - The index at which the bytes will be written
        value - The medium int value to be written
        Returns:
        This buffer
      • getUnsignedInt

        public long getUnsignedInt​(int index)
        Reads four bytes unsigned integer.
        Specified by:
        getUnsignedInt in class IoBuffer
      • getHexDump

        public String getHexDump()
        Returns hexdump of this buffer. The data and pointer are not changed as a result of this method call.
        Specified by:
        getHexDump in class IoBuffer
        Returns:
        hexidecimal representation of this buffer
      • getHexDump

        public String getHexDump​(int lengthLimit)
        Return hexdump of this buffer with limited length.
        Specified by:
        getHexDump in class IoBuffer
        Parameters:
        lengthLimit - The maximum number of bytes to dump from the current buffer position.
        Returns:
        hexidecimal representation of this buffer
      • putString

        public IoBuffer putString​(CharSequence val,
                                  int fieldSize,
                                  CharsetEncoder encoder)
                           throws CharacterCodingException
        Writes the content of in into this buffer as a NUL-terminated string using the specified encoder.

        If the charset name of the encoder is UTF-16, you cannot specify odd fieldSize, and this method will append two NULs as a terminator.

        Please note that this method doesn't terminate with NUL if the input string is longer than fieldSize.

        Specified by:
        putString in class IoBuffer
        fieldSize - the maximum number of bytes to write
        Throws:
        CharacterCodingException
      • putPrefixedString

        public IoBuffer putPrefixedString​(CharSequence in,
                                          int prefixLength,
                                          int padding,
                                          CharsetEncoder encoder)
                                   throws CharacterCodingException
        Writes the content of in into this buffer as a string which has a 16-bit length field before the actual encoded string, using the specified encoder. This method is a shortcut for putPrefixedString(in, prefixLength, padding, ( byte ) 0, encoder) .
        Specified by:
        putPrefixedString in class IoBuffer
        prefixLength - the length of the length field (1, 2, or 4)
        padding - the number of padded NULs (1 (or 0), 2, or 4)
        Throws:
        CharacterCodingException
      • putPrefixedString

        public IoBuffer putPrefixedString​(CharSequence val,
                                          int prefixLength,
                                          int padding,
                                          byte padValue,
                                          CharsetEncoder encoder)
                                   throws CharacterCodingException
        Writes the content of in into this buffer as a string which has a 16-bit length field before the actual encoded string, using the specified encoder.
        Specified by:
        putPrefixedString in class IoBuffer
        prefixLength - the length of the length field (1, 2, or 4)
        padding - the number of padded bytes (1 (or 0), 2, or 4)
        padValue - the value of padded bytes
        Throws:
        CharacterCodingException
      • prefixedDataAvailable

        public boolean prefixedDataAvailable​(int prefixLength)
        Returns true if this buffer contains a data which has a data length as a prefix and the buffer has remaining data as enough as specified in the data length field. This method is identical with prefixedDataAvailable( prefixLength, Integer.MAX_VALUE ). Please not that using this method can allow DoS (Denial of Service) attack in case the remote peer sends too big data length value. It is recommended to use IoBuffer.prefixedDataAvailable(int, int) instead.
        Specified by:
        prefixedDataAvailable in class IoBuffer
        Parameters:
        prefixLength - the length of the prefix field (1, 2, or 4)
      • prefixedDataAvailable

        public boolean prefixedDataAvailable​(int prefixLength,
                                             int maxDataLength)
        Returns true if this buffer contains a data which has a data length as a prefix and the buffer has remaining data as enough as specified in the data length field.
        Specified by:
        prefixedDataAvailable in class IoBuffer
        Parameters:
        prefixLength - the length of the prefix field (1, 2, or 4)
        maxDataLength - the allowed maximum of the read data length
      • indexOf

        public int indexOf​(byte b)
        Returns the first occurence position of the specified byte from the current position to the current limit.
        Specified by:
        indexOf in class IoBuffer
        Returns:
        -1 if the specified byte is not found
      • skip

        public IoBuffer skip​(int size)
        Forwards the position of this buffer as the specified size bytes.
        Specified by:
        skip in class IoBuffer
      • fill

        public IoBuffer fill​(byte value,
                             int size)
        Fills this buffer with the specified value. This method moves buffer position forward.
        Specified by:
        fill in class IoBuffer
      • fillAndReset

        public IoBuffer fillAndReset​(byte value,
                                     int size)
        Fills this buffer with the specified value. This method does not change buffer position.
        Specified by:
        fillAndReset in class IoBuffer
      • fill

        public IoBuffer fill​(int size)
        Fills this buffer with NUL (0x00). This method moves buffer position forward.
        Specified by:
        fill in class IoBuffer
      • fillAndReset

        public IoBuffer fillAndReset​(int size)
        Fills this buffer with NUL (0x00). This method does not change buffer position.
        Specified by:
        fillAndReset in class IoBuffer
      • getEnum

        public <E extends Enum<E>> E getEnum​(Class<E> enumClass)
        Reads a byte from the buffer and returns the correlating enum constant defined by the specified enum type.
        Specified by:
        getEnum in class IoBuffer
        Type Parameters:
        E - The enum type to return
        Parameters:
        enumClass - The enum's class object
      • getEnum

        public <E extends Enum<E>> E getEnum​(int index,
                                             Class<E> enumClass)
        Reads a byte from the buffer and returns the correlating enum constant defined by the specified enum type.
        Specified by:
        getEnum in class IoBuffer
        Type Parameters:
        E - The enum type to return
        Parameters:
        index - the index from which the byte will be read
        enumClass - The enum's class object
      • getEnumShort

        public <E extends Enum<E>> E getEnumShort​(Class<E> enumClass)
        Reads a short from the buffer and returns the correlating enum constant defined by the specified enum type.
        Specified by:
        getEnumShort in class IoBuffer
        Type Parameters:
        E - The enum type to return
        Parameters:
        enumClass - The enum's class object
      • getEnumShort

        public <E extends Enum<E>> E getEnumShort​(int index,
                                                  Class<E> enumClass)
        Reads a short from the buffer and returns the correlating enum constant defined by the specified enum type.
        Specified by:
        getEnumShort in class IoBuffer
        Type Parameters:
        E - The enum type to return
        Parameters:
        index - the index from which the bytes will be read
        enumClass - The enum's class object
      • getEnumInt

        public <E extends Enum<E>> E getEnumInt​(Class<E> enumClass)
        Reads an int from the buffer and returns the correlating enum constant defined by the specified enum type.
        Specified by:
        getEnumInt in class IoBuffer
        Type Parameters:
        E - The enum type to return
        Parameters:
        enumClass - The enum's class object
      • getEnumInt

        public <E extends Enum<E>> E getEnumInt​(int index,
                                                Class<E> enumClass)
        Reads an int from the buffer and returns the correlating enum constant defined by the specified enum type.
        Specified by:
        getEnumInt in class IoBuffer
        Type Parameters:
        E - The enum type to return
        Parameters:
        index - the index from which the bytes will be read
        enumClass - The enum's class object
      • putEnum

        public IoBuffer putEnum​(Enum<?> e)
        Writes an enum's ordinal value to the buffer as a byte.
        Specified by:
        putEnum in class IoBuffer
        Parameters:
        e - The enum to write to the buffer
      • putEnum

        public IoBuffer putEnum​(int index,
                                Enum<?> e)
        Writes an enum's ordinal value to the buffer as a byte.
        Specified by:
        putEnum in class IoBuffer
        Parameters:
        index - The index at which the byte will be written
        e - The enum to write to the buffer
      • putEnumShort

        public IoBuffer putEnumShort​(Enum<?> e)
        Writes an enum's ordinal value to the buffer as a short.
        Specified by:
        putEnumShort in class IoBuffer
        Parameters:
        e - The enum to write to the buffer
      • putEnumShort

        public IoBuffer putEnumShort​(int index,
                                     Enum<?> e)
        Writes an enum's ordinal value to the buffer as a short.
        Specified by:
        putEnumShort in class IoBuffer
        Parameters:
        index - The index at which the bytes will be written
        e - The enum to write to the buffer
      • putEnumInt

        public IoBuffer putEnumInt​(Enum<?> e)
        Writes an enum's ordinal value to the buffer as an integer.
        Specified by:
        putEnumInt in class IoBuffer
        Parameters:
        e - The enum to write to the buffer
      • putEnumInt

        public IoBuffer putEnumInt​(int index,
                                   Enum<?> e)
        Writes an enum's ordinal value to the buffer as an integer.
        Specified by:
        putEnumInt in class IoBuffer
        Parameters:
        index - The index at which the bytes will be written
        e - The enum to write to the buffer
      • getEnumSet

        public <E extends Enum<E>> EnumSet<E> getEnumSet​(Class<E> enumClass)
        Reads a byte sized bit vector and converts it to an EnumSet.

        Each bit is mapped to a value in the specified enum. The least significant bit maps to the first entry in the specified enum and each subsequent bit maps to each subsequent bit as mapped to the subsequent enum value.

        Specified by:
        getEnumSet in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
      • getEnumSet

        public <E extends Enum<E>> EnumSet<E> getEnumSet​(int index,
                                                         Class<E> enumClass)
        Reads a byte sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSet in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        index - the index from which the byte will be read
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • getEnumSetShort

        public <E extends Enum<E>> EnumSet<E> getEnumSetShort​(Class<E> enumClass)
        Reads a short sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSetShort in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • getEnumSetShort

        public <E extends Enum<E>> EnumSet<E> getEnumSetShort​(int index,
                                                              Class<E> enumClass)
        Reads a short sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSetShort in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        index - the index from which the bytes will be read
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • getEnumSetInt

        public <E extends Enum<E>> EnumSet<E> getEnumSetInt​(Class<E> enumClass)
        Reads an int sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSetInt in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • getEnumSetInt

        public <E extends Enum<E>> EnumSet<E> getEnumSetInt​(int index,
                                                            Class<E> enumClass)
        Reads an int sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSetInt in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        index - the index from which the bytes will be read
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • getEnumSetLong

        public <E extends Enum<E>> EnumSet<E> getEnumSetLong​(Class<E> enumClass)
        Reads a long sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSetLong in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • getEnumSetLong

        public <E extends Enum<E>> EnumSet<E> getEnumSetLong​(int index,
                                                             Class<E> enumClass)
        Reads a long sized bit vector and converts it to an EnumSet.
        Specified by:
        getEnumSetLong in class IoBuffer
        Type Parameters:
        E - the enum type
        Parameters:
        index - the index from which the bytes will be read
        enumClass - the enum class used to create the EnumSet
        Returns:
        the EnumSet representation of the bit vector
        See Also:
        IoBuffer.getEnumSet(Class)
      • putEnumSet

        public <E extends Enum<E>> IoBuffer putEnumSet​(Set<E> set)
        Writes the specified Set to the buffer as a byte sized bit vector.
        Specified by:
        putEnumSet in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        set - the enum set to write to the buffer
      • putEnumSet

        public <E extends Enum<E>> IoBuffer putEnumSet​(int index,
                                                       Set<E> set)
        Writes the specified Set to the buffer as a byte sized bit vector.
        Specified by:
        putEnumSet in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        index - the index at which the byte will be written
        set - the enum set to write to the buffer
      • putEnumSetShort

        public <E extends Enum<E>> IoBuffer putEnumSetShort​(Set<E> set)
        Writes the specified Set to the buffer as a short sized bit vector.
        Specified by:
        putEnumSetShort in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        set - the enum set to write to the buffer
      • putEnumSetShort

        public <E extends Enum<E>> IoBuffer putEnumSetShort​(int index,
                                                            Set<E> set)
        Writes the specified Set to the buffer as a short sized bit vector.
        Specified by:
        putEnumSetShort in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        index - the index at which the bytes will be written
        set - the enum set to write to the buffer
      • putEnumSetInt

        public <E extends Enum<E>> IoBuffer putEnumSetInt​(Set<E> set)
        Writes the specified Set to the buffer as an int sized bit vector.
        Specified by:
        putEnumSetInt in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        set - the enum set to write to the buffer
      • putEnumSetInt

        public <E extends Enum<E>> IoBuffer putEnumSetInt​(int index,
                                                          Set<E> set)
        Writes the specified Set to the buffer as an int sized bit vector.
        Specified by:
        putEnumSetInt in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        index - the index at which the bytes will be written
        set - the enum set to write to the buffer
      • putEnumSetLong

        public <E extends Enum<E>> IoBuffer putEnumSetLong​(Set<E> set)
        Writes the specified Set to the buffer as a long sized bit vector.
        Specified by:
        putEnumSetLong in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        set - the enum set to write to the buffer
      • putEnumSetLong

        public <E extends Enum<E>> IoBuffer putEnumSetLong​(int index,
                                                           Set<E> set)
        Writes the specified Set to the buffer as a long sized bit vector.
        Specified by:
        putEnumSetLong in class IoBuffer
        Type Parameters:
        E - the enum type of the Set
        Parameters:
        index - the index at which the bytes will be written
        set - the enum set to write to the buffer