Class Buffers

  • All Implemented Interfaces:
    java.lang.Iterable<java.nio.ByteBuffer>

    public class Buffers
    extends java.lang.Object
    implements java.lang.Iterable<java.nio.ByteBuffer>
    Class to do scattering reads or gathering writes on a sequence of ByteBuffer instances. The buffers are kept in an array with fixed capacity (max Short.MAX_VALUE). Buffers can be added and removed dynamically, but they're dropped when the capacity is exceeded. This means that, if a we have configured a capacity of 5 buffers, and want to add 6, none of the 6 buffers will be added!
    A read is successful when all non-null buffers from left to right are filled, i.e. all Buffer.remaining() methods return 0.
    Same for writes: when all non-null buffers (from left to right) have been written (Buffer.remaining() == 0), a write is considered successful; otherwise it is partial.
    Individual buffers can be accessed; e.g. for reading its value after a read. It is also possible to add buffers dynamically, e.g. after reading a 'length' buffer, a user may want to add a new buffer allocated for reading 'length' bytes.
    This class is not synchronized.
    Since:
    3.6.5
    Author:
    Bela Ban
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  Buffers.BuffersIterator
      Iterates over the non-null buffers
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.nio.ByteBuffer[] bufs  
      protected short limit  
      protected int max_length  
      protected short next_to_copy  
      protected short position  
    • Constructor Summary

      Constructors 
      Constructor Description
      Buffers​(int capacity)
      Creates a new instance with an array of capacity buffers
      Buffers​(java.nio.ByteBuffer... data)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Buffers add​(java.nio.ByteBuffer buf)
      Adds a buffer.
      Buffers add​(java.nio.ByteBuffer... buffers)
      Adds a number of buffers.
      protected boolean adjustPosition​(boolean null_complete_data)  
      protected static int assertPositiveUnsignedShort​(int num)  
      Buffers copy()
      Copies the data that has not yet been written and moves last_copied.
      static java.nio.ByteBuffer copyBuffer​(java.nio.ByteBuffer buf)
      Copies a ByteBuffer by copying and wrapping the underlying array of a heap-based buffer.
      java.nio.ByteBuffer get​(int index)  
      boolean hasRemaining()  
      java.util.Iterator<java.nio.ByteBuffer> iterator()  
      int limit()  
      Buffers limit​(int new_limit)  
      protected boolean makeSpace()  
      int maxLength()  
      Buffers maxLength​(int len)  
      int nextToCopy()  
      Buffers nextToCopy​(int next)  
      protected boolean nullData()
      Looks at all buffers in range [position ..
      int position()  
      Buffers position​(int new_pos)  
      boolean read​(java.nio.channels.SocketChannel ch)
      Performs a scattering read into all (contiguous) non-null buffers in range [position ..
      java.nio.ByteBuffer readLengthAndData​(java.nio.channels.SocketChannel ch)
      Reads length and then length bytes into the data buffer, which is grown if needed.
      int remaining()  
      Buffers remove​(int index)
      Nulls the buffer at index
      Buffers set​(int index, java.nio.ByteBuffer buf)  
      int size()
      Returns the number of elements that have not yet been read or written
      protected boolean spaceAvailable​(int num_buffers)  
      protected static short toPositiveUnsignedShort​(int num)  
      java.lang.String toString()  
      boolean write​(java.nio.channels.GatheringByteChannel ch)
      Writes the buffers from position to limit to the given channel.
      boolean write​(java.nio.channels.GatheringByteChannel ch, java.nio.ByteBuffer... buffers)
      Helper method which adds the buffers passed as arguments and then calls write()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • bufs

        protected final java.nio.ByteBuffer[] bufs
      • position

        protected short position
      • limit

        protected short limit
      • next_to_copy

        protected short next_to_copy
      • max_length

        protected int max_length
    • Constructor Detail

      • Buffers

        public Buffers​(int capacity)
        Creates a new instance with an array of capacity buffers
        Parameters:
        capacity - Must be an unsigned positive short [1 .. Short.MAX_VALUE]
      • Buffers

        public Buffers​(java.nio.ByteBuffer... data)
    • Method Detail

      • position

        public int position()
      • position

        public Buffers position​(int new_pos)
      • limit

        public int limit()
      • limit

        public Buffers limit​(int new_limit)
      • nextToCopy

        public int nextToCopy()
      • nextToCopy

        public Buffers nextToCopy​(int next)
      • maxLength

        public int maxLength()
      • maxLength

        public Buffers maxLength​(int len)
      • remaining

        public int remaining()
      • hasRemaining

        public boolean hasRemaining()
      • add

        public Buffers add​(java.nio.ByteBuffer... buffers)
        Adds a number of buffers. Note that if the buffers cannot be added as a whole, e.g. because of exceeding the capacity, none of them will be added!
      • add

        public Buffers add​(java.nio.ByteBuffer buf)
        Adds a buffer. If there's no capacity, the buffer will not be added and will be silently dropped
      • get

        public java.nio.ByteBuffer get​(int index)
      • set

        public Buffers set​(int index,
                           java.nio.ByteBuffer buf)
      • remove

        public Buffers remove​(int index)
        Nulls the buffer at index
      • readLengthAndData

        public java.nio.ByteBuffer readLengthAndData​(java.nio.channels.SocketChannel ch)
                                              throws java.lang.Exception
        Reads length and then length bytes into the data buffer, which is grown if needed.
        Parameters:
        ch - The channel to read data from
        Returns:
        The data buffer (position is 0 and limit is length), or null if not all data could be read.
        Throws:
        java.lang.Exception
      • read

        public boolean read​(java.nio.channels.SocketChannel ch)
                     throws java.lang.Exception
        Performs a scattering read into all (contiguous) non-null buffers in range [position .. limit]. Returns true if the scattering read was successful, else false. Note that to read the contents of the individual buffers, ByteBuffer.clear() has to be called (all buffers have their position == limit on a successful read).
        Throws:
        java.lang.Exception
      • write

        public boolean write​(java.nio.channels.GatheringByteChannel ch,
                             java.nio.ByteBuffer... buffers)
                      throws java.lang.Exception
        Helper method which adds the buffers passed as arguments and then calls write()
        Throws:
        java.lang.Exception
      • write

        public boolean write​(java.nio.channels.GatheringByteChannel ch)
                      throws java.lang.Exception
        Writes the buffers from position to limit to the given channel. Note that all buffers need to have their Buffer.position() at the start of the data to be written
        Parameters:
        ch - The channel to write to
        Returns:
        True if all the bytes of the buffer were written successfully, false otherwise (partial write).
        Throws:
        java.lang.Exception - Thrown if the write failed
      • copy

        public Buffers copy()
        Copies the data that has not yet been written and moves last_copied. Typically done after an unsuccessful write, if copying is required. This is typically needed if the output buffer is reused. Note that direct buffers will be converted to heap-based buffers
      • size

        public int size()
        Returns the number of elements that have not yet been read or written
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • spaceAvailable

        protected boolean spaceAvailable​(int num_buffers)
      • makeSpace

        protected boolean makeSpace()
      • nullData

        protected boolean nullData()
        Looks at all buffers in range [position .. limit-1] and nulls buffers that have no remaining data. Returns true if all buffers could be nulled, and false otherwise
      • adjustPosition

        protected boolean adjustPosition​(boolean null_complete_data)
      • toPositiveUnsignedShort

        protected static short toPositiveUnsignedShort​(int num)
      • assertPositiveUnsignedShort

        protected static int assertPositiveUnsignedShort​(int num)
      • copyBuffer

        public static java.nio.ByteBuffer copyBuffer​(java.nio.ByteBuffer buf)
        Copies a ByteBuffer by copying and wrapping the underlying array of a heap-based buffer. Direct buffers are converted to heap-based buffers
      • iterator

        public java.util.Iterator<java.nio.ByteBuffer> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.nio.ByteBuffer>