public class Buffers
extends java.lang.Object
implements java.lang.Iterable<java.nio.ByteBuffer>
ByteBuffer
instances. The buffers are
kept in an array with fixed capacity (max Short.MAX_VALUE).
Buffers can be added and removed dynamically (they're dropped when the capacity is exceeded).
A read is successful when all non-null buffers from left to right are filled, ie. 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.Modifier and Type | Class and Description |
---|---|
protected class |
Buffers.BuffersIterator
Iterates over the non-null buffers
|
Modifier and Type | Field and Description |
---|---|
protected java.nio.ByteBuffer[] |
bufs |
protected short |
limit |
protected short |
next_to_copy |
protected short |
position |
Constructor and Description |
---|
Buffers(java.nio.ByteBuffer... data) |
Buffers(int capacity)
Creates a new instance with an array of capacity buffers
|
Modifier and Type | Method and Description |
---|---|
Buffers |
add(java.nio.ByteBuffer... buffers) |
Buffers |
add(java.nio.ByteBuffer buf) |
protected boolean |
adjustPosition(boolean null_complete_data) |
protected static void |
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 |
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()
|
protected final java.nio.ByteBuffer[] bufs
protected short position
protected short limit
protected short next_to_copy
public Buffers(int capacity)
capacity
- Must be an unsigned positive short [1 .. Short.MAX_VALUE]public Buffers(java.nio.ByteBuffer... data)
public int position()
public Buffers position(int new_pos)
public int limit()
public Buffers limit(int new_limit)
public int nextToCopy()
public Buffers nextToCopy(int next)
public int remaining()
public boolean hasRemaining()
public Buffers add(java.nio.ByteBuffer... buffers)
public Buffers add(java.nio.ByteBuffer buf)
public java.nio.ByteBuffer get(int index)
public Buffers set(int index, java.nio.ByteBuffer buf)
public Buffers remove(int index)
public java.nio.ByteBuffer readLengthAndData(java.nio.channels.SocketChannel ch) throws java.lang.Exception
ch
- The channel to read data fromjava.lang.Exception
public boolean read(java.nio.channels.SocketChannel ch) throws java.lang.Exception
Buffer.clear()
has to be called (all buffers have their position == limit on a successfull read).java.lang.Exception
public boolean write(java.nio.channels.GatheringByteChannel ch, java.nio.ByteBuffer... buffers) throws java.lang.Exception
java.lang.Exception
public boolean write(java.nio.channels.GatheringByteChannel ch) throws java.lang.Exception
Buffer.position
at the start of the data to be written
be at the start of the data to be written.ch
- The channel to write tojava.lang.Exception
- Thrown if the write failedpublic Buffers copy()
public int size()
public java.lang.String toString()
toString
in class java.lang.Object
protected boolean spaceAvailable(int num_buffers)
protected boolean makeSpace()
protected boolean nullData()
protected boolean adjustPosition(boolean null_complete_data)
protected static short toPositiveUnsignedShort(int num)
protected static void assertPositiveUnsignedShort(int num)
public static java.nio.ByteBuffer copyBuffer(java.nio.ByteBuffer buf)
public java.util.Iterator<java.nio.ByteBuffer> iterator()
iterator
in interface java.lang.Iterable<java.nio.ByteBuffer>
Copyright © 1998-2020 Red Hat. All Rights Reserved.