Class ByteBufPool
ByteBufConcurrentStack which stores ByteBufs of a
particular capacity which is a power of two.
When you need a new ByteBuf, it is either created (if a ByteBuf of such capacity hasn't been used and recycled yet) or popped from the appropriate slabs' stack.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classManages stats for thisByteBufPool.static interfacestatic final classStores information about ByteBufs for stats. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull ByteBufallocate(int size) Allocates byte buffer from the pool with size ofceil(log2(size))2(rounds up to the nearest power of 2) bytes.static @NotNull ByteBufstatic @NotNull ByteBufallocateExact(int size) Allocates byte buffer in the same way asallocate(int)does, but sets its positions so that write-remaining is equal to requested size.static @NotNull ByteBufallocateExact(@NotNull MemSize size) static @NotNull ByteBufstatic @NotNull ByteBufAppends byte array to ByteBuf.static @NotNull ByteBufAppends one ByteBuf to another ByteBuf.static voidclear()Clears all the slabs and stats.static @NotNull ByteBufensureWriteRemaining(@NotNull ByteBuf buf, int newWriteRemaining) static @NotNull ByteBufensureWriteRemaining(@NotNull ByteBuf buf, int minSize, int newWriteRemaining) Checks if current ByteBuf can accommodate the needed amount of writable bytes.static @NotNull ByteBufPool.ByteBufPoolStatsgetStats()
-
Method Details
-
allocate
Allocates byte buffer from the pool with size ofceil(log2(size))2(rounds up to the nearest power of 2) bytes.Note that resource intensive
buildRegistryEntry(ByteBuf)will be executed only if {#REGISTRY} is settrue. Also, such parameters asSTATS,MIN_MAX_CHECKS,MIN_SIZE,MAX_SIZEsignificantly influence the workflow of theallocateoperation.- Parameters:
size- returned byte buffer size is guaranteed to be bigger or equal to requested size- Returns:
- byte buffer from this pool
-
allocateExact
Allocates byte buffer in the same way asallocate(int)does, but sets its positions so that write-remaining is equal to requested size.For example, if you need a
ByteBufof size 21, aByteBufof size 32 is allocated. (|______|)
But its read/write positions are set to 11 so that only last 21 are writable (|__####|)- Parameters:
size- requested size- Returns:
- byte buffer from this pool with appropriate positions set
-
allocate
-
allocateExact
-
ensureWriteRemaining
-
ensureWriteRemaining
@NotNull public static @NotNull ByteBuf ensureWriteRemaining(@NotNull @NotNull ByteBuf buf, int minSize, int newWriteRemaining) Checks if current ByteBuf can accommodate the needed amount of writable bytes.Returns this ByteBuf, if it contains enough writable bytes.
Otherwise creates a new ByteBuf which contains data from the original ByteBuf and fits the parameters. Then recycles the original ByteBuf.
- Parameters:
buf- the ByteBuf to checkminSize- the minimal size of the ByteBufnewWriteRemaining- amount of needed writable bytes- Returns:
- a ByteBuf which fits the parameters
-
append
@NotNull public static @NotNull ByteBuf append(@NotNull @NotNull ByteBuf to, @NotNull @NotNull ByteBuf from) Appends one ByteBuf to another ByteBuf. If target ByteBuf can't accommodate the ByteBuf to be appended, a new ByteBuf is created which contains both target and source ByteBufs data. The source ByteBuf is recycled after append.If target ByteBuf has no readable bytes, it is being recycled and the source ByteBuf is returned.
Both ByteBufs must be not recycled before the operation.
- Parameters:
to- the target ByteBuf to which another ByteBuf will be appendedfrom- the source ByteBuf to be appended- Returns:
- ByteBuf which contains the result of the appending
-
append
@NotNull public static @NotNull ByteBuf append(@NotNull @NotNull ByteBuf to, byte[] from, int offset, int length) Appends byte array to ByteBuf. If ByteBuf can't accommodate the byte array, a new ByteBuf is created which contains all data from the original ByteBuf and has enough capacity to accommodate the byte array.ByteBuf must be not recycled before the operation.
- Parameters:
to- the target ByteBuf to which byte array will be appendedfrom- the source byte array to be appendedoffset- the value of offset for the byte arraylength- amount of the bytes to be appended to the ByteBuf The sum of the length and offset parameters can't be greater than the whole length of the byte array- Returns:
- ByteBuf which contains the result of the appending
-
append
-
clear
public static void clear()Clears all the slabs and stats. -
getStats
-