Interface ByteBuffer<T>

Type Parameters:
T - buffer type
All Known Implementing Classes:
ByteArrayByteBuffer, DelegateByteBuffer

public interface ByteBuffer<T>
Interface to allow interfacing with different byte buffer implementations, primarily as an abstraction over Netty.
Since:
1.0
Author:
Graeme Rocher
  • Method Summary

    Modifier and Type
    Method
    Description
     
    Exposes this buffer's readable bytes as an NIO ByteBuffer.
    asNioBuffer(int index, int length)
    Exposes this buffer's sub-region as an NIO ByteBuffer.
    capacity(int capacity)
    Adjusts the capacity of this buffer.
    byte
    getByte(int index)
    Get the byte at the specified index.
    int
    indexOf(byte b)
    Find the index of the first occurrence of the given byte.
    int
    Returns the maximum allowed capacity of this buffer.
    byte
    Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.
    read(byte[] destination)
    Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= dst.length).
    read(byte[] destination, int offset, int length)
    Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= length).
    int
    Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex).
    readCharSequence(int length, Charset charset)
    Gets a CharSequence with the given length at the current readerIndex and increases the readerIndex by the given length.
    int
    Returns the readerIndex of this buffer.
    readerIndex(int readPosition)
    Sets the readerIndex of this buffer.
    slice(int index, int length)
    Create a new ByteBuffer whose contents is a shared subsequence of this data buffer's content.
    byte[]
    Create a copy of the underlying storage from buf into a byte array.
    Convert the ByteBuffer into an input stream.
    Convert the ByteBuffer into an output stream.
    toString(Charset charset)
    To string.
    int
    Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex).
    write(byte b)
    Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.
    write(byte[] source)
    Transfers the specified source array's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= src.length).
    write(byte[] source, int offset, int length)
    Transfers the specified source array's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= length).
    write(ByteBuffer... buffers)
    Write the given ByteBuffer instances to this buffer.
    write(CharSequence source, Charset charset)
    Transfers the specified source CharSequence's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= src.length).
    write(ByteBuffer... buffers)
    Write the given ByteBuffer instances to this buffer.
    int
    Returns the writerIndex of this buffer.
    writerIndex(int position)
    Sets the writerIndex of this buffer.
  • Method Details

    • asNativeBuffer

      T asNativeBuffer()
      Returns:
      The native buffer type
    • readableBytes

      int readableBytes()
      Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex).
      Returns:
      bytes
    • writableBytes

      int writableBytes()
      Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex).
      Returns:
      The bytes
    • maxCapacity

      int maxCapacity()
      Returns the maximum allowed capacity of this buffer. If a user attempts to increase the capacity of this buffer beyond the maximum capacity using capacity(int) or IllegalArgumentException.
      Returns:
      The max capacity
    • capacity

      ByteBuffer capacity(int capacity)
      Adjusts the capacity of this buffer. If the newCapacity is less than the current capacity, the content of this buffer is truncated. If the newCapacity is greater than the current capacity, the buffer is appended with unspecified data whose length is (newCapacity - currentCapacity).
      Parameters:
      capacity - capacity
      Returns:
      The bytebuffer
    • readerIndex

      int readerIndex()
      Returns the readerIndex of this buffer.
      Returns:
      The index
    • readerIndex

      ByteBuffer readerIndex(int readPosition)
      Sets the readerIndex of this buffer.
      Parameters:
      readPosition - readPosition
      Returns:
      The buffer
      Throws:
      IndexOutOfBoundsException - if the specified readerIndex is less than 0 or greater than this.writerIndex
    • writerIndex

      int writerIndex()
      Returns the writerIndex of this buffer.
      Returns:
      The index
    • writerIndex

      ByteBuffer writerIndex(int position)
      Sets the writerIndex of this buffer.
      Parameters:
      position - The position
      Returns:
      The index as buffer
      Throws:
      IndexOutOfBoundsException - if the specified writerIndex is less than this.readerIndex or greater than this.capacity
    • read

      byte read()
      Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.
      Returns:
      bytes
      Throws:
      IndexOutOfBoundsException - if this.readableBytes is less than 1
    • readCharSequence

      CharSequence readCharSequence(int length, Charset charset)
      Gets a CharSequence with the given length at the current readerIndex and increases the readerIndex by the given length.
      Parameters:
      length - the length to read
      charset - that should be used
      Returns:
      the sequence
      Throws:
      IndexOutOfBoundsException - if length is greater than this.readableBytes
    • read

      ByteBuffer read(byte[] destination)
      Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= dst.length).
      Parameters:
      destination - destination
      Returns:
      bytesBuffer
      Throws:
      IndexOutOfBoundsException - if dst.length is greater than this.readableBytes
    • read

      ByteBuffer read(byte[] destination, int offset, int length)
      Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= length).
      Parameters:
      destination - The destination byte array
      offset - the first index of the destination
      length - the number of bytes to transfer
      Returns:
      bytesBuffer
      Throws:
      IndexOutOfBoundsException - if the specified dstIndex is less than 0, if length is greater than this.readableBytes, or if dstIndex + length is greater than dst.length
    • write

      ByteBuffer write(byte b)
      Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer. The 24 high-order bits of the specified value are ignored.
      Parameters:
      b - The byte to write
      Returns:
      bytesBuffer
      Throws:
      IndexOutOfBoundsException - if this.writableBytes is less than 1
    • write

      ByteBuffer write(byte[] source)
      Transfers the specified source array's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= src.length).
      Parameters:
      source - The source bytes
      Returns:
      bytesBuffer
      Throws:
      IndexOutOfBoundsException - if src.length is greater than this.writableBytes
    • write

      ByteBuffer write(CharSequence source, Charset charset)
      Transfers the specified source CharSequence's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= src.length).
      Parameters:
      source - The char sequence
      charset - The charset
      Returns:
      This buffer
    • write

      ByteBuffer write(byte[] source, int offset, int length)
      Transfers the specified source array's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= length).
      Parameters:
      source - The source byte array
      offset - the first index of the source
      length - the number of bytes to transfer
      Returns:
      bytesBuffer
      Throws:
      IndexOutOfBoundsException - if the specified srcIndex is less than 0, if srcIndex + length is greater than src.length, or if length is greater than this.writableBytes
    • write

      ByteBuffer write(ByteBuffer... buffers)
      Write the given ByteBuffer instances to this buffer.
      Parameters:
      buffers - The buffers to write
      Returns:
      this buffer
    • write

      ByteBuffer write(ByteBuffer... buffers)
      Write the given ByteBuffer instances to this buffer.
      Parameters:
      buffers - The buffers to write
      Returns:
      this buffer
    • slice

      ByteBuffer slice(int index, int length)
      Create a new ByteBuffer whose contents is a shared subsequence of this data buffer's content. Data between this byte buffer and the returned buffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.
      Parameters:
      index - the index at which to start the slice
      length - the length of the slice
      Returns:
      the specified slice of this data buffer
    • asNioBuffer

      ByteBuffer asNioBuffer()
      Exposes this buffer's readable bytes as an NIO ByteBuffer. The returned buffer shares the content with this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method is identical to buf.nioBuffer(buf.readerIndex(), buf.readableBytes()). This method does not modify readerIndex or writerIndex of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer, and it adjusted its capacity.
      Returns:
      byteBuffer
      Throws:
      UnsupportedOperationException - if this buffer cannot create a ByteBuffer that shares the content with itself
    • asNioBuffer

      ByteBuffer asNioBuffer(int index, int length)
      Exposes this buffer's sub-region as an NIO ByteBuffer. The returned buffer shares the content with this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modify readerIndex or writerIndex of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer, and it adjusted its capacity.
      Parameters:
      index - The index
      length - The length
      Returns:
      byteBuffer
      Throws:
      UnsupportedOperationException - if this buffer cannot create a ByteBuffer that shares the content with itself
    • toInputStream

      InputStream toInputStream()
      Convert the ByteBuffer into an input stream.
      Returns:
      this buffer as an input stream
    • toOutputStream

      OutputStream toOutputStream()
      Convert the ByteBuffer into an output stream.
      Returns:
      this buffer as an input stream
    • toByteArray

      byte[] toByteArray()
      Create a copy of the underlying storage from buf into a byte array. The copy will start at readerIndex() and copy readableBytes() bytes.
      Returns:
      byte array
    • toString

      String toString(Charset charset)
      To string.
      Parameters:
      charset - converted charset
      Returns:
      string
    • indexOf

      int indexOf(byte b)
      Find the index of the first occurrence of the given byte.
      Parameters:
      b - The byte to find
      Returns:
      The index of the byte
    • getByte

      byte getByte(int index)
      Get the byte at the specified index.
      Parameters:
      index - The index
      Returns:
      The byte