Class ByteBufs

java.lang.Object
io.activej.bytebuf.ByteBufs
All Implemented Interfaces:
Recyclable

public final class ByteBufs extends Object implements Recyclable
Represents a circular FIFO queue of ByteBufs optimized for efficient work with multiple ByteBufs.

There are first and last indexes which represent which ByteBuf of the queue is currently the first and the last to be taken.

  • Constructor Details

    • ByteBufs

      public ByteBufs()
      Returns ByteBufs whose capacity is 8.
    • ByteBufs

      public ByteBufs(int capacity)
  • Method Details

    • collector

      public static Collector<ByteBuf,ByteBufs,ByteBuf> collector()
      Accumulates input ByteBufs into ByteBufs and then transforms accumulated result into another ByteBuf.
      Returns:
      a Collector described with ByteBuf, ByteBufs and a resulting ByteBuf
    • collector

      public static Collector<ByteBuf,ByteBufs,ByteBuf> collector(int maxSize)
    • add

      public void add(@NotNull @NotNull ByteBuf buf)
      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 last position of the queue. Then last index is increased by 1 or set to the value 0 if it has run a full circle of the queue.

      If last and first indexes become the same, this ByteBufs size will be doubled.

      Parameters:
      buf - the ByteBuf to be added to the queue
    • addAll

      public void addAll(@NotNull @NotNull Iterable<ByteBuf> byteBufs)
    • take

      @NotNull public @NotNull ByteBuf take()
      Returns the first ByteBuf of the queue if the queue is not empty. Then first index 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

      @Nullable public @Nullable ByteBuf poll()
      Returns the first ByteBuf of the queue if the queue is not empty otherwise returns null.
      Returns:
      the first ByteBuf of this ByteBufs. If the queue is empty, returns null
      See Also:
    • takeAtMost

      @NotNull public @NotNull ByteBuf takeAtMost(int size)
      Creates and returns a ByteBuf.ByteBufSlice which contains size bytes 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 first index 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

      @NotNull public @NotNull ByteBuf takeAtLeast(int size)
      Creates and returns a ByteBuf which contains at least size bytes from queue's first ByteBuf if the latter contains enough bytes. Then first index 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 ByteBufPool with size bytes 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 size bytes
    • takeAtLeast

      @NotNull public @NotNull ByteBuf takeAtLeast(int size, @NotNull @NotNull Consumer<ByteBuf> recycledBufs)
    • takeExactSize

      @NotNull public @NotNull ByteBuf takeExactSize(int exactSize)
      Creates and returns a ByteBuf which contains all bytes from the queue's first ByteBuf if the latter contains exactSize of bytes. Then first index 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 exactSize which contains all bytes from queue's first ByteBuf.

      Parameters:
      exactSize - the size of returned ByteBuf
      Returns:
      ByteBuf with exactSize bytes
    • takeExactSize

      @NotNull public @NotNull ByteBuf takeExactSize(int exactSize, @NotNull @NotNull Consumer<ByteBuf> recycledBufs)
    • consume

      public void consume(int size, @NotNull @NotNull Consumer<ByteBuf> consumer)
      Consumes the first ByteBuf of the queue to the provided consumer if the ByteBuf has at least size bytes available for reading. If after consuming ByteBuf has no readable bytes left, it is recycled and first index 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 size bytes 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 consumed
      consumer - a consumer for the ByteBuf
    • takeRemaining

      @NotNull public @NotNull ByteBuf takeRemaining()
      Creates and returns a ByteBuf with all remaining bytes of the queue.
      Returns:
      ByteBuf with all remaining bytes
    • peekBuf

      @Contract(pure=true) public ByteBuf 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

      @Contract(pure=true) @NotNull public @NotNull ByteBuf peekBuf(int n)
      Returns the ByteBuf of the given index relatively to the first index (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 least remaining bytes.
      Parameters:
      remaining - number of bytes to be checked
      Returns:
      true if the queue contains at least remaining bytes
    • getByte

      public byte getByte()
      Returns the first byte of the first ByteBuf of this queue and increases ByteBuf.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)
      Removes maxSize bytes from this bufs.
      Parameters:
      maxSize - number of bytes to be removed
      Returns:
      number of removed bytes
    • skip

      public int skip(int maxSize, @NotNull @NotNull Consumer<ByteBuf> recycledBufs)
    • drainTo

      public int drainTo(byte[] dest, int destOffset, int maxSize)
      Adds maxSize bytes from this bufs to dest if queue contains more than maxSize bytes. Otherwise, adds all bytes from queue to dest. In both cases increases queue's position to the number of drained bytes.
      Parameters:
      dest - array to drain to
      destOffset - start position for adding to dest
      maxSize - number of bytes for adding
      Returns:
      number of drained bytes
    • drainTo

      public int drainTo(byte[] dest, int destOffset, int maxSize, @NotNull @NotNull Consumer<ByteBuf> recycledBufs)
    • drainTo

      public int drainTo(@NotNull @NotNull ByteBuf dest, int maxSize)
      Adds maxSize bytes from this queue to ByteBuf dest if queue contains more than maxSize bytes. Otherwise, adds all bytes from queue to dest. In both cases increases queue's position to number of drained bytes.
      Parameters:
      dest - ByteBuf for draining
      maxSize - number of bytes for adding
      Returns:
      number of drained bytes
    • drainTo

      public int drainTo(@NotNull @NotNull ByteBuf dest)
      Adds as many bytes to dest as 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

      public int drainTo(@NotNull @NotNull ByteBufs dest)
      Copies all bytes from this queue to dest, and removes it from this queue.
      Parameters:
      dest - ByteBufs for draining
      Returns:
      number of adding bytes
    • drainTo

      public int drainTo(@NotNull @NotNull ByteBufs dest, int maxSize)
      Adds to ByteBufs dest maxSize bytes from this queue. If this queue doesn't contain enough bytes, adds all bytes from this queue.
      Parameters:
      dest - ByteBufs for draining
      maxSize - number of bytes for adding
      Returns:
      number of added elements
    • scanBytes

      public int scanBytes(ByteBufs.ByteScanner byteScanner) throws MalformedDataException
      Throws:
      MalformedDataException
    • scanBytes

      public int scanBytes(int offset, ByteBufs.ByteScanner byteScanner) throws MalformedDataException
      Throws:
      MalformedDataException
    • consumeBytes

      public int consumeBytes(ByteBufs.ByteScanner byteScanner) throws MalformedDataException
      Throws:
      MalformedDataException
    • consumeBytes

      public int consumeBytes(ByteBufs.ByteScanner byteScanner, Consumer<ByteBuf> recycledBufs) throws MalformedDataException
      Throws:
      MalformedDataException
    • consumeBytes

      public int consumeBytes(int offset, ByteBufs.ByteScanner byteScanner) throws MalformedDataException
      Throws:
      MalformedDataException
    • consumeBytes

      public int consumeBytes(int offset, ByteBufs.ByteScanner byteScanner, Consumer<ByteBuf> recycledBufs) throws MalformedDataException
      Throws:
      MalformedDataException
    • asIterator

      @NotNull public @NotNull Iterator<ByteBuf> asIterator()
    • isRecycled

      public boolean isRecycled()
    • recycle

      public void recycle()
      Recycles all present ByteBufs and sets first and last indexes to 0.
      Specified by:
      recycle in interface Recyclable
    • toString

      public String toString()
      Overrides:
      toString in class Object