Package org.jgroups.nio
Class Buffers
java.lang.Object
org.jgroups.nio.Buffers
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classIterates over the non-null buffers -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ByteBuffer[]protected shortprotected intprotected shortprotected short -
Constructor Summary
ConstructorsConstructorDescriptionBuffers(int capacity) Creates a new instance with an array of capacity buffersBuffers(ByteBuffer... data) -
Method Summary
Modifier and TypeMethodDescriptionadd(ByteBuffer buf) Adds a buffer.add(ByteBuffer... buffers) Adds a number of buffers.protected booleanadjustPosition(boolean null_complete_data) protected static intassertPositiveUnsignedShort(int num) copy()Copies the data that has not yet been written and moves last_copied.static ByteBuffercopyBuffer(ByteBuffer buf) Copies a ByteBuffer by copying and wrapping the underlying array of a heap-based buffer.get(int index) booleaniterator()intlimit()limit(int new_limit) protected booleanintmaxLength(int len) intnextToCopy(int next) protected booleannullData()Looks at all buffers in range [position ..intposition()position(int new_pos) booleanread(SocketChannel ch) Performs a scattering read into all (contiguous) non-null buffers in range [position ..Reads length and then length bytes into the data buffer, which is grown if needed.intremove(int index) Nulls the buffer at indexset(int index, ByteBuffer buf) intsize()Returns the number of elements that have not yet been read or writtenprotected booleanspaceAvailable(int num_buffers) protected static shorttoPositiveUnsignedShort(int num) toString()booleanWrites the buffers from position to limit to the given channel.booleanwrite(GatheringByteChannel ch, 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, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
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
-
-
Method Details
-
position
public int position() -
position
-
limit
public int limit() -
limit
-
nextToCopy
public int nextToCopy() -
nextToCopy
-
maxLength
public int maxLength() -
maxLength
-
remaining
public int remaining() -
hasRemaining
public boolean hasRemaining() -
add
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
Adds a buffer. If there's no capacity, the buffer will not be added and will be silently dropped -
get
-
set
-
remove
Nulls the buffer at index -
readLengthAndData
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
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
Helper method which adds the buffers passed as arguments and then calls write()- Throws:
Exception
-
write
Writes the buffers from position to limit to the given channel. Note that all buffers need to have theirBuffer.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
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
-
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
Copies a ByteBuffer by copying and wrapping the underlying array of a heap-based buffer. Direct buffers are converted to heap-based buffers -
iterator
- Specified by:
iteratorin interfaceIterable<ByteBuffer>
-