Class ByteBuf

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

public class ByteBuf extends Object implements Recyclable
Represents a wrapper over a byte array and has 2 positions: head and tail.

When you write data to ByteBuf, it's tail increases by the amount of bytes written.

When you read data from ByteBuf, it's head increases by the amount of bytes read.

You can read bytes from ByteBuf only when tail is bigger than head.

You can write bytes to ByteBuf until tail doesn't exceed length of the underlying array.

ByteBuf is similar to a FIFO byte queue except it has no wrap-around or growth.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final byte[]
    Stores bytes of this ByteBuf.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Increases refs value by 1.
    byte[]
    Returns byte array array.
    byte[]
    Returns a byte array from head to tail.
    asString(@NotNull Charset charset)
    Returns a String created from this ByteBuf using given charset.
    byte
    at(int index)
    Returns byte from this ByteBuf which index is equal to the passed value.
    boolean
    Checks if there are bytes available for reading if this ByteBuf is not recycled.
    boolean
    Checks if there are bytes available for writing if this ByteBuf is not recycled.
    int
    drainTo(byte[] array, int offset, int length)
    Drains bytes from this ByteBuf starting from current head to a given byte array with specified offset and length.
    int
    drainTo(@NotNull ByteBuf buf, int length)
    Drains bytes to a given ByteBuf.
    static ByteBuf
    Creates an empty ByteBuf with array of size 0, tail and head both equal to 0.
    int
    find(byte b)
    Finds the given value in the array and returns its position.
    int
    find(byte[] bytes)
    Finds the given byte array in the array and returns its position.
    int
    find(byte[] bytes, int off, int len)
    Finds the given byte array in the array and returns its position.
    byte
    get()
    Returns the byte from this ByteBuf which index is equal to head.
    byte[]
    Returns a byte array from head to tail.
    getString(@NotNull Charset charset)
    Returns a String created from this ByteBuf using given charset.
    int
    Returns head if this ByteBuf is not recycled.
    void
    head(int pos)
    Sets head if this ByteBuf is not recycled.
    boolean
    isContentEqual(byte[] array)
    Checks if provided array is equal to the readable bytes of the array.
    boolean
    isContentEqual(byte[] array, int offset, int length)
    Checks if provided array is equal to the readable bytes of the array.
    boolean
    isContentEqual(@NotNull ByteBuf other)
    Checks if provided ByteBuf readable bytes are equal to the readable bytes of the array.
    protected boolean
    Checks if this ByteBuf is recycled.
    protected boolean
    Checks if this ByteBuf needs recycling by checking the value of refs.
    int
    Returns length of the array of this ByteBuf.
    void
    moveHead(int delta)
    Sets new value of head by moving it by the given delta if this ByteBuf is not recycled.
    void
    moveTail(int delta)
    Sets new value of tail by moving it by the given delta if this ByteBuf is not recycled.
    void
    Unwraps given Java's ByteBuffer into ByteBuf.
    void
    Unwraps given Java's ByteBuffer into ByteBuf.
    byte
    Returns a byte from this array which is located at head if this ByteBuf is not recycled.
    byte
    peek(int offset)
    Returns a byte from this array which is located at head position increased by the offset if this ByteBuf is not recycled.
    void
    put(byte b)
    Puts given byte to the array at the tail and increases the tail by 1.
    void
    put(byte[] bytes)
    Puts given byte array to the array at the tail and increases the tail by the length of the given array.
    void
    put(byte[] bytes, int offset, int length)
    Puts given byte array to the ByteBuf from the tail with given offset.
    void
    put(@NotNull ByteBuf buf)
    Puts given ByteBuf to this ByteBuf from the tail and increases the tail by the length of the given ByteBuf.
    int
    read(byte[] b)
     
    int
    read(byte[] b, int off, int len)
     
    boolean
     
    byte
     
    char
     
    double
     
    float
     
    int
     
    long
     
    int
    Returns the amount of bytes which are available for reading if this ByteBuf is not recycled.
    short
     
    int
     
    long
     
    void
    Recycles this ByteBuf by returning it to ByteBufPool.
    void
    Sets tail and head of this ByteBuf to 0.
    void
    set(int index, byte b)
    Sets given byte at particular position of the array if this ByteBuf is not recycled.
    @NotNull ByteBuf
    Creates a slice of this ByteBuf if it is not recycled.
    @NotNull ByteBuf
    slice(int length)
    Creates a slice of this ByteBuf with the given length if it is not recycled.
    @NotNull ByteBuf
    slice(int offset, int length)
    Creates a slice of this ByteBuf with the given offset and length.
    int
    Returns tail if this ByteBuf is not recycled.
    void
    tail(int pos)
    Sets tail if this ByteBuf is not recycled.
    Wraps this ByteBuf into Java's ByteBuffer ready to read.
     
    Wraps this ByteBuf into Java's ByteBuffer ready to write.
    static @NotNull ByteBuf
    wrap(byte[] bytes, int head, int tail)
    Wraps provided byte array into ByteBuf with specified tail and head.
    static @NotNull ByteBuf
    wrapForReading(byte[] bytes)
    Wraps provided byte array into ByteBuf with tail equal to length of provided array.
    static @NotNull ByteBuf
    wrapForWriting(byte[] bytes)
    Wraps provided byte array into ByteBuf with tail equal to 0.
    void
    write(byte[] b)
     
    void
    write(byte[] b, int off, int len)
     
    void
    writeBoolean(boolean v)
     
    void
    writeByte(byte v)
     
    void
    writeChar(char v)
     
    void
    writeDouble(double v)
     
    void
    writeFloat(float v)
     
    void
    writeInt(int v)
     
    void
    writeLong(long v)
     
    int
    Returns the amount of bytes which are available for writing if this ByteBuf is not recycled.
    void
    writeShort(short v)
     
    void
    writeVarInt(int v)
     
    void
    writeVarLong(long v)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • array

      protected final byte[] array
      Stores bytes of this ByteBuf.
  • Method Details

    • empty

      @Contract(pure=true) public static ByteBuf empty()
      Creates an empty ByteBuf with array of size 0, tail and head both equal to 0.
      Returns:
      an empty ByteBuf
    • wrapForWriting

      @Contract("_ -> new") @NotNull public static @NotNull ByteBuf wrapForWriting(byte[] bytes)
      Wraps provided byte array into ByteBuf with tail equal to 0.
      Parameters:
      bytes - byte array to be wrapped into ByteBuf
      Returns:
      ByteBuf over underlying byte array that is ready for writing
    • wrapForReading

      @Contract("_ -> new") @NotNull public static @NotNull ByteBuf wrapForReading(byte[] bytes)
      Wraps provided byte array into ByteBuf with tail equal to length of provided array.
      Parameters:
      bytes - byte array to be wrapped into ByteBuf
      Returns:
      ByteBuf over underlying byte array that is ready for reading
    • wrap

      @Contract("_, _, _ -> new") @NotNull public static @NotNull ByteBuf wrap(byte[] bytes, int head, int tail)
      Wraps provided byte array into ByteBuf with specified tail and head.
      Parameters:
      bytes - byte array to be wrapped into ByteBuf
      head - head of ByteBuf
      tail - tail of ByteBuf
      Returns:
      ByteBuf over underlying byte array with given tail and head
    • slice

      @Contract("-> new") @NotNull public @NotNull ByteBuf slice()
      Creates a slice of this ByteBuf if it is not recycled. Its head and tail won't change.

      refs increases by 1.

      Returns:
      a ByteBuf.ByteBufSlice of this ByteBuf
    • slice

      @Contract("_ -> new") @NotNull public @NotNull ByteBuf slice(int length)
      Creates a slice of this ByteBuf with the given length if it is not recycled.
      Parameters:
      length - length of the new slice. Defines tail of the new ByteBuf.ByteBufSlice. It is added to the current head.
      Returns:
      a ByteBufSlice of this ByteBuf.
    • slice

      @Contract("_, _ -> new") @NotNull public @NotNull ByteBuf slice(int offset, int length)
      Creates a slice of this ByteBuf with the given offset and length.
      Parameters:
      offset - offset from which to slice this ByteBuf.
      length - length of the slice.
      Returns:
      a ByteBufSlice of this ByteBuf with the given offset and length.
    • recycle

      public void recycle()
      Recycles this ByteBuf by returning it to ByteBufPool.
      Specified by:
      recycle in interface Recyclable
    • addRef

      public void addRef()
      Increases refs value by 1.
    • isRecycled

      @Contract(pure=true) protected boolean isRecycled()
      Checks if this ByteBuf is recycled.
      Returns:
      true or false
    • rewind

      public void rewind()
      Sets tail and head of this ByteBuf to 0.
    • isRecycleNeeded

      @Contract(pure=true) protected boolean isRecycleNeeded()
      Checks if this ByteBuf needs recycling by checking the value of refs. If the value is greater than 0, returns true.
      Returns:
      true if this ByteBuf needs recycle, otherwise false
    • toReadByteBuffer

      public ByteBuffer toReadByteBuffer()
      Wraps this ByteBuf into Java's ByteBuffer ready to read.
      Returns:
      ByteBuffer ready to read
    • toWriteByteBuffer

      public ByteBuffer toWriteByteBuffer()
      Wraps this ByteBuf into Java's ByteBuffer ready to write.
      Returns:
      ByteBuffer ready to write
    • ofReadByteBuffer

      public void ofReadByteBuffer(ByteBuffer byteBuffer)
      Unwraps given Java's ByteBuffer into ByteBuf.
      Parameters:
      byteBuffer - ByteBuffer to be unwrapped
    • ofWriteByteBuffer

      public void ofWriteByteBuffer(ByteBuffer byteBuffer)
      Unwraps given Java's ByteBuffer into ByteBuf.
      Parameters:
      byteBuffer - ByteBuffer to be unwrapped
    • array

      @Contract(value="->!null", pure=true) public byte[] array()
      Returns byte array array.
      Returns:
      array
    • limit

      @Contract(pure=true) public int limit()
      Returns length of the array of this ByteBuf.
      Returns:
      length of this ByteBuf
    • head

      @Contract(pure=true) public int head()
      Returns head if this ByteBuf is not recycled.
      Returns:
      head
    • head

      public void head(int pos)
      Sets head if this ByteBuf is not recycled.
      Parameters:
      pos - the value which will be assigned to the head. Must be smaller or equal to tail
    • tail

      @Contract(pure=true) public int tail()
      Returns tail if this ByteBuf is not recycled.
      Returns:
      tail
    • tail

      public void tail(int pos)
      Sets tail if this ByteBuf is not recycled.
      Parameters:
      pos - the value which will be assigned to the tail. Must be bigger or equal to head and smaller than length of the array
    • moveHead

      public void moveHead(int delta)
      Sets new value of head by moving it by the given delta if this ByteBuf is not recycled.
      Parameters:
      delta - the value by which current head will be moved. New head must be bigger or equal to 0 and smaller or equal to tail
    • moveTail

      public void moveTail(int delta)
      Sets new value of tail by moving it by the given delta if this ByteBuf is not recycled.
      Parameters:
      delta - the value by which current tail will be moved. New tail must be bigger or equal to head and smaller or equal to the length of the array
    • readRemaining

      @Contract(pure=true) public int readRemaining()
      Returns the amount of bytes which are available for reading if this ByteBuf is not recycled.
      Returns:
      amount of bytes available for reading
    • writeRemaining

      @Contract(pure=true) public int writeRemaining()
      Returns the amount of bytes which are available for writing if this ByteBuf is not recycled.
      Returns:
      amount of bytes available for writing
    • canRead

      @Contract(pure=true) public boolean canRead()
      Checks if there are bytes available for reading if this ByteBuf is not recycled.
      Returns:
      true if head doesn't equal tail, otherwise false
    • canWrite

      @Contract(pure=true) public boolean canWrite()
      Checks if there are bytes available for writing if this ByteBuf is not recycled.
      Returns:
      true if tail doesn't equal the length of the array, otherwise false
    • get

      public byte get()
      Returns the byte from this ByteBuf which index is equal to head. Then increases head by 1.
      Returns:
      byte value at the head
    • at

      @Contract(pure=true) public byte at(int index)
      Returns byte from this ByteBuf which index is equal to the passed value. Then increases head by 1.
      Parameters:
      index - index of the byte to be returned.
      Returns:
      the byte at the specified position.
    • peek

      @Contract(pure=true) public byte peek()
      Returns a byte from this array which is located at head if this ByteBuf is not recycled.
      Returns:
      a byte from this array which is located at head.
    • peek

      @Contract(pure=true) public byte peek(int offset)
      Returns a byte from this array which is located at head position increased by the offset if this ByteBuf is not recycled. head doesn't change.
      Parameters:
      offset - added to the head value. Received value must be smaller than current tail.
      Returns:
      a byte from this array which is located at head with provided offset
    • drainTo

      public int drainTo(byte[] array, int offset, int length)
      Drains bytes from this ByteBuf starting from current head to a given byte array with specified offset and length.

      This ByteBuf must be not recycled.

      Parameters:
      array - array to which bytes will be drained to
      offset - starting position in the destination data. The sum of the value and the length parameter must be smaller or equal to array length
      length - number of bytes to be drained to given array. Must be greater or equal to 0. The sum of the value and head must be smaller or equal to tail
      Returns:
      number of bytes that were drained.
    • drainTo

      public int drainTo(@NotNull @NotNull ByteBuf buf, int length)
      Drains bytes to a given ByteBuf.
      See Also:
    • set

      public void set(int index, byte b)
      Sets given byte at particular position of the array if this ByteBuf is not recycled.
      Parameters:
      index - the index of the array where the given byte will be set
      b - the byte to be set at the given index of the array
    • put

      public void put(byte b)
      Puts given byte to the array at the tail and increases the tail by 1.
      Parameters:
      b - the byte which will be put to the array.
    • put

      public void put(@NotNull @NotNull ByteBuf buf)
      Puts given ByteBuf to this ByteBuf from the tail and increases the tail by the length of the given ByteBuf. Then equates ByteBuf's head to tail.

      Only those bytes which are located between head and tail are put to the array.

      Parameters:
      buf - the ByteBuf which will be put to the ByteBuf
    • put

      public void put(byte[] bytes)
      Puts given byte array to the array at the tail and increases the tail by the length of the given array.
      Parameters:
      bytes - the byte array which will be put to the array
    • put

      public void put(byte[] bytes, int offset, int length)
      Puts given byte array to the ByteBuf from the tail with given offset. Increases the tail by the length of the given array.

      This ByteBuf must be not recycled. Its length must be greater or equal to the sum of its tail and the length of the byte array which will be put in it. Also, the sum of the provided offset and length of the byte array which will be put to the array must smaller or equal to the whole length of the byte array.

      Parameters:
      bytes - the byte array which will be put to the array
      offset - value of the offset in the byte array
      length - length of the byte array which will be put to the array
    • find

      public int find(byte b)
      Finds the given value in the array and returns its position.

      This ByteBuf must be not recycled.

      Parameters:
      b - the byte which is to be found in the array
      Returns:
      position of byte in the array. If the byte wasn't found, returns -1
    • find

      public int find(byte[] bytes)
      Finds the given byte array in the array and returns its position. This ByteBuf must be not recycled.
      Parameters:
      bytes - the byte array which is to be found in the array
      Returns:
      the position of byte array in the array. If the byte wasn't found, returns -1
    • find

      public int find(byte[] bytes, int off, int len)
      Finds the given byte array in the array and returns its position. This ByteBuf must be not recycled.
      Parameters:
      bytes - the byte array which is to be found in the array
      off - offset in the byte array
      len - amount of the bytes to be found
      Returns:
      the position of byte array in the array. If the byte wasn't found, returns -1
    • isContentEqual

      @Contract(pure=true) public boolean isContentEqual(byte[] array, int offset, int length)
      Checks if provided array is equal to the readable bytes of the array.
      Parameters:
      array - byte array to be compared with the array
      offset - offset value for the provided byte array
      length - amount of the bytes to be compared
      Returns:
      true if the byte array is equal to the array, otherwise false
    • isContentEqual

      @Contract(pure=true) public boolean isContentEqual(@NotNull @NotNull ByteBuf other)
      Checks if provided ByteBuf readable bytes are equal to the readable bytes of the array.
      Parameters:
      other - ByteBuf to be compared with the array
      Returns:
      true if the ByteBuf is equal to the array, otherwise false
    • isContentEqual

      @Contract(pure=true) public boolean isContentEqual(byte[] array)
      Checks if provided array is equal to the readable bytes of the array.
      Parameters:
      array - byte array to be compared with the array
      Returns:
      true if the byte array is equal to the array, otherwise false
    • getArray

      @Contract(value="->!null", pure=true) public byte[] getArray()
      Returns a byte array from head to tail. Doesn't recycle this ByteBuf.
      Returns:
      byte array from head to tail
    • asArray

      @Contract(value="->!null", pure=false) public byte[] asArray()
      Returns a byte array from head to tail. DOES recycle this ByteBuf.
      Returns:
      byte array created from this ByteBuf
    • getString

      @Contract(pure=true) public String getString(@NotNull @NotNull Charset charset)
      Returns a String created from this ByteBuf using given charset. Does not recycle this ByteBuf.
      Parameters:
      charset - charset which is used to create String from this ByteBuf.
      Returns:
      String from this ByteBuf in a given charset.
    • asString

      @Contract(pure=false) public String asString(@NotNull @NotNull Charset charset)
      Returns a String created from this ByteBuf using given charset. DOES recycle this ByteBuf.
      Parameters:
      charset - charset which is used to create string from ByteBuf
      Returns:
      String from this ByteBuf in a given charset.
    • read

      public int read(byte[] b)
    • read

      public int read(byte[] b, int off, int len)
    • readByte

      public byte readByte()
    • readBoolean

      public boolean readBoolean()
    • readChar

      public char readChar()
    • readDouble

      public double readDouble()
    • readFloat

      public float readFloat()
    • readInt

      public int readInt()
    • readVarInt

      public int readVarInt()
    • readLong

      public long readLong()
    • readShort

      public short readShort()
    • readVarLong

      public long readVarLong()
    • write

      public void write(byte[] b)
    • write

      public void write(byte[] b, int off, int len)
    • writeBoolean

      public void writeBoolean(boolean v)
    • writeByte

      public void writeByte(byte v)
    • writeChar

      public void writeChar(char v)
    • writeDouble

      public void writeDouble(double v)
    • writeFloat

      public void writeFloat(float v)
    • writeInt

      public void writeInt(int v)
    • writeLong

      public void writeLong(long v)
    • writeShort

      public void writeShort(short v)
    • writeVarInt

      public void writeVarInt(int v)
    • writeVarLong

      public void writeVarLong(long v)
    • toString

      @Contract(pure=true) public String toString()
      Overrides:
      toString in class Object