Package org.apache.qpid.protonj2.buffer
Class ProtonCompositeBuffer
- java.lang.Object
-
- org.apache.qpid.protonj2.buffer.ProtonAbstractBuffer
-
- org.apache.qpid.protonj2.buffer.ProtonCompositeBuffer
-
- All Implemented Interfaces:
java.lang.Comparable<ProtonBuffer>,ProtonBuffer
public final class ProtonCompositeBuffer extends ProtonAbstractBuffer
A composite of 1 or more ProtonBuffer instances used when aggregating buffer views.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAXIMUM_CAPACITYThe default maximum capacity for a compositeProtonBuffer.-
Fields inherited from class org.apache.qpid.protonj2.buffer.ProtonAbstractBuffer
markedReadIndex, markedWriteIndex, readIndex, writeIndex
-
-
Constructor Summary
Constructors Constructor Description ProtonCompositeBuffer()Creates a Composite Buffer instance with max capacity ofInteger.MAX_VALUE.ProtonCompositeBuffer(int maximumCapacity)Creates a Composite Buffer instance with the maximum capacity provided.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ProtonCompositeBufferappend(byte[] array)Appends the given byte array to the end of the buffer segments that comprise this compositeProtonBufferinstance.ProtonCompositeBufferappend(byte[] array, int offset, int length)Appends the given byte array to the end of the buffer segments that comprise this compositeProtonBufferinstance.ProtonCompositeBufferappend(ProtonBuffer buffer)Appends the givenProtonBufferto the end of the buffer segments that comprise this compositeProtonBufferinstance.intcapacity()ProtonBuffercapacity(int newCapacity)Adjusts the capacity of this buffer.ProtonBuffercopy(int index, int length)Returns a copy of this buffer's sub-region.ProtonBufferduplicate()Create a duplicate of this ProtonBuffer instance that shares the same backing data store and but maintains separate position index values.ProtonCompositeBufferforeachBuffer(java.util.function.Consumer<ProtonBuffer> consumer)For each of the buffers contained in thisProtonCompositeBufferinstance the given consumer will be invoked with a duplicate of the buffer that can be independently modified and not affect the contents of this buffer.ProtonCompositeBufferforeachInternalBuffer(java.util.function.Consumer<ProtonBuffer> consumer)For each of the buffers contained in thisProtonCompositeBufferinstance the given consumer will be invoked with theProtonBufferthat backs this composite instance.byte[]getArray()Returns the backing array for this ProtonBuffer instance if there is such an array or throws an exception if this ProtonBuffer implementation has no backing array.intgetArrayOffset()bytegetByte(int index)Gets a byte from the specified index, this method will not modify the read or write index.ProtonBuffergetBytes(int index, byte[] destination, int offset, int length)Transfers this buffer's data to the specified destination starting at the specified absoluteindex.ProtonBuffergetBytes(int index, java.nio.ByteBuffer destination)Transfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination's position reaches its limit.ProtonBuffergetBytes(int index, ProtonBuffer destination, int destinationIndex, int length)Transfers this buffer's data to the specified destination starting at the specified absoluteindex.intgetInt(int index)Gets a int from the specified index, this method will not modify the read or write index.longgetLong(int index)Gets a long from the specified index, this method will not modify the read or write index.shortgetShort(int index)Gets a short from the specified index, this method will not modify the read or write index.booleanhasArray()intnumberOfBuffers()ProtonCompositeBufferreclaimRead()For any buffer that precedes the buffer pointed to by the current read index remove that buffer from to composite and discard.ProtonBuffersetByte(int index, int value)Sets the byte value at the given write index in this buffer's backing data store.ProtonBuffersetBytes(int index, byte[] source, int sourceIndex, int length)Transfers the specified source array's data to this buffer starting at the specified absoluteindex.ProtonBuffersetBytes(int index, java.nio.ByteBuffer source)Transfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer's position reaches its limit.ProtonBuffersetBytes(int index, ProtonBuffer source, int sourceIndex, int length)Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex.ProtonBuffersetInt(int index, int value)Sets the int value at the given write index in this buffer's backing data store.ProtonBuffersetLong(int index, long value)Sets the long value at the given write index in this buffer's backing data store.ProtonBuffersetShort(int index, int value)Sets the short value at the given write index in this buffer's backing data store.java.nio.ByteBuffertoByteBuffer(int index, int length)Returns a ByteBuffer that represents the given span of bytes from the readable portion of this buffer.-
Methods inherited from class org.apache.qpid.protonj2.buffer.ProtonAbstractBuffer
adjustIndexMarks, checkDestinationIndex, checkIndex, checkNewCapacity, checkReadableBytes, checkSourceIndex, clear, compareTo, copy, ensureWritable, equals, getBoolean, getBytes, getBytes, getBytes, getChar, getDouble, getFloat, getMaxWritableBytes, getReadableBytes, getReadIndex, getUnsignedByte, getUnsignedInt, getUnsignedShort, getWritableBytes, getWriteIndex, hashCode, isOutOfBounds, isReadable, isReadable, isWritable, isWritable, markReadIndex, markWriteIndex, maxCapacity, readBoolean, readByte, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readDouble, readFloat, readInt, readLong, readShort, resetReadIndex, resetWriteIndex, setBoolean, setBytes, setBytes, setBytes, setChar, setDouble, setFloat, setIndex, setReadIndex, setWriteIndex, skipBytes, slice, slice, toByteBuffer, toString, toString, writeBoolean, writeByte, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeDouble, writeFloat, writeInt, writeLong, writeShort
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.qpid.protonj2.buffer.ProtonBuffer
unwrap
-
-
-
-
Field Detail
-
DEFAULT_MAXIMUM_CAPACITY
public static final int DEFAULT_MAXIMUM_CAPACITY
The default maximum capacity for a compositeProtonBuffer.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProtonCompositeBuffer
public ProtonCompositeBuffer()
Creates a Composite Buffer instance with max capacity ofInteger.MAX_VALUE.
-
ProtonCompositeBuffer
public ProtonCompositeBuffer(int maximumCapacity)
Creates a Composite Buffer instance with the maximum capacity provided.- Parameters:
maximumCapacity- The maximum capacity that this buffer can grow to.
-
-
Method Detail
-
append
public ProtonCompositeBuffer append(byte[] array)
Appends the given byte array to the end of the buffer segments that comprise this compositeProtonBufferinstance.- Parameters:
array- The array to append.- Returns:
- this
ProtonCompositeBufferinstance. - Throws:
java.lang.IndexOutOfBoundsException- if the appended buffer would result in max capacity being exceeded.
-
append
public ProtonCompositeBuffer append(byte[] array, int offset, int length)
Appends the given byte array to the end of the buffer segments that comprise this compositeProtonBufferinstance.- Parameters:
array- The array to append.offset- The offset into the given array to index read and write operations.length- The usable portion of the given array.- Returns:
- this
ProtonCompositeBufferinstance. - Throws:
java.lang.IndexOutOfBoundsException- if the appended buffer would result in max capacity being exceeded.
-
append
public ProtonCompositeBuffer append(ProtonBuffer buffer)
Appends the givenProtonBufferto the end of the buffer segments that comprise this compositeProtonBufferinstance.- Parameters:
buffer- TheProtonBufferinstance to append.- Returns:
- this
ProtonCompositeBufferinstance. - Throws:
java.lang.IndexOutOfBoundsException- if the appended buffer would result in max capacity being exceeded.
-
numberOfBuffers
public int numberOfBuffers()
- Returns:
- the total number of
ProtonBuffersegments in this composite buffer instance.
-
foreachBuffer
public ProtonCompositeBuffer foreachBuffer(java.util.function.Consumer<ProtonBuffer> consumer)
For each of the buffers contained in thisProtonCompositeBufferinstance the given consumer will be invoked with a duplicate of the buffer that can be independently modified and not affect the contents of this buffer.- Parameters:
consumer- TheConsumerthat will be called with each buffer instance.- Returns:
- this
ProtonCompositeBufferinstance.
-
foreachInternalBuffer
public ProtonCompositeBuffer foreachInternalBuffer(java.util.function.Consumer<ProtonBuffer> consumer)
For each of the buffers contained in thisProtonCompositeBufferinstance the given consumer will be invoked with theProtonBufferthat backs this composite instance. Modifying theProtonBufferpassed to the consumer modified the buffer backing this composite and as such leaves this composite in an unknown and invalid state.- Parameters:
consumer- TheConsumerthat will be called with each buffer instance.- Returns:
- this
ProtonCompositeBufferinstance.
-
reclaimRead
public ProtonCompositeBuffer reclaimRead()
For any buffer that precedes the buffer pointed to by the current read index remove that buffer from to composite and discard.- Returns:
- this
ProtonCompositeBufferinstance.
-
hasArray
public boolean hasArray()
- Returns:
- true if this buffer has a backing byte array that can be accessed.
-
getArray
public byte[] getArray()
Description copied from interface:ProtonBufferReturns the backing array for this ProtonBuffer instance if there is such an array or throws an exception if this ProtonBuffer implementation has no backing array.Changes to the returned array are visible to other users of this ProtonBuffer.
- Returns:
- the backing byte array for this ProtonBuffer.
-
getArrayOffset
public int getArrayOffset()
- Returns:
- the offset of the first byte in the backing array belonging to this buffer.
-
capacity
public int capacity()
- Returns:
- the number of bytes this buffer can currently contain.
-
capacity
public ProtonBuffer capacity(int newCapacity)
Description copied from interface:ProtonBufferAdjusts the capacity of this buffer. If the new capacity is less than the current capacity, the content of this buffer is truncated. If the new capacity is greater than the current capacity, the buffer is appended with unspecified data whose length is new capacity - current capacity.- Parameters:
newCapacity- the new maximum capacity value of this buffer.- Returns:
- this buffer for using in call chaining.
-
duplicate
public ProtonBuffer duplicate()
Description copied from interface:ProtonBufferCreate a duplicate of this ProtonBuffer instance that shares the same backing data store and but maintains separate position index values. Changes to one buffer are visible in any of its duplicates. This method does not copy the read or write markers to the new buffer instance.- Specified by:
duplicatein interfaceProtonBuffer- Overrides:
duplicatein classProtonAbstractBuffer- Returns:
- a new ProtonBuffer instance that shares the backing data as this one.
-
getByte
public byte getByte(int index)
Description copied from interface:ProtonBufferGets a byte from the specified index, this method will not modify the read or write index.- Parameters:
index- The index into the buffer where the value should be read.- Returns:
- the value read from the given index.
-
getShort
public short getShort(int index)
Description copied from interface:ProtonBufferGets a short from the specified index, this method will not modify the read or write index.- Parameters:
index- The index into the buffer where the value should be read.- Returns:
- the value read from the given index.
-
getInt
public int getInt(int index)
Description copied from interface:ProtonBufferGets a int from the specified index, this method will not modify the read or write index.- Parameters:
index- The index into the buffer where the value should be read.- Returns:
- the value read from the given index.
-
getLong
public long getLong(int index)
Description copied from interface:ProtonBufferGets a long from the specified index, this method will not modify the read or write index.- Parameters:
index- The index into the buffer where the value should be read.- Returns:
- the value read from the given index.
-
getBytes
public ProtonBuffer getBytes(int index, ProtonBuffer destination, int destinationIndex, int length)
Description copied from interface:ProtonBufferTransfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modifyreadIndexorwriteIndexof both the source (i.e.this) and the destination.- Parameters:
index- The index into the buffer where the value should be read.destination- The buffer where the bytes read will be written todestinationIndex- The offset into the destination where the write startslength- The number of bytes to transfer- Returns:
- this buffer for chaining
-
getBytes
public ProtonBuffer getBytes(int index, byte[] destination, int offset, int length)
Description copied from interface:ProtonBufferTransfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modify#getReadIndex()or#getWriteIndex()of this buffer.- Parameters:
index- The index into the buffer where the value should be read.destination- The buffer where the bytes read will be written tooffset- the offset into the destination to begin writing the bytes.length- the number of bytes to transfer from this buffer to the target buffer.- Returns:
- this buffer for chaining
-
getBytes
public ProtonBuffer getBytes(int index, java.nio.ByteBuffer destination)
Description copied from interface:ProtonBufferTransfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination's position reaches its limit. This method does not modify#getReadIndex()or#getWriteIndex()of this buffer while the destination'spositionwill be increased.- Parameters:
index- The index into the buffer where the value should be read.destination- The buffer where the bytes read will be written to- Returns:
- this buffer for chaining
-
setByte
public ProtonBuffer setByte(int index, int value)
Description copied from interface:ProtonBufferSets the byte value at the given write index in this buffer's backing data store.- Parameters:
index- The index to start the write from.value- The value to write at the given index.- Returns:
- a reference to this ProtonBuffer for chaining.
-
setShort
public ProtonBuffer setShort(int index, int value)
Description copied from interface:ProtonBufferSets the short value at the given write index in this buffer's backing data store.- Parameters:
index- The index to start the write from.value- The value to write at the given index.- Returns:
- a reference to this ProtonBuffer for chaining.
-
setInt
public ProtonBuffer setInt(int index, int value)
Description copied from interface:ProtonBufferSets the int value at the given write index in this buffer's backing data store.- Parameters:
index- The index to start the write from.value- The value to write at the given index.- Returns:
- a reference to this ProtonBuffer for chaining.
-
setLong
public ProtonBuffer setLong(int index, long value)
Description copied from interface:ProtonBufferSets the long value at the given write index in this buffer's backing data store.- Parameters:
index- The index to start the write from.value- The value to write at the given index.- Returns:
- a reference to this ProtonBuffer for chaining.
-
setBytes
public ProtonBuffer setBytes(int index, ProtonBuffer source, int sourceIndex, int length)
Description copied from interface:ProtonBufferTransfers the specified source buffer's data to this buffer starting at the specified absoluteindex. This method does not modifyreadIndexorwriteIndexof both the source (i.e.this) and the destination.- Parameters:
index- The index in this buffer where the write operation starts.source- The source buffer from which the bytes are read.sourceIndex- The offset into the source where the set begins.length- The number of bytes to transfer- Returns:
- this buffer for chaining
-
setBytes
public ProtonBuffer setBytes(int index, byte[] source, int sourceIndex, int length)
Description copied from interface:ProtonBufferTransfers the specified source array's data to this buffer starting at the specified absoluteindex. This method does not modifyreadIndexorwriteIndexof this buffer.- Parameters:
index- The index in this buffer where the write operation starts.source- The source buffer from which the bytes are read.sourceIndex- The offset into the source where the set begins.length- The number of bytes to transfer- Returns:
- this buffer for chaining
-
setBytes
public ProtonBuffer setBytes(int index, java.nio.ByteBuffer source)
Description copied from interface:ProtonBufferTransfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer's position reaches its limit. This method does not modifyreadIndexorwriteIndexof this buffer.- Parameters:
index- The index in this buffer where the write operation starts.source- The source buffer from which the bytes are read.- Returns:
- this buffer for chaining
-
copy
public ProtonBuffer copy(int index, int length)
Description copied from interface:ProtonBufferReturns a copy of this buffer's sub-region. Modifying the content of the returned buffer or this buffer does not affect each other at all. This method does not modify the value returned fromProtonBuffer.getReadIndex()orProtonBuffer.getWriteIndex()of this buffer.- Specified by:
copyin interfaceProtonBuffer- Specified by:
copyin classProtonAbstractBuffer- Parameters:
index- The index in this buffer where the copy should beginlength- The number of bytes to copy to the new buffer from this one.- Returns:
- a new ProtonBuffer instance containing the copied bytes.
-
toByteBuffer
public java.nio.ByteBuffer toByteBuffer(int index, int length)Description copied from interface:ProtonBufferReturns a ByteBuffer that represents the given span of bytes from the readable portion of this buffer.This method should attempt to return a ByteBuffer that shares the backing data store with this buffer however if that is not possible it is permitted that the returned ByteBuffer contain a copy of the readable bytes of this ProtonBuffer.
- Specified by:
toByteBufferin interfaceProtonBuffer- Specified by:
toByteBufferin classProtonAbstractBuffer- Parameters:
index- The starting index in this where the ByteBuffer view should begin.length- The number of bytes to include in the ByteBuffer view.- Returns:
- a ByteBuffer that represents the given view of this buffers readable bytes.
-
-