Class ByteBufs
- All Implemented Interfaces:
Recyclable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds provided ByteBuf to this ByteBufs.voidcollector(int maxSize) voidConsumes the first ByteBuf of the queue to the provided consumer if the ByteBuf has at leastsizebytes available for reading.intconsumeBytes(int offset, ByteBufs.ByteScanner byteScanner) intconsumeBytes(int offset, ByteBufs.ByteScanner byteScanner, Consumer<ByteBuf> recycledBufs) intconsumeBytes(ByteBufs.ByteScanner byteScanner) intconsumeBytes(ByteBufs.ByteScanner byteScanner, Consumer<ByteBuf> recycledBufs) intdrainTo(byte[] dest, int destOffset, int maxSize) AddsmaxSizebytes from this bufs todestif queue contains more thanmaxSizebytes.intintAdds as many bytes todestas it can store.intAddsmaxSizebytes from this queue to ByteBufdestif queue contains more thanmaxSizebytes.intCopies all bytes from this queue todest, and removes it from this queue.intAdds to ByteBufsdestmaxSizebytes from this queue.bytegetByte()Returns the first byte of the first ByteBuf of this queue and increasesByteBuf.head()of the ByteBuf.booleanChecks if this queue is empty.booleanhasRemainingBytes(int remaining) Checks if this queue has at leastremainingbytes.booleanisEmpty()booleanpeekBuf()Returns the first ByteBuf of this queue if the queue is not empty.@NotNull ByteBufpeekBuf(int n) Returns the ByteBuf of the given index relatively to thefirstindex (head) of the queue.bytepeekByte()Returns the first byte from this bufs without any recycling.bytepeekByte(int index) Returns the byte from this bufs of the given index (not necessarily from the first ByteBuf of the bufs).intpeekTo(byte[] dest, int destOffset, int maxSize) @Nullable ByteBufpoll()Returns the first ByteBuf of the queue if the queue is not empty otherwise returnsnull.voidrecycle()intReturns the number of ByteBufs in this queue.intReturns the number of bytes in this queue.intscanBytes(int offset, ByteBufs.ByteScanner byteScanner) intscanBytes(ByteBufs.ByteScanner byteScanner) voidsetByte(int index, byte b) intskip(int maxSize) RemovesmaxSizebytes from this bufs.int@NotNull ByteBuftake()Returns the first ByteBuf of the queue if the queue is not empty.@NotNull ByteBuftakeAtLeast(int size) Creates and returns a ByteBuf which contains at leastsizebytes from queue's first ByteBuf if the latter contains enough bytes.@NotNull ByteBuftakeAtLeast(int size, @NotNull Consumer<ByteBuf> recycledBufs) @NotNull ByteBuftakeAtMost(int size) Creates and returns aByteBuf.ByteBufSlicewhich containssizebytes from queue's first ByteBuf if the latter contains too many bytes.@NotNull ByteBuftakeExactSize(int exactSize) Creates and returns a ByteBuf which contains all bytes from the queue's first ByteBuf if the latter containsexactSizeof bytes.@NotNull ByteBuftakeExactSize(int exactSize, @NotNull Consumer<ByteBuf> recycledBufs) @NotNull ByteBufCreates and returns a ByteBuf with all remaining bytes of the queue.toString()
-
Constructor Details
-
ByteBufs
public ByteBufs()Returns ByteBufs whose capacity is 8. -
ByteBufs
public ByteBufs(int capacity)
-
-
Method Details
-
collector
Accumulates inputByteBufs intoByteBufsand then transforms accumulated result into anotherByteBuf.- Returns:
- a
Collectordescribed with ByteBuf, ByteBufs and a resulting ByteBuf
-
collector
-
add
Adds provided ByteBuf to this ByteBufs. If this ByteBuf has no readable bytes, it won't be added to the bufs and will be recycled.The added ByteBuf is set at the current
lastposition of the queue. Thenlastindex is increased by 1 or set to the value 0 if it has run a full circle of the queue.If
lastandfirstindexes become the same, this ByteBufs size will be doubled.- Parameters:
buf- the ByteBuf to be added to the queue
-
addAll
-
take
Returns the first ByteBuf of the queue if the queue is not empty. Thenfirstindex is increased by 1 or set to the value 0 if it has run a full circle of the queue.- Returns:
- the first ByteBuf of this
ByteBufs
-
poll
Returns the first ByteBuf of the queue if the queue is not empty otherwise returnsnull.- Returns:
- the first ByteBuf of this
ByteBufs. If the queue is empty, returns null - See Also:
-
takeAtMost
Creates and returns aByteBuf.ByteBufSlicewhich containssizebytes from queue's first ByteBuf if the latter contains too many bytes.Otherwise creates and returns a ByteBuf which contains all bytes from the first ByteBuf in the queue. Then
firstindex is increased by 1 or set to the value 0 if it has run a full circle of the queue.- Parameters:
size- number of bytes to be returned- Returns:
- ByteBuf with result bytes
-
takeAtLeast
Creates and returns a ByteBuf which contains at leastsizebytes from queue's first ByteBuf if the latter contains enough bytes. Thenfirstindex is increased by 1 or set to the value 0 if it has run a full circle of the queue.Otherwise a new ByteBuf is allocated from the
ByteBufPoolwithsizebytes which contains all data from the queue's first ByteBuf.- Parameters:
size- the minimum size of returned ByteBuf- Returns:
- a ByteBuf which contains at least
sizebytes
-
takeAtLeast
-
takeExactSize
Creates and returns a ByteBuf which contains all bytes from the queue's first ByteBuf if the latter containsexactSizeof bytes. Thenfirstindex is increased by 1 or set to the value 0 if it has run a full circle of the queue.Otherwise creates and returns a ByteBuf of
exactSizewhich contains all bytes from queue's first ByteBuf.- Parameters:
exactSize- the size of returned ByteBuf- Returns:
- ByteBuf with
exactSizebytes
-
takeExactSize
-
consume
Consumes the first ByteBuf of the queue to the provided consumer if the ByteBuf has at leastsizebytes available for reading. If after consuming ByteBuf has no readable bytes left, it is recycled andfirstindex is increased by 1 or set to the value 0 if it has run a full circle of the queue.If the first ByteBuf of the queue doesn't have enough bytes available for reading, a new ByteBuf with
sizebytes is created, it contains all data from the queue's first ByteBuf. This new ByteBuf is consumed and then recycled.- Parameters:
size- the size of the ByteBuf to be consumedconsumer- a consumer for the ByteBuf
-
takeRemaining
Creates and returns a ByteBuf with all remaining bytes of the queue.- Returns:
- ByteBuf with all remaining bytes
-
peekBuf
Returns the first ByteBuf of this queue if the queue is not empty. Otherwise, returns null.- Returns:
- the first ByteBuf of the queue or
null
-
peekBuf
Returns the ByteBuf of the given index relatively to thefirstindex (head) of the queue.- Parameters:
n- index of the ByteBuf to return (relatively to the head of the queue)- Returns:
- a ByteBuf of the given index
-
peekTo
public int peekTo(byte[] dest, int destOffset, int maxSize) -
remainingBufs
@Contract(pure=true) public int remainingBufs()Returns the number of ByteBufs in this queue. -
remainingBytes
@Contract(pure=true) public int remainingBytes()Returns the number of bytes in this queue. -
isEmpty
@Contract(pure=true) public boolean isEmpty() -
hasRemaining
@Contract(pure=true) public boolean hasRemaining()Checks if this queue is empty.- Returns:
- true only if there is at least one element remains in this queue
-
hasRemainingBytes
@Contract(pure=true) public boolean hasRemainingBytes(int remaining) Checks if this queue has at leastremainingbytes.- Parameters:
remaining- number of bytes to be checked- Returns:
- true if the queue contains at least
remainingbytes
-
getByte
public byte getByte()Returns the first byte of the first ByteBuf of this queue and increasesByteBuf.head()of the ByteBuf. If there are no readable bytes left after the operation, this ByteBuf will be recycled. -
peekByte
@Contract(pure=true) public byte peekByte()Returns the first byte from this bufs without any recycling. -
peekByte
@Contract(pure=true) public byte peekByte(int index) Returns the byte from this bufs of the given index (not necessarily from the first ByteBuf of the bufs).- Parameters:
index- the index at which the bytes will be returned
-
setByte
public void setByte(int index, byte b) -
skip
public int skip(int maxSize) RemovesmaxSizebytes from this bufs.- Parameters:
maxSize- number of bytes to be removed- Returns:
- number of removed bytes
-
skip
-
drainTo
public int drainTo(byte[] dest, int destOffset, int maxSize) AddsmaxSizebytes from this bufs todestif queue contains more thanmaxSizebytes. Otherwise, adds all bytes from queue todest. In both cases increases queue's position to the number of drained bytes.- Parameters:
dest- array to drain todestOffset- start position for adding to destmaxSize- number of bytes for adding- Returns:
- number of drained bytes
-
drainTo
-
drainTo
AddsmaxSizebytes from this queue to ByteBufdestif queue contains more thanmaxSizebytes. Otherwise, adds all bytes from queue to dest. In both cases increases queue's position to number of drained bytes.- Parameters:
dest-ByteBuffor drainingmaxSize- number of bytes for adding- Returns:
- number of drained bytes
-
drainTo
Adds as many bytes todestas it can store. If queue doesn't contain enough bytes - adds all byte from queue. Increases queue's position to number of drained bytes.- Parameters:
dest- ByteBuf for draining- Returns:
- number of drained bytes
-
drainTo
Copies all bytes from this queue todest, and removes it from this queue.- Parameters:
dest- ByteBufs for draining- Returns:
- number of adding bytes
-
drainTo
Adds to ByteBufsdestmaxSizebytes from this queue. If this queue doesn't contain enough bytes, adds all bytes from this queue.- Parameters:
dest-ByteBufsfor drainingmaxSize- number of bytes for adding- Returns:
- number of added elements
-
scanBytes
- Throws:
MalformedDataException
-
scanBytes
- Throws:
MalformedDataException
-
consumeBytes
- Throws:
MalformedDataException
-
consumeBytes
public int consumeBytes(ByteBufs.ByteScanner byteScanner, Consumer<ByteBuf> recycledBufs) throws MalformedDataException - Throws:
MalformedDataException
-
consumeBytes
- Throws:
MalformedDataException
-
consumeBytes
public int consumeBytes(int offset, ByteBufs.ByteScanner byteScanner, Consumer<ByteBuf> recycledBufs) throws MalformedDataException - Throws:
MalformedDataException
-
asIterator
-
isRecycled
public boolean isRecycled() -
recycle
public void recycle()- Specified by:
recyclein interfaceRecyclable
-
toString
-