Class ByteBufPool

java.lang.Object
io.activej.bytebuf.ByteBufPool

public final class ByteBufPool extends Object
Represents a pool of ByteBufs with 33 slabs. Each of these slabs is a 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.

  • Method Details

    • allocate

      @NotNull public static @NotNull ByteBuf allocate(int size)
      Allocates byte buffer from the pool with size of ceil(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 set true. Also, such parameters as STATS, MIN_MAX_CHECKS, MIN_SIZE, MAX_SIZE significantly influence the workflow of the allocate operation.

      Parameters:
      size - returned byte buffer size is guaranteed to be bigger or equal to requested size
      Returns:
      byte buffer from this pool
    • allocateExact

      @NotNull public static @NotNull ByteBuf allocateExact(int size)
      Allocates byte buffer in the same way as allocate(int) does, but sets its positions so that write-remaining is equal to requested size.

      For example, if you need a ByteBuf of size 21, a ByteBuf of 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

      @NotNull public static @NotNull ByteBuf allocate(@NotNull @NotNull MemSize size)
    • allocateExact

      @NotNull public static @NotNull ByteBuf allocateExact(@NotNull @NotNull MemSize size)
    • ensureWriteRemaining

      @NotNull public static @NotNull ByteBuf ensureWriteRemaining(@NotNull @NotNull ByteBuf buf, int newWriteRemaining)
    • 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 check
      minSize - the minimal size of the ByteBuf
      newWriteRemaining - 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 appended
      from - 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 appended
      from - the source byte array to be appended
      offset - the value of offset for the byte array
      length - 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

      @NotNull public static @NotNull ByteBuf append(@NotNull @NotNull ByteBuf to, byte[] from)
    • clear

      public static void clear()
      Clears all the slabs and stats.
    • getStats

      @NotNull public static @NotNull ByteBufPool.ByteBufPoolStats getStats()