public class CompositeReadableBuffer extends java.lang.Object implements ReadableBuffer
ReadableBuffer.ByteBufferReader
Constructor and Description |
---|
CompositeReadableBuffer()
Creates a default empty composite buffer
|
Modifier and Type | Method and Description |
---|---|
CompositeReadableBuffer |
append(byte[] array)
Adds the given array into the composite buffer at the end.
|
CompositeReadableBuffer |
append(CompositeReadableBuffer buffer)
Adds the given composite buffer contents (from current position, up to the limit) into this
composite buffer at the end.
|
CompositeReadableBuffer |
append(ReadableBuffer buffer)
Adds the given readable buffer contents (from current position, up to the limit) into this
composite buffer at the end.
|
byte[] |
array()
Returns the primitive array that backs this buffer if one exists and the
buffer is not read-only.
|
int |
arrayOffset()
Returns the offset into the backing array of the first element in the buffer.
|
java.nio.ByteBuffer |
byteBuffer() |
int |
capacity()
Returns the capacity of the backing buffer of this ReadableBuffer
|
CompositeReadableBuffer |
clear()
Resets the buffer position to zero and sets the limit to the buffer capacity,
the mark value is discarded if set.
|
CompositeReadableBuffer |
duplicate()
Creates a duplicate
ReadableBuffer to this instance. |
boolean |
equals(java.lang.Object other) |
CompositeReadableBuffer |
flip()
Sets the buffer limit to the current position and the position is set to zero, the
mark value reset to undefined.
|
byte |
get()
Reads the byte at the current position and advances the position by 1.
|
CompositeReadableBuffer |
get(byte[] data)
A bulk read method that copies bytes from this buffer into the target byte array.
|
CompositeReadableBuffer |
get(byte[] data,
int offset,
int length)
A bulk read method that copies bytes from this buffer into the target byte array.
|
byte |
get(int index)
Reads the byte at the given index, the buffer position is not affected.
|
CompositeReadableBuffer |
get(WritableBuffer target)
Copy data from this buffer to the target buffer starting from the current
position and continuing until either this buffer's remaining bytes are
consumed or the target is full.
|
java.util.List<byte[]> |
getArrays() |
int |
getCurrentArrayPosition()
Gets the current position index in the current backing array, which represents the current buffer position.
|
int |
getCurrentIndex() |
double |
getDouble()
Reads eight bytes from the buffer and returns them as an double value.
|
float |
getFloat()
Reads four bytes from the buffer and returns them as an float value.
|
int |
getInt()
Reads four bytes from the buffer and returns them as an integer value.
|
long |
getLong()
Reads eight bytes from the buffer and returns them as an long value.
|
short |
getShort()
Reads two bytes from the buffer and returns them as an short value.
|
boolean |
hasArray()
Returns true if this ReadableBuffer is backed by an array which can be
accessed by the
ReadableBuffer.array() and ReadableBuffer.arrayOffset() methods. |
int |
hashCode() |
boolean |
hasRemaining() |
int |
limit() |
CompositeReadableBuffer |
limit(int limit)
Sets the current read limit of this buffer to the given value.
|
CompositeReadableBuffer |
mark()
Mark the current position of this buffer which can be returned to after a
read operation by calling
ReadableBuffer.reset() . |
int |
position() |
CompositeReadableBuffer |
position(int position)
Sets the current position of this buffer to the given value.
|
java.lang.String |
readString(java.nio.charset.CharsetDecoder decoder)
Decodes a String from the buffer using the provided
CharsetDecoder
starting the decode at the current position and reading until the limit is
reached. |
java.lang.String |
readUTF8()
Reads a UTF-8 encoded String from the buffer starting the decode at the
current position and reading until the limit is reached.
|
CompositeReadableBuffer |
reclaimRead()
Compact the buffer dropping arrays that have been consumed by previous
reads from this Composite buffer.
|
int |
remaining() |
CompositeReadableBuffer |
reset()
Reset the buffer's position to a previously marked value, the mark should remain
set after calling this method.
|
CompositeReadableBuffer |
rewind()
Resets the buffer position to zero and clears and previously set mark.
|
CompositeReadableBuffer |
slice()
Creates a new ReadableBuffer instance that is a view of the readable portion of
this buffer.
|
java.lang.String |
toString() |
public CompositeReadableBuffer()
public java.util.List<byte[]> getArrays()
public int getCurrentIndex()
public int getCurrentArrayPosition()
public boolean hasArray()
ReadableBuffer
ReadableBuffer.array()
and ReadableBuffer.arrayOffset()
methods.hasArray
in interface ReadableBuffer
public int capacity()
ReadableBuffer
capacity
in interface ReadableBuffer
public byte[] array()
ReadableBuffer
ReadableBuffer.hasArray()
method before calling this method.array
in interface ReadableBuffer
public int arrayOffset()
ReadableBuffer
ReadableBuffer.hasArray()
method before calling this method.arrayOffset
in interface ReadableBuffer
public byte get()
ReadableBuffer
get
in interface ReadableBuffer
public byte get(int index)
ReadableBuffer
get
in interface ReadableBuffer
index
- The index in the buffer from which to read the byte.public int getInt()
ReadableBuffer
getInt
in interface ReadableBuffer
public long getLong()
ReadableBuffer
getLong
in interface ReadableBuffer
public short getShort()
ReadableBuffer
getShort
in interface ReadableBuffer
public float getFloat()
ReadableBuffer
getFloat
in interface ReadableBuffer
public double getDouble()
ReadableBuffer
getDouble
in interface ReadableBuffer
public CompositeReadableBuffer get(byte[] data)
ReadableBuffer
get
in interface ReadableBuffer
data
- The byte array to copy bytes read from this buffer.public CompositeReadableBuffer get(byte[] data, int offset, int length)
ReadableBuffer
get
in interface ReadableBuffer
data
- The byte array to copy bytes read from this buffer.offset
- The offset into the given array where the copy starts.length
- The number of bytes to copy into the target array.public CompositeReadableBuffer get(WritableBuffer target)
ReadableBuffer
get
in interface ReadableBuffer
target
- The WritableBuffer to transfer this buffer's data to.public CompositeReadableBuffer position(int position)
ReadableBuffer
position
in interface ReadableBuffer
position
- The new position to set for this buffer.ReadableBuffer
.public int position()
position
in interface ReadableBuffer
public CompositeReadableBuffer slice()
ReadableBuffer
ReadableBuffer.remaining()
method, the mark
will be undefined.slice
in interface ReadableBuffer
public CompositeReadableBuffer flip()
ReadableBuffer
flip
in interface ReadableBuffer
ReadableBuffer
.public CompositeReadableBuffer limit(int limit)
ReadableBuffer
limit
in interface ReadableBuffer
limit
- The new read limit to set for this buffer.ReadableBuffer
.public int limit()
limit
in interface ReadableBuffer
public CompositeReadableBuffer mark()
ReadableBuffer
ReadableBuffer.reset()
.mark
in interface ReadableBuffer
ReadableBuffer
.public CompositeReadableBuffer reset()
ReadableBuffer
reset
in interface ReadableBuffer
ReadableBuffer
.public CompositeReadableBuffer rewind()
ReadableBuffer
rewind
in interface ReadableBuffer
ReadableBuffer
.public CompositeReadableBuffer clear()
ReadableBuffer
clear
in interface ReadableBuffer
ReadableBuffer
.public int remaining()
remaining
in interface ReadableBuffer
public boolean hasRemaining()
hasRemaining
in interface ReadableBuffer
public CompositeReadableBuffer duplicate()
ReadableBuffer
ReadableBuffer
to this instance.
The duplicated buffer will have the same position, limit and mark as this buffer. The two buffers share the same backing data.
duplicate
in interface ReadableBuffer
ReadableBuffer
.public java.nio.ByteBuffer byteBuffer()
byteBuffer
in interface ReadableBuffer
public java.lang.String readUTF8() throws java.nio.charset.CharacterCodingException
ReadableBuffer
readUTF8
in interface ReadableBuffer
java.nio.charset.CharacterCodingException
- if the encoding is invalid for any reason.public java.lang.String readString(java.nio.charset.CharsetDecoder decoder) throws java.nio.charset.CharacterCodingException
ReadableBuffer
CharsetDecoder
starting the decode at the current position and reading until the limit is
reached. The position is advanced to the limit once this method returns.
If there is no bytes remaining in the buffer when this method is called a
null is returned.readString
in interface ReadableBuffer
java.nio.charset.CharacterCodingException
- if the encoding is invalid for any reason.public CompositeReadableBuffer reclaimRead()
reclaimRead
in interface ReadableBuffer
public CompositeReadableBuffer append(byte[] array)
The appended array is not copied so changes to the source array are visible in this
buffer and vice versa. If this composite was empty than it would return true for the
hasArray()
method until another array is appended.
Calling this method resets the limit to the new capacity.
array
- The array to add to this composite buffer.CompositeReadableBuffer
.java.lang.IllegalArgumentException
- if the array is null or zero size.java.lang.IllegalStateException
- if the buffer does not allow appends.public CompositeReadableBuffer append(CompositeReadableBuffer buffer)
The appended buffer contents are not copied wherever possible, so changes to the source arrays are typically visible in this buffer and vice versa. Exceptions include where the source buffer position is not located at the start of its current backing array, or where the given buffer has a limit that doesn't encompass all of the last array used, and so the remainder of that arrays contents must be copied first to append here.
Calling this method resets the limit to the new capacity.
buffer
- the buffer with contents to append into this composite buffer.CompositeReadableBuffer
.java.lang.IllegalArgumentException
- if the given buffer is null or has zero remainder.java.lang.IllegalStateException
- if the buffer does not allow appends.public CompositeReadableBuffer append(ReadableBuffer buffer)
The appended buffer contents are not copied wherever possible, so changes to the source arrays are typically visible in this buffer and vice versa. Exceptions are where the source buffer is not backed by an array, or where the source buffer position is not located at the start of its backing array, and so the remainder of the contents must be copied first to append here.
Calling this method resets the limit to the new capacity.
buffer
- the buffer with contents to append into this composite buffer.CompositeReadableBuffer
.java.lang.IllegalArgumentException
- if the given buffer is null or has zero remainder.java.lang.IllegalStateException
- if the buffer does not allow appends.public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object