Class Buffers

java.lang.Object
org.jgroups.nio.Buffers
All Implemented Interfaces:
Iterable<ByteBuffer>

public class Buffers extends Object implements Iterable<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
  • Field Details

    • bufs

      protected final 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 Details

    • 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(ByteBuffer... data)
  • Method Details

    • 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(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(ByteBuffer buf)
      Adds a buffer. If there's no capacity, the buffer will not be added and will be silently dropped
    • get

      public ByteBuffer get(int index)
    • set

      public Buffers set(int index, ByteBuffer buf)
    • remove

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

      public ByteBuffer readLengthAndData(SocketChannel ch) throws 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:
      Exception
    • read

      public boolean read(SocketChannel ch) throws 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:
      Exception
    • write

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

      public boolean write(GatheringByteChannel ch) throws 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:
      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 String toString()
      Overrides:
      toString in class 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 ByteBuffer copyBuffer(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 Iterator<ByteBuffer> iterator()
      Specified by:
      iterator in interface Iterable<ByteBuffer>