Class ByteArrayBuffer

  • All Implemented Interfaces:
    Readable
    Direct Known Subclasses:
    SessionWorkBuffer

    public class ByteArrayBuffer
    extends Buffer
    Provides an implementation of Buffer using a backing byte array
    • Field Detail

      • DEFAULT_SIZE

        public static final int DEFAULT_SIZE
        Initial default allocated buffer size if none specified
        See Also:
        Constant Field Values
      • data

        private byte[] data
      • rpos

        private int rpos
      • wpos

        private int wpos
    • Constructor Detail

      • ByteArrayBuffer

        public ByteArrayBuffer()
        Allocates a buffer for writing purposes with 256 bytes
      • ByteArrayBuffer

        public ByteArrayBuffer​(int size)
        Allocates a buffer for writing purposes
        Parameters:
        size - Initial buffer size - Note: it is rounded to the closest power of 2 that is greater or equal to it.
        See Also:
        ByteArrayBuffer(int, boolean)
      • ByteArrayBuffer

        public ByteArrayBuffer​(int size,
                               boolean roundOff)
        Allocates a buffer for writing purposes
        Parameters:
        size - Initial buffer size
        roundOff - Whether to round it to closest power of 2 that is greater or equal to the specified size
      • ByteArrayBuffer

        public ByteArrayBuffer​(byte[] data)
        Wraps data bytes for reading
        Parameters:
        data - Data bytes to read from
        See Also:
        ByteArrayBuffer(byte[], boolean)
      • ByteArrayBuffer

        public ByteArrayBuffer​(byte[] data,
                               boolean read)
        Parameters:
        data - Data bytes to use
        read - Whether the data bytes are for reading or writing
      • ByteArrayBuffer

        public ByteArrayBuffer​(byte[] data,
                               int off,
                               int len)
        Wraps data bytes for reading
        Parameters:
        data - Data bytes to read from
        off - Offset to read from
        len - Available bytes from given offset
        See Also:
        ByteArrayBuffer(byte[], int, int, boolean)
      • ByteArrayBuffer

        public ByteArrayBuffer​(byte[] data,
                               int off,
                               int len,
                               boolean read)
        Parameters:
        data - Data bytes to use
        off - Offset to read/write (according to read parameter)
        len - Available bytes from given offset
        read - Whether the data bytes are for reading or writing
    • Method Detail

      • rpos

        public int rpos()
        Specified by:
        rpos in class Buffer
        Returns:
        Current reading position
      • rpos

        public void rpos​(int rpos)
        Specified by:
        rpos in class Buffer
        Parameters:
        rpos - Set current reading position
      • wpos

        public int wpos()
        Specified by:
        wpos in class Buffer
        Returns:
        Current writing position
      • wpos

        public void wpos​(int wpos)
        Specified by:
        wpos in class Buffer
        Parameters:
        wpos - Set current writing position - Note: if necessary, the underlying data buffer will be increased so as to allow writing from the new position
      • available

        public int available()
      • capacity

        public int capacity()
        Specified by:
        capacity in class Buffer
        Returns:
        Number of bytes that can still be written without re-sizing the internal buffer
      • array

        public byte[] array()
        Specified by:
        array in class Buffer
        Returns:
        The raw underlying data bytes
      • getBytesConsumed

        public byte[] getBytesConsumed()
        Specified by:
        getBytesConsumed in class Buffer
        Returns:
        The bytes consumed so far
      • rawByte

        public byte rawByte​(int pos)
        Overrides:
        rawByte in class Buffer
        Parameters:
        pos - A position in the raw underlying data bytes
        Returns:
        The byte at the specified position without changing the current read position. Note: no validation is made whether the position lies within array boundaries
      • rawUInt

        public long rawUInt​(int pos)
        Overrides:
        rawUInt in class Buffer
        Parameters:
        pos - A position in the raw underlying data bytes
        Returns:
        The unsigned 32 bit integer at the specified position without changing the current read position. Note: no validation is made whether the position and the required extra 4 bytes lie within array boundaries
      • compact

        public void compact()
        Description copied from class: Buffer
        "Shift" the internal data so that reading starts from position zero.
        Specified by:
        compact in class Buffer
      • clear

        public Buffer clear​(boolean wipeData)
        Description copied from class: Buffer
        Reset read/write positions to zero
        Specified by:
        clear in class Buffer
        Parameters:
        wipeData - Whether to zero any previously existing data
        Returns:
        Reference to this buffer
      • getByte

        public byte getByte()
        Overrides:
        getByte in class Buffer
      • putByte

        public void putByte​(byte b)
        Overrides:
        putByte in class Buffer
      • putBuffer

        public void putBuffer​(java.nio.ByteBuffer buffer)
        Specified by:
        putBuffer in class Buffer
      • putRawBytes

        public void putRawBytes​(byte[] d,
                                int off,
                                int len)
        Specified by:
        putRawBytes in class Buffer
      • getString

        public java.lang.String getString​(java.nio.charset.Charset charset)
        Description copied from class: Buffer
        Reads a string using a given charset.
        Specified by:
        getString in class Buffer
        Parameters:
        charset - The Charset to use for the string bytes
        Returns:
        The read string
      • getRawBytes

        public void getRawBytes​(byte[] buf,
                                int off,
                                int len)
      • copyRawBytes

        protected void copyRawBytes​(int offset,
                                    byte[] buf,
                                    int pos,
                                    int len)
        Specified by:
        copyRawBytes in class Buffer
      • ensureCapacity

        public Buffer ensureCapacity​(int capacity,
                                     java.util.function.IntUnaryOperator growthFactor)
        Specified by:
        ensureCapacity in class Buffer
        Parameters:
        capacity - The required capacity
        growthFactor - An IntUnaryOperator that is invoked if the current capacity is insufficient. The argument is the minimum required new data length, the function result should be the effective new data length to be allocated - if less than minimum then an exception is thrown
        Returns:
        This buffer instance
      • size

        protected int size()
        Specified by:
        size in class Buffer
        Returns:
        Current size of underlying backing data bytes array
      • getCompactClone

        public static ByteArrayBuffer getCompactClone​(byte[] data)
        Creates a compact buffer (i.e., one that starts at offset zero) containing a copy of the original data
        Parameters:
        data - The original data buffer
        Returns:
        A ByteArrayBuffer containing a copy of the original data starting at zero read position
        See Also:
        getCompactClone(byte[], int, int)
      • getCompactClone

        public static ByteArrayBuffer getCompactClone​(byte[] data,
                                                      int offset,
                                                      int len)
        Creates a compact buffer (i.e., one that starts at offset zero) containing a copy of the original data
        Parameters:
        data - The original data buffer
        offset - The offset of the valid data in the buffer
        len - The size (in bytes) of of the valid data in the buffer
        Returns:
        A ByteArrayBuffer containing a copy of the original data starting at zero read position