Class CompositeReadableBuffer
- java.lang.Object
-
- org.apache.qpid.proton.codec.CompositeReadableBuffer
-
- All Implemented Interfaces:
ReadableBuffer
public class CompositeReadableBuffer extends java.lang.Object implements ReadableBuffer
ReadableBuffer implementation whose content is made up of one or more byte arrays.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.qpid.proton.codec.ReadableBuffer
ReadableBuffer.ByteBufferReader
-
-
Constructor Summary
Constructors Constructor Description CompositeReadableBuffer()Creates a default empty composite buffer
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompositeReadableBufferappend(byte[] array)Adds the given array into the composite buffer at the end.CompositeReadableBufferappend(CompositeReadableBuffer buffer)Adds the given composite buffer contents (from current position, up to the limit) into this composite buffer at the end.CompositeReadableBufferappend(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.intarrayOffset()Returns the offset into the backing array of the first element in the buffer.java.nio.ByteBufferbyteBuffer()intcapacity()Returns the capacity of the backing buffer of this ReadableBufferCompositeReadableBufferclear()Resets the buffer position to zero and sets the limit to the buffer capacity, the mark value is discarded if set.CompositeReadableBufferduplicate()Creates a duplicateReadableBufferto this instance.booleanequals(java.lang.Object other)CompositeReadableBufferflip()Sets the buffer limit to the current position and the position is set to zero, the mark value reset to undefined.byteget()Reads the byte at the current position and advances the position by 1.CompositeReadableBufferget(byte[] data)A bulk read method that copies bytes from this buffer into the target byte array.CompositeReadableBufferget(byte[] data, int offset, int length)A bulk read method that copies bytes from this buffer into the target byte array.byteget(int index)Reads the byte at the given index, the buffer position is not affected.CompositeReadableBufferget(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()intgetCurrentArrayPosition()Gets the current position index in the current backing array, which represents the current buffer position.intgetCurrentIndex()doublegetDouble()Reads eight bytes from the buffer and returns them as an double value.floatgetFloat()Reads four bytes from the buffer and returns them as an float value.intgetInt()Reads four bytes from the buffer and returns them as an integer value.longgetLong()Reads eight bytes from the buffer and returns them as an long value.shortgetShort()Reads two bytes from the buffer and returns them as an short value.booleanhasArray()Returns true if this ReadableBuffer is backed by an array which can be accessed by theReadableBuffer.array()andReadableBuffer.arrayOffset()methods.inthashCode()booleanhasRemaining()intlimit()CompositeReadableBufferlimit(int limit)Sets the current read limit of this buffer to the given value.CompositeReadableBuffermark()Mark the current position of this buffer which can be returned to after a read operation by callingReadableBuffer.reset().intposition()CompositeReadableBufferposition(int position)Sets the current position of this buffer to the given value.java.lang.StringreadString(java.nio.charset.CharsetDecoder decoder)Decodes a String from the buffer using the providedCharsetDecoderstarting the decode at the current position and reading until the limit is reached.java.lang.StringreadUTF8()Reads a UTF-8 encoded String from the buffer starting the decode at the current position and reading until the limit is reached.CompositeReadableBufferreclaimRead()Compact the buffer dropping arrays that have been consumed by previous reads from this Composite buffer.intremaining()CompositeReadableBufferreset()Reset the buffer's position to a previously marked value, the mark should remain set after calling this method.CompositeReadableBufferrewind()Resets the buffer position to zero and clears and previously set mark.CompositeReadableBufferslice()Creates a new ReadableBuffer instance that is a view of the readable portion of this buffer.java.lang.StringtoString()
-
-
-
Method Detail
-
getArrays
public java.util.List<byte[]> getArrays()
-
getCurrentIndex
public int getCurrentIndex()
-
getCurrentArrayPosition
public int getCurrentArrayPosition()
Gets the current position index in the current backing array, which represents the current buffer position. This value includes any buffer position movement, and resets when moving across array segments, so it only gives the starting offset for the first array if the buffer position is 0. Value may be out of array bounds if the the buffer currently has no content remaining.- Returns:
- the position index in the current array representing the current buffer position.
-
hasArray
public boolean hasArray()
Description copied from interface:ReadableBufferReturns true if this ReadableBuffer is backed by an array which can be accessed by theReadableBuffer.array()andReadableBuffer.arrayOffset()methods.- Specified by:
hasArrayin interfaceReadableBuffer- Returns:
- true if the buffer is backed by a primitive array.
-
capacity
public int capacity()
Description copied from interface:ReadableBufferReturns the capacity of the backing buffer of this ReadableBuffer- Specified by:
capacityin interfaceReadableBuffer- Returns:
- the capacity of the backing buffer of this ReadableBuffer
-
array
public byte[] array()
Description copied from interface:ReadableBufferReturns the primitive array that backs this buffer if one exists and the buffer is not read-only. The caller should have checked theReadableBuffer.hasArray()method before calling this method.- Specified by:
arrayin interfaceReadableBuffer- Returns:
- the array that backs this buffer is available.
-
arrayOffset
public int arrayOffset()
Description copied from interface:ReadableBufferReturns the offset into the backing array of the first element in the buffer. The caller should have checked theReadableBuffer.hasArray()method before calling this method.- Specified by:
arrayOffsetin interfaceReadableBuffer- Returns:
- the offset into the backing array of the first element in the buffer.
-
get
public byte get()
Description copied from interface:ReadableBufferReads the byte at the current position and advances the position by 1.- Specified by:
getin interfaceReadableBuffer- Returns:
- the byte at the current position.
-
get
public byte get(int index)
Description copied from interface:ReadableBufferReads the byte at the given index, the buffer position is not affected.- Specified by:
getin interfaceReadableBuffer- Parameters:
index- The index in the buffer from which to read the byte.- Returns:
- the byte value stored at the target index.
-
getInt
public int getInt()
Description copied from interface:ReadableBufferReads four bytes from the buffer and returns them as an integer value. The buffer position is advanced by four byes.- Specified by:
getIntin interfaceReadableBuffer- Returns:
- and integer value composed of bytes read from the buffer.
-
getLong
public long getLong()
Description copied from interface:ReadableBufferReads eight bytes from the buffer and returns them as an long value. The buffer position is advanced by eight byes.- Specified by:
getLongin interfaceReadableBuffer- Returns:
- and long value composed of bytes read from the buffer.
-
getShort
public short getShort()
Description copied from interface:ReadableBufferReads two bytes from the buffer and returns them as an short value. The buffer position is advanced by two byes.- Specified by:
getShortin interfaceReadableBuffer- Returns:
- and short value composed of bytes read from the buffer.
-
getFloat
public float getFloat()
Description copied from interface:ReadableBufferReads four bytes from the buffer and returns them as an float value. The buffer position is advanced by four byes.- Specified by:
getFloatin interfaceReadableBuffer- Returns:
- and float value composed of bytes read from the buffer.
-
getDouble
public double getDouble()
Description copied from interface:ReadableBufferReads eight bytes from the buffer and returns them as an double value. The buffer position is advanced by eight byes.- Specified by:
getDoublein interfaceReadableBuffer- Returns:
- and double value composed of bytes read from the buffer.
-
get
public CompositeReadableBuffer get(byte[] data)
Description copied from interface:ReadableBufferA bulk read method that copies bytes from this buffer into the target byte array.- Specified by:
getin interfaceReadableBuffer- Parameters:
data- The byte array to copy bytes read from this buffer.- Returns:
- a reference to this ReadableBuffer instance.
-
get
public CompositeReadableBuffer get(byte[] data, int offset, int length)
Description copied from interface:ReadableBufferA bulk read method that copies bytes from this buffer into the target byte array.- Specified by:
getin interfaceReadableBuffer- Parameters:
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.- Returns:
- a reference to this ReadableBuffer instance.
-
get
public CompositeReadableBuffer get(WritableBuffer target)
Description copied from interface:ReadableBufferCopy 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.- Specified by:
getin interfaceReadableBuffer- Parameters:
target- The WritableBuffer to transfer this buffer's data to.- Returns:
- a reference to this ReadableBuffer instance.
-
position
public CompositeReadableBuffer position(int position)
Description copied from interface:ReadableBufferSets the current position of this buffer to the given value. If the buffer mark value is defined and is larger than the newly set position is must be discarded.- Specified by:
positionin interfaceReadableBuffer- Parameters:
position- The new position to set for this buffer.- Returns:
- a reference to this
ReadableBuffer.
-
position
public int position()
- Specified by:
positionin interfaceReadableBuffer- Returns:
- the current position from which the next read operation will start.
-
slice
public CompositeReadableBuffer slice()
Description copied from interface:ReadableBufferCreates a new ReadableBuffer instance that is a view of the readable portion of this buffer. The position will be set to zero and the limit and the reported capacity will match the value returned by this buffer'sReadableBuffer.remaining()method, the mark will be undefined.- Specified by:
slicein interfaceReadableBuffer- Returns:
- a new ReadableBuffer that is a view of the readable portion of this buffer.
-
flip
public CompositeReadableBuffer flip()
Description copied from interface:ReadableBufferSets the buffer limit to the current position and the position is set to zero, the mark value reset to undefined.- Specified by:
flipin interfaceReadableBuffer- Returns:
- a reference to this
ReadableBuffer.
-
limit
public CompositeReadableBuffer limit(int limit)
Description copied from interface:ReadableBufferSets the current read limit of this buffer to the given value. If the buffer mark value is defined and is larger than the limit the mark will be discarded. If the position is larger than the new limit it will be reset to the new limit.- Specified by:
limitin interfaceReadableBuffer- Parameters:
limit- The new read limit to set for this buffer.- Returns:
- a reference to this
ReadableBuffer.
-
limit
public int limit()
- Specified by:
limitin interfaceReadableBuffer- Returns:
- the current value of this buffer's limit.
-
mark
public CompositeReadableBuffer mark()
Description copied from interface:ReadableBufferMark the current position of this buffer which can be returned to after a read operation by callingReadableBuffer.reset().- Specified by:
markin interfaceReadableBuffer- Returns:
- a reference to this
ReadableBuffer.
-
reset
public CompositeReadableBuffer reset()
Description copied from interface:ReadableBufferReset the buffer's position to a previously marked value, the mark should remain set after calling this method.- Specified by:
resetin interfaceReadableBuffer- Returns:
- a reference to this
ReadableBuffer.
-
rewind
public CompositeReadableBuffer rewind()
Description copied from interface:ReadableBufferResets the buffer position to zero and clears and previously set mark.- Specified by:
rewindin interfaceReadableBuffer- Returns:
- a reference to this
ReadableBuffer.
-
clear
public CompositeReadableBuffer clear()
Description copied from interface:ReadableBufferResets the buffer position to zero and sets the limit to the buffer capacity, the mark value is discarded if set.- Specified by:
clearin interfaceReadableBuffer- Returns:
- a reference to this
ReadableBuffer.
-
remaining
public int remaining()
- Specified by:
remainingin interfaceReadableBuffer- Returns:
- the remaining number of readable bytes in this buffer.
-
hasRemaining
public boolean hasRemaining()
- Specified by:
hasRemainingin interfaceReadableBuffer- Returns:
- true if there are readable bytes still remaining in this buffer.
-
duplicate
public CompositeReadableBuffer duplicate()
Description copied from interface:ReadableBufferCreates a duplicateReadableBufferto this instance.The duplicated buffer will have the same position, limit and mark as this buffer. The two buffers share the same backing data.
- Specified by:
duplicatein interfaceReadableBuffer- Returns:
- a duplicate of this
ReadableBuffer.
-
byteBuffer
public java.nio.ByteBuffer byteBuffer()
- Specified by:
byteBufferin interfaceReadableBuffer- Returns:
- a ByteBuffer view of the current readable portion of this buffer.
-
readUTF8
public java.lang.String readUTF8() throws java.nio.charset.CharacterCodingExceptionDescription copied from interface:ReadableBufferReads a UTF-8 encoded String from the buffer 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.- Specified by:
readUTF8in interfaceReadableBuffer- Returns:
- a string decoded from the remaining bytes in this buffer.
- Throws:
java.nio.charset.CharacterCodingException- if the encoding is invalid for any reason.
-
readString
public java.lang.String readString(java.nio.charset.CharsetDecoder decoder) throws java.nio.charset.CharacterCodingExceptionDescription copied from interface:ReadableBufferDecodes a String from the buffer using the providedCharsetDecoderstarting 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.- Specified by:
readStringin interfaceReadableBuffer- Returns:
- a string decoded from the remaining bytes in this buffer.
- Throws:
java.nio.charset.CharacterCodingException- if the encoding is invalid for any reason.
-
reclaimRead
public CompositeReadableBuffer reclaimRead()
Compact the buffer dropping arrays that have been consumed by previous reads from this Composite buffer. The limit is reset to the new capacity- Specified by:
reclaimReadin interfaceReadableBuffer- Returns:
- a reference to this buffer
-
append
public CompositeReadableBuffer append(byte[] array)
Adds the given array into the composite buffer at the end.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.
- Parameters:
array- The array to add to this composite buffer.- Returns:
- a reference to this
CompositeReadableBuffer. - Throws:
java.lang.IllegalArgumentException- if the array is null or zero size.java.lang.IllegalStateException- if the buffer does not allow appends.
-
append
public CompositeReadableBuffer append(CompositeReadableBuffer buffer)
Adds the given composite buffer contents (from current position, up to the limit) into this composite buffer at the end. The source buffer position will be set to its limit.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.
- Parameters:
buffer- the buffer with contents to append into this composite buffer.- Returns:
- a reference to this
CompositeReadableBuffer. - Throws:
java.lang.IllegalArgumentException- if the given buffer is null or has zero remainder.java.lang.IllegalStateException- if the buffer does not allow appends.
-
append
public CompositeReadableBuffer append(ReadableBuffer buffer)
Adds the given readable buffer contents (from current position, up to the limit) into this composite buffer at the end. The source buffer position will be set to its limit.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.
- Parameters:
buffer- the buffer with contents to append into this composite buffer.- Returns:
- a reference to this
CompositeReadableBuffer. - Throws:
java.lang.IllegalArgumentException- if the given buffer is null or has zero remainder.java.lang.IllegalStateException- if the buffer does not allow appends.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-