Interface BufferData

All Known Subinterfaces:
CompositeBufferData

public interface BufferData
Wrapper around a byte array.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte[]
    Empty byte array.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Number of bytes available for reading.
    int
    Number of bytes that can be written to this instance.
    Reset read and write position and make the buffer empty.
    boolean
    Whether this buffer is fully consumed (all available bytes were read).
    default BufferData
    Copy the underlying data into a new buffer that does not retain any reference.
    static BufferData
    create(byte[] bytes)
    Fixed size buffer data fully written.
    static BufferData
    create(byte[] bytes, int position, int length)
    Fixed size buffer data fully written.
    static BufferData
    create(int length)
    Fixed size buffer data.
    static BufferData
    create(BufferData... data)
    Buffer data mapping multiple buffers.
    static BufferData
    create(String stringValue)
    Create buffer data from a string.
    static BufferData
    Create composite buffer data from a list.
    Composite buffer data that are mutable.
    Composite buffer data that are mutable with initial value.
    static BufferData
    createReadOnly(byte[] bytes, int offset, int length)
    Fixed size buffer data fully written.
    Debug this buffer as binary string.
    default String
    Debug the full buffer.
    debugDataHex(boolean fullBuffer)
    Debug this buffer as hex string.
    static BufferData
    Empty buffer data.
    default void
    forEach(int length, Function<Byte,Boolean> consumer)
    Do an operation on each byte in this buffer.
    int
    get(int index)
    Get byte at index (current read position + index).
    static BufferData
    growing(int initialLength)
    Growing buffer data.
    int
    indexOf(byte aByte)
    Find index of the provided byte from the current position.
    default int
    lastIndexOf(byte aByte)
    Find last index of the provided byte from the current position.
    int
    lastIndexOf(byte aByte, int length)
    Find last index of the provided byte from the current position.
    int
    Read a single byte from this buffer.
    default int
    read(byte[] bytes)
    Read bytes from this buffer into the provided buffer.
    int
    read(byte[] bytes, int position, int length)
    Read bytes from this buffer into the provided buffer.
    default byte[]
    Read the content of this data as bytes.
    int
    Read bytes from the input stream.
    default int
    readHpackInt(int originalValue, int bitsOfPrefix)
    HPack integer value (may be 1 or more bytes).
    default int
    Read 16-bit integer.
    default int
    Read a 24-bit integer.
    default int
    Read 32-bit integer.
    default long
    Read 64-bit long.
    default String
    readString(int length)
    Read a UTF-8 string from this buffer.
    readString(int length, Charset charset)
    Read a string from this buffer.
    default long
    Read 32-bit unsigned integer (must be represented as a long, as Java integer is 32-bit signed).
    Reset read and write position of this buffer.
    Set read position to 0, so others may read the same data from the start.
    void
    skip(int length)
    Skip the next n bytes (move read position).
    static String
    toBinaryString(int value)
    Integer to binary string.
    static int
    toInt31(int value)
    Get 31 bits of an integer, ignoring the first bit.
    trim(int x)
    Trim the last x bytes from a buffer (remove them).
    default void
    write(byte[] bytes)
    Write the byte array to this buffer.
    void
    write(byte[] bytes, int offset, int length)
    Write the byte array to this buffer.
    write(int value)
    Write a byte.
    default void
    write(BufferData toWrite)
    Write the provided buffer to this buffer.
    void
    write(BufferData toWrite, int length)
    Write n bytes from the provided buffer to this buffer.
    default void
    Write ascii string to this buffer.
    default BufferData
    writeHpackInt(int value, int prefixedInt, int bitPrefix)
    Write hpack integer to this buffer.
    default BufferData
    writeInt16(int number)
    Write 16-bit integer.
    default BufferData
    writeInt24(int number)
    Write 24-bit integer.
    default BufferData
    writeInt32(int number)
    Write 32-bit integer.
    default BufferData
    writeInt8(int number)
    Write 8-bit integer.
    void
    Write all available bytes of this buffer to the output stream.
    int
    writeTo(ByteBuffer writeBuffer, int length)
    Write n bytes from this buffer to the provided buffer.
    default BufferData
    writeUnsignedInt32(long number)
    Write 32-bit unsigned integer.
  • Field Details

    • EMPTY_BYTES

      static final byte[] EMPTY_BYTES
      Empty byte array.
  • Method Details

    • create

      static BufferData create(int length)
      Fixed size buffer data.
      Parameters:
      length - length of the underlying buffer
      Returns:
      new byte array buffer data
    • create

      static BufferData create(byte[] bytes)
      Fixed size buffer data fully written.
      Parameters:
      bytes - byte array
      Returns:
      new byte array buffer data with write position moved to the last byte
    • create

      static BufferData create(byte[] bytes, int position, int length)
      Fixed size buffer data fully written.
      Parameters:
      bytes - byte array
      position - position within the byte array
      length - number of bytes from position that contain the data
      Returns:
      new byte array buffer data read to be read
    • createReadOnly

      static BufferData createReadOnly(byte[] bytes, int offset, int length)
      Fixed size buffer data fully written.
      Parameters:
      bytes - byte array
      offset - offset within the byte array
      length - length
      Returns:
      new byte array buffer data that are read only
    • growing

      static BufferData growing(int initialLength)
      Growing buffer data. The buffer will grow when necessary to accommodate more bytes.
      Parameters:
      initialLength - initial buffer length
      Returns:
      growing buffer data
    • create

      static BufferData create(BufferData... data)
      Buffer data mapping multiple buffers.
      Parameters:
      data - data to wrap
      Returns:
      composite buffer data
    • createComposite

      static CompositeBufferData createComposite()
      Composite buffer data that are mutable.
      Returns:
      composite buffer
    • createComposite

      static CompositeBufferData createComposite(BufferData first)
      Composite buffer data that are mutable with initial value.
      Parameters:
      first - first buffer to be added to the composite buffer
      Returns:
      composite buffer
    • create

      static BufferData create(List<BufferData> data)
      Create composite buffer data from a list.
      Parameters:
      data - list of buffers to use
      Returns:
      composite buffer
    • toBinaryString

      static String toBinaryString(int value)
      Integer to binary string.
      Parameters:
      value - integer to print as binary
      Returns:
      binary value
    • toInt31

      static int toInt31(int value)
      Get 31 bits of an integer, ignoring the first bit.
      Parameters:
      value - integer value (32 bits)
      Returns:
      integer value (31 bits)
    • empty

      static BufferData empty()
      Empty buffer data.
      Returns:
      empty buffer
    • create

      static BufferData create(String stringValue)
      Create buffer data from a string.
      Parameters:
      stringValue - UTF-8 string
      Returns:
      buffer data with bytes of the string
    • reset

      BufferData reset()
      Reset read and write position of this buffer. Does not impact length.
      Returns:
      this instance
    • rewind

      BufferData rewind()
      Set read position to 0, so others may read the same data from the start.
      Returns:
      this instance
    • clear

      BufferData clear()
      Reset read and write position and make the buffer empty. Growing buffer length is set to 0
      Returns:
      this instance
    • writeTo

      void writeTo(OutputStream out)
      Write all available bytes of this buffer to the output stream.
      Parameters:
      out - output stream
    • readFrom

      int readFrom(InputStream in)
      Read bytes from the input stream. Reads at least 1 byte.
      Parameters:
      in - input stream
      Returns:
      number of bytes read, -1 if the input stream is finished
    • read

      int read()
      Read a single byte from this buffer.
      Returns:
      next byte
    • read

      default int read(byte[] bytes)
      Read bytes from this buffer into the provided buffer.
      Parameters:
      bytes - buffer to write to
      Returns:
      number of bytes actually written to the provided buffer
    • read

      int read(byte[] bytes, int position, int length)
      Read bytes from this buffer into the provided buffer.
      Parameters:
      bytes - buffer to write to
      position - position in the buffer
      length - length that can be written
      Returns:
      actual number of bytes written
    • forEach

      default void forEach(int length, Function<Byte,Boolean> consumer)
      Do an operation on each byte in this buffer.
      Parameters:
      length - number of bytes to go through
      consumer - function that consumes a byte and returns true if we should proceed to the next byte
    • readString

      default String readString(int length)
      Read a UTF-8 string from this buffer.
      Parameters:
      length - number of bytes to read
      Returns:
      string from the bytes
    • readString

      String readString(int length, Charset charset)
      Read a string from this buffer.
      Parameters:
      length - number of bytes to read
      charset - charset of the string, such as StandardCharsets.UTF_8 or StandardCharsets.US_ASCII
      Returns:
      string from the bytes
    • consumed

      boolean consumed()
      Whether this buffer is fully consumed (all available bytes were read).
      Returns:
      if this buffer is consumed
    • readInt16

      default int readInt16()
      Read 16-bit integer.
      Returns:
      integer from the next 2 bytes
    • readInt24

      default int readInt24()
      Read a 24-bit integer.
      Returns:
      integer from the next 3 bytes
    • readInt32

      default int readInt32()
      Read 32-bit integer.
      Returns:
      integer from the next 4 bytes
    • readUnsignedInt32

      default long readUnsignedInt32()
      Read 32-bit unsigned integer (must be represented as a long, as Java integer is 32-bit signed).
      Returns:
      long from the next 4 bytes
    • readLong

      default long readLong()
      Read 64-bit long.
      Returns:
      long from the next 8 bytes
    • writeInt8

      default BufferData writeInt8(int number)
      Write 8-bit integer.
      Parameters:
      number - integer to write as a single byte
      Returns:
      this buffer
    • writeInt16

      default BufferData writeInt16(int number)
      Write 16-bit integer.
      Parameters:
      number - integer to write as 2 bytes
      Returns:
      this buffer
    • writeInt24

      default BufferData writeInt24(int number)
      Write 24-bit integer.
      Parameters:
      number - integer to write as 3 bytes
      Returns:
      this buffer
    • writeInt32

      default BufferData writeInt32(int number)
      Write 32-bit integer.
      Parameters:
      number - integer to write as 4 bytes
      Returns:
      this buffer
    • writeUnsignedInt32

      default BufferData writeUnsignedInt32(long number)
      Write 32-bit unsigned integer.
      Parameters:
      number - long to write as 4 bytes
      Returns:
      this buffer
    • write

      BufferData write(int value)
      Write a byte.
      Parameters:
      value - value
      Returns:
      this buffer
    • writeTo

      int writeTo(ByteBuffer writeBuffer, int length)
      Write n bytes from this buffer to the provided buffer.
      Parameters:
      writeBuffer - buffer to write to
      length - number of bytes to write
      Returns:
      number of bytes actually written
    • write

      default void write(byte[] bytes)
      Write the byte array to this buffer.
      Parameters:
      bytes - byte to write
    • write

      void write(byte[] bytes, int offset, int length)
      Write the byte array to this buffer.
      Parameters:
      bytes - bytes to write
      offset - offset within the array
      length - number of bytes to write (from the offset)
    • write

      default void write(BufferData toWrite)
      Write the provided buffer to this buffer.
      Parameters:
      toWrite - buffer to write
    • write

      void write(BufferData toWrite, int length)
      Write n bytes from the provided buffer to this buffer.
      Parameters:
      toWrite - buffer to write
      length - number of bytes to write
    • readHpackInt

      default int readHpackInt(int originalValue, int bitsOfPrefix)
      HPack integer value (may be 1 or more bytes). TODO enforce limit that the hpack int is max 4 bytes (as otherwise we overflow int and this may be an attack) TODO enforce limit to string values (not here, but on headers processing)
      Parameters:
      originalValue - value (only bitsOfPrefix are used, bits before that are ignored)
      bitsOfPrefix - number of bits significant in the value
      Returns:
      integer value
    • writeHpackInt

      default BufferData writeHpackInt(int value, int prefixedInt, int bitPrefix)
      Write hpack integer to this buffer.
      Parameters:
      value - the full value we want to write
      prefixedInt - value to store in the other bits of the first byte
      bitPrefix - bits reserved for our value in the first byte
      Returns:
      this instance
    • debugDataBinary

      String debugDataBinary()
      Debug this buffer as binary string.
      Returns:
      binary string debug data (including headers)
    • debugDataHex

      String debugDataHex(boolean fullBuffer)
      Debug this buffer as hex string.
      Parameters:
      fullBuffer - whether to debug all bytes in this buffer (true), or only unread bytes false)
      Returns:
      hex debug data (including headers)
    • debugDataHex

      default String debugDataHex()
      Debug the full buffer.
      Returns:
      hex debug data (including headers)
    • copy

      default BufferData copy()
      Copy the underlying data into a new buffer that does not retain any reference. Reads this buffer fully and creates a new instance that is not completed.
      Returns:
      copy of data
    • available

      int available()
      Number of bytes available for reading.
      Returns:
      available bytes
    • skip

      void skip(int length)
      Skip the next n bytes (move read position).
      Parameters:
      length - number of bytes to skip
    • indexOf

      int indexOf(byte aByte)
      Find index of the provided byte from the current position.
      Parameters:
      aByte - byte to find
      Returns:
      index of the byte, or -1 if not found
    • lastIndexOf

      default int lastIndexOf(byte aByte)
      Find last index of the provided byte from the current position.
      Parameters:
      aByte - byte to find
      Returns:
      index of the byte, or -1 if not found
    • lastIndexOf

      int lastIndexOf(byte aByte, int length)
      Find last index of the provided byte from the current position.
      Parameters:
      aByte - byte to find
      length - maximal length to search for (e.g. this will be the last byte before reaching the length)
      Returns:
      index of the byte, or -1 if not found
    • trim

      BufferData trim(int x)
      Trim the last x bytes from a buffer (remove them).
      Parameters:
      x - trim by this number of bytes
      Returns:
      this instance
    • capacity

      int capacity()
      Number of bytes that can be written to this instance.
      Returns:
      capacity of this buffer
    • writeAscii

      default void writeAscii(String text)
      Write ascii string to this buffer.
      Parameters:
      text - ascii string to write
    • get

      int get(int index)
      Get byte at index (current read position + index). Does not modify any position.
      Parameters:
      index - index to get
      Returns:
      byte at the index
    • readBytes

      default byte[] readBytes()
      Read the content of this data as bytes. This method always creates a new byte array.
      Returns:
      byte array with available() bytes, may be empty