Interface IoBufferHeader

  • All Known Subinterfaces:
    IoBuffer
    All Known Implementing Classes:
    ByteBuffer, FastByteBuffer

    public interface IoBufferHeader
    Interface definition in line with the jdk Buffer abstract class. This definition is needed to allow to redirect and allow for different buffer implementations.

    A buffer is a linear, finite sequence of elements of a specific primitive type. Aside from its content, the essential properties of a buffer are its capacity, limit, and position:

    A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.

    A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.

    A buffer's position is the index of the next element to be read or written. A buffer's position is never negative and is never greater than its limit.

    The following invariant holds for the mark, position, limit, and capacity values:

    0 <= position <= limit <= capacity
    Author:
    rstein
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      int capacity()  
      void clear()
      Clears this buffer.
      void ensureAdditionalCapacity​(int capacity)  
      void ensureCapacity​(int capacity)  
      void flip()
      Flips this buffer.
      void forceCapacity​(int length, int preserve)
      Forces buffer to contain the given number of entries, preserving just a part of the array.
      boolean hasRemaining()  
      boolean isReadOnly()  
      int limit()  
      void limit​(int newLimit)
      Sets this buffer's limit.
      java.util.concurrent.locks.ReadWriteLock lock()
      For efficiency/performance reasons the buffer implementation is not required to safe-guard each put/get method independently.
      int position()  
      void position​(int newPosition)
      Sets this buffer's position.
      int remaining()  
      void reset()
      resets the buffer read/write position to zero
      void trim()
      Trims the internal buffer array so that the capacity is equal to the size.
      void trim​(int requestedCapacity)
      Trims the internal buffer array if it is too large.
    • Method Detail

      • capacity

        int capacity()
        Returns:
        the capacity of this buffer
      • clear

        void clear()
        Clears this buffer. The position is set to zero amd the limit is set to the capacity.

        Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:

         buf.clear(); // Prepare buffer for reading
         in.read(buf); // Read data
         

        This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.

      • ensureAdditionalCapacity

        void ensureAdditionalCapacity​(int capacity)
      • ensureCapacity

        void ensureCapacity​(int capacity)
      • flip

        void flip()
        Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.

        After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:

         buf.put(magic);    // Prepend header
         in.read(buf);      // Read data into rest of buffer
         buf.flip();        // Flip buffer
         out.write(buf);    // Write header + data to channel
      • forceCapacity

        void forceCapacity​(int length,
                           int preserve)
        Forces buffer to contain the given number of entries, preserving just a part of the array.
        Parameters:
        length - the new minimum length for this array.
        preserve - the number of elements of the old buffer that shall be preserved in case a new allocation is necessary.
      • hasRemaining

        boolean hasRemaining()
        Returns:
        true if, and only if, there is at least one element remaining in this buffer
      • isReadOnly

        boolean isReadOnly()
        Returns:
        true if, and only if, this buffer is read-only
      • limit

        int limit()
        Returns:
        the limit of this buffer
      • limit

        void limit​(int newLimit)
        Sets this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.
        Parameters:
        newLimit - the new limit value; must be non-negative and no larger than this buffer's capacity
      • lock

        java.util.concurrent.locks.ReadWriteLock lock()
        For efficiency/performance reasons the buffer implementation is not required to safe-guard each put/get method independently. Thus the user-code should acquire the given lock around a set of put/get appropriately.
        Returns:
        the read-write lock
      • position

        int position()
        Returns:
        the position of this buffer
      • position

        void position​(int newPosition)
        Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded.
        Parameters:
        newPosition - the new position value; must be non-negative and no larger than the current limit
      • remaining

        int remaining()
        Returns:
        the number of elements remaining in this buffer
      • reset

        void reset()
        resets the buffer read/write position to zero
      • trim

        void trim()
        Trims the internal buffer array so that the capacity is equal to the size.
        See Also:
        ArrayList.trimToSize()
      • trim

        void trim​(int requestedCapacity)
        Trims the internal buffer array if it is too large. If the current array length is smaller than or equal to n, this method does nothing. Otherwise, it trims the array length to the maximum between requestedCapacity and capacity().

        This method is useful when reusing FastBuffers. Clearing a list leaves the array length untouched. If you are reusing a list many times, you can call this method with a typical size to avoid keeping around a very large array just because of a few large transient lists.

        Parameters:
        requestedCapacity - the threshold for the trimming.