Package org.apache.qpid.protonj2.buffer
Class ProtonByteBuffer
- java.lang.Object
-
- org.apache.qpid.protonj2.buffer.ProtonAbstractBuffer
-
- org.apache.qpid.protonj2.buffer.ProtonByteBuffer
-
- All Implemented Interfaces:
Comparable<ProtonBuffer>
,ProtonBuffer
public class ProtonByteBuffer extends ProtonAbstractBuffer
Implementation of the ProtonBuffer interface that uses an array backing the buffer that is dynamically resized as bytes are written.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_CAPACITY
The default initial capacity used for the underlying byte array.static int
DEFAULT_MAXIMUM_CAPACITY
The default maximum capacity that this buffer can grow to.-
Fields inherited from class org.apache.qpid.protonj2.buffer.ProtonAbstractBuffer
markedReadIndex, markedWriteIndex, readIndex, writeIndex
-
-
Constructor Summary
Constructors Modifier Constructor Description ProtonByteBuffer()
Creates a newProtonByteBuffer
instance that uses default configuration values for initial capacity and the maximum allowed capacity to which the underlying byte array will grow before errors will be thrown from operations that would expand the storage.ProtonByteBuffer(byte[] array)
Creates a newProtonByteBuffer
with the given byte array as the backing store to be used initially.protected
ProtonByteBuffer(byte[] array, int maximumCapacity)
protected
ProtonByteBuffer(byte[] array, int maximumCapacity, int writeIndex)
ProtonByteBuffer(int initialCapacity)
Creates a newProtonByteBuffer
with the given initial capacity and uses the default value for the maximum capacity restriction.ProtonByteBuffer(int initialCapacity, int maximumCapacity)
Creates a newProtonByteBuffer
with the given initial capacity and the given maximum capacity restriction.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
capacity()
ProtonBuffer
capacity(int newCapacity)
Adjusts the capacity of this buffer.ProtonBuffer
copy(int index, int length)
Returns a copy of this buffer's sub-region.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.int
getArrayOffset()
byte
getByte(int index)
Gets a byte from the specified index, this method will not modify the read or write index.ProtonBuffer
getBytes(int index, byte[] destination, int destinationIndex, int length)
Transfers this buffer's data to the specified destination starting at the specified absoluteindex
.ProtonBuffer
getBytes(int index, ByteBuffer destination)
Transfers this buffer's data to the specified destination starting at the specified absoluteindex
until the destination's position reaches its limit.ProtonBuffer
getBytes(int index, ProtonBuffer destination, int destinationIndex, int length)
Transfers this buffer's data to the specified destination starting at the specified absoluteindex
.int
getInt(int index)
Gets a int from the specified index, this method will not modify the read or write index.long
getLong(int index)
Gets a long from the specified index, this method will not modify the read or write index.short
getShort(int index)
Gets a short from the specified index, this method will not modify the read or write index.boolean
hasArray()
ProtonBuffer
setByte(int index, int value)
Sets the byte value at the given write index in this buffer's backing data store.ProtonBuffer
setBytes(int index, byte[] source, int sourceIndex, int length)
Transfers the specified source array's data to this buffer starting at the specified absoluteindex
.ProtonBuffer
setBytes(int index, ByteBuffer src)
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex
until the source buffer's position reaches its limit.ProtonBuffer
setBytes(int index, ProtonBuffer source, int sourceIndex, int length)
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex
.ProtonBuffer
setInt(int index, int value)
Sets the int value at the given write index in this buffer's backing data store.ProtonBuffer
setLong(int index, long value)
Sets the long value at the given write index in this buffer's backing data store.ProtonBuffer
setShort(int index, int value)
Sets the short value at the given write index in this buffer's backing data store.ByteBuffer
toByteBuffer(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, duplicate, 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_CAPACITY
public static final int DEFAULT_CAPACITY
The default initial capacity used for the underlying byte array.- See Also:
- Constant Field Values
-
DEFAULT_MAXIMUM_CAPACITY
public static final int DEFAULT_MAXIMUM_CAPACITY
The default maximum capacity that this buffer can grow to.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProtonByteBuffer
public ProtonByteBuffer()
Creates a newProtonByteBuffer
instance that uses default configuration values for initial capacity and the maximum allowed capacity to which the underlying byte array will grow before errors will be thrown from operations that would expand the storage.
-
ProtonByteBuffer
public ProtonByteBuffer(int initialCapacity)
Creates a newProtonByteBuffer
with the given initial capacity and uses the default value for the maximum capacity restriction.- Parameters:
initialCapacity
- The initial size of the backing byte store.- Throws:
IllegalArgumentException
- if the given value is less than zero.
-
ProtonByteBuffer
public ProtonByteBuffer(int initialCapacity, int maximumCapacity)
Creates a newProtonByteBuffer
with the given initial capacity and the given maximum capacity restriction.- Parameters:
initialCapacity
- The initial size of the backing byte store.maximumCapacity
- The maximum size the backing byte store is allowed to grow.- Throws:
IllegalArgumentException
- if the given value is less than zero or greater than the maximum.
-
ProtonByteBuffer
public ProtonByteBuffer(byte[] array)
Creates a newProtonByteBuffer
with the given byte array as the backing store to be used initially. The buffer uses the default value for maximum capacity meaning as the buffer is written to the backing store will eventually be reallocated and no longer wrap the original array. The resulting buffer will have a read index of zero and a write index set to the size of the backing array.- Parameters:
array
- The initial array use use as the backing store for this byte buffer.
-
ProtonByteBuffer
protected ProtonByteBuffer(byte[] array, int maximumCapacity)
-
ProtonByteBuffer
protected ProtonByteBuffer(byte[] array, int maximumCapacity, int writeIndex)
-
-
Method Detail
-
capacity
public int capacity()
- Returns:
- the number of bytes this buffer can currently contain.
-
capacity
public ProtonBuffer capacity(int newCapacity)
Description copied from interface:ProtonBuffer
Adjusts 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.
-
copy
public ProtonBuffer copy(int index, int length)
Description copied from interface:ProtonBuffer
Returns 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:
copy
in interfaceProtonBuffer
- Specified by:
copy
in 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 ByteBuffer toByteBuffer(int index, int length)
Description copied from interface:ProtonBuffer
Returns 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:
toByteBuffer
in interfaceProtonBuffer
- Specified by:
toByteBuffer
in 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.
-
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:ProtonBuffer
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.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.
-
getByte
public byte getByte(int index)
Description copied from interface:ProtonBuffer
Gets 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:ProtonBuffer
Gets 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:ProtonBuffer
Gets 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:ProtonBuffer
Gets 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:ProtonBuffer
Transfers this buffer's data to the specified destination starting at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
of 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 destinationIndex, int length)
Description copied from interface:ProtonBuffer
Transfers 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 todestinationIndex
- 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, ByteBuffer destination)
Description copied from interface:ProtonBuffer
Transfers this buffer's data to the specified destination starting at the specified absoluteindex
until the destination's position reaches its limit. This method does not modify#getReadIndex()
or#getWriteIndex()
of this buffer while the destination'sposition
will 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:ProtonBuffer
Sets 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:ProtonBuffer
Sets 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:ProtonBuffer
Sets 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:ProtonBuffer
Sets 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:ProtonBuffer
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
of 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:ProtonBuffer
Transfers the specified source array's data to this buffer starting at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
of 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, ByteBuffer src)
Description copied from interface:ProtonBuffer
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex
until the source buffer's position reaches its limit. This method does not modifyreadIndex
orwriteIndex
of this buffer.- Parameters:
index
- The index in this buffer where the write operation starts.src
- The source buffer from which the bytes are read.- Returns:
- this buffer for chaining
-
-