Class DataBufferWrapper

  • All Implemented Interfaces:
    DataBuffer

    public class DataBufferWrapper
    extends Object
    implements DataBuffer
    Provides a convenient implementation of the DataBuffer interface that can be overridden to adapt the delegate.

    These methods default to calling through to the wrapped delegate object.

    Since:
    5.2
    Author:
    Arjen Poutsma
    • Constructor Detail

      • DataBufferWrapper

        public DataBufferWrapper​(DataBuffer delegate)
        Create a new DataBufferWrapper that wraps the given buffer.
        Parameters:
        delegate - the buffer to wrap
    • Method Detail

      • indexOf

        public int indexOf​(IntPredicate predicate,
                           int fromIndex)
        Description copied from interface: DataBuffer
        Return the index of the first byte in this buffer that matches the given predicate.
        Specified by:
        indexOf in interface DataBuffer
        Parameters:
        predicate - the predicate to match
        fromIndex - the index to start the search from
        Returns:
        the index of the first byte that matches predicate; or -1 if none match
      • lastIndexOf

        public int lastIndexOf​(IntPredicate predicate,
                               int fromIndex)
        Description copied from interface: DataBuffer
        Return the index of the last byte in this buffer that matches the given predicate.
        Specified by:
        lastIndexOf in interface DataBuffer
        Parameters:
        predicate - the predicate to match
        fromIndex - the index to start the search from
        Returns:
        the index of the last byte that matches predicate; or -1 if none match
      • capacity

        public int capacity()
        Description copied from interface: DataBuffer
        Return the number of bytes that this buffer can contain.
        Specified by:
        capacity in interface DataBuffer
        Returns:
        the capacity
      • capacity

        public DataBuffer capacity​(int capacity)
        Description copied from interface: DataBuffer
        Set the number of bytes that this buffer can contain.

        If the new capacity is lower than the current capacity, the contents of this buffer will be truncated. If the new capacity is higher than the current capacity, it will be expanded.

        Specified by:
        capacity in interface DataBuffer
        Parameters:
        capacity - the new capacity
        Returns:
        this buffer
      • readPosition

        public DataBuffer readPosition​(int readPosition)
        Description copied from interface: DataBuffer
        Set the position from which this buffer will read.
        Specified by:
        readPosition in interface DataBuffer
        Parameters:
        readPosition - the new read position
        Returns:
        this buffer
      • writePosition

        public DataBuffer writePosition​(int writePosition)
        Description copied from interface: DataBuffer
        Set the position to which this buffer will write.
        Specified by:
        writePosition in interface DataBuffer
        Parameters:
        writePosition - the new write position
        Returns:
        this buffer
      • getByte

        public byte getByte​(int index)
        Description copied from interface: DataBuffer
        Read a single byte at the given index from this data buffer.
        Specified by:
        getByte in interface DataBuffer
        Parameters:
        index - the index at which the byte will be read
        Returns:
        the byte at the given index
      • read

        public byte read()
        Description copied from interface: DataBuffer
        Read a single byte from the current reading position from this data buffer.
        Specified by:
        read in interface DataBuffer
        Returns:
        the byte at this buffer's current reading position
      • read

        public DataBuffer read​(byte[] destination)
        Description copied from interface: DataBuffer
        Read this buffer's data into the specified destination, starting at the current reading position of this buffer.
        Specified by:
        read in interface DataBuffer
        Parameters:
        destination - the array into which the bytes are to be written
        Returns:
        this buffer
      • read

        public DataBuffer read​(byte[] destination,
                               int offset,
                               int length)
        Description copied from interface: DataBuffer
        Read at most length bytes of this buffer into the specified destination, starting at the current reading position of this buffer.
        Specified by:
        read in interface DataBuffer
        Parameters:
        destination - the array into which the bytes are to be written
        offset - the index within destination of the first byte to be written
        length - the maximum number of bytes to be written in destination
        Returns:
        this buffer
      • write

        public DataBuffer write​(byte b)
        Description copied from interface: DataBuffer
        Write a single byte into this buffer at the current writing position.
        Specified by:
        write in interface DataBuffer
        Parameters:
        b - the byte to be written
        Returns:
        this buffer
      • write

        public DataBuffer write​(byte[] source)
        Description copied from interface: DataBuffer
        Write the given source into this buffer, starting at the current writing position of this buffer.
        Specified by:
        write in interface DataBuffer
        Parameters:
        source - the bytes to be written into this buffer
        Returns:
        this buffer
      • write

        public DataBuffer write​(byte[] source,
                                int offset,
                                int length)
        Description copied from interface: DataBuffer
        Write at most length bytes of the given source into this buffer, starting at the current writing position of this buffer.
        Specified by:
        write in interface DataBuffer
        Parameters:
        source - the bytes to be written into this buffer
        offset - the index within source to start writing from
        length - the maximum number of bytes to be written from source
        Returns:
        this buffer
      • write

        public DataBuffer write​(DataBuffer... buffers)
        Description copied from interface: DataBuffer
        Write one or more DataBuffers to this buffer, starting at the current writing position. It is the responsibility of the caller to release the given data buffers.
        Specified by:
        write in interface DataBuffer
        Parameters:
        buffers - the byte buffers to write into this buffer
        Returns:
        this buffer
      • write

        public DataBuffer write​(ByteBuffer... buffers)
        Description copied from interface: DataBuffer
        Write one or more ByteBuffer to this buffer, starting at the current writing position.
        Specified by:
        write in interface DataBuffer
        Parameters:
        buffers - the byte buffers to write into this buffer
        Returns:
        this buffer
      • write

        public DataBuffer write​(CharSequence charSequence,
                                Charset charset)
        Description copied from interface: DataBuffer
        Write the given CharSequence using the given Charset, starting at the current writing position.
        Specified by:
        write in interface DataBuffer
        Parameters:
        charSequence - the char sequence to write into this buffer
        charset - the charset to encode the char sequence with
        Returns:
        this buffer
      • slice

        public DataBuffer slice​(int index,
                                int length)
        Description copied from interface: DataBuffer
        Create a new DataBuffer whose contents is a shared subsequence of this data buffer's content. Data between this data 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.

        Note that this method will not call DataBufferUtils.retain(DataBuffer) on the resulting slice: the reference count will not be increased.

        Specified by:
        slice in interface DataBuffer
        Parameters:
        index - the index at which to start the slice
        length - the length of the slice
        Returns:
        the specified slice of this data buffer
      • retainedSlice

        public DataBuffer retainedSlice​(int index,
                                        int length)
        Description copied from interface: DataBuffer
        Create a new DataBuffer whose contents is a shared, retained subsequence of this data buffer's content. Data between this data 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.

        Note that unlike DataBuffer.slice(int, int), this method will call DataBufferUtils.retain(DataBuffer) (or equivalent) on the resulting slice.

        Specified by:
        retainedSlice in interface DataBuffer
        Parameters:
        index - the index at which to start the slice
        length - the length of the slice
        Returns:
        the specified, retained slice of this data buffer
      • asByteBuffer

        public ByteBuffer asByteBuffer()
        Description copied from interface: DataBuffer
        Expose this buffer's bytes as a ByteBuffer. Data between this DataBuffer and the returned ByteBuffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.
        Specified by:
        asByteBuffer in interface DataBuffer
        Returns:
        this data buffer as a byte buffer
      • asByteBuffer

        public ByteBuffer asByteBuffer​(int index,
                                       int length)
        Description copied from interface: DataBuffer
        Expose a subsequence of this buffer's bytes as a ByteBuffer. Data between this DataBuffer and the returned ByteBuffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.
        Specified by:
        asByteBuffer in interface DataBuffer
        Parameters:
        index - the index at which to start the byte buffer
        length - the length of the returned byte buffer
        Returns:
        this data buffer as a byte buffer
      • asInputStream

        public InputStream asInputStream​(boolean releaseOnClose)
        Description copied from interface: DataBuffer
        Expose this buffer's data as an InputStream. Both data and read position are shared between the returned stream and this data buffer.
        Specified by:
        asInputStream in interface DataBuffer
        Parameters:
        releaseOnClose - whether the underlying buffer will be released when the input stream is closed.
        Returns:
        this data buffer as an input stream
      • toString

        public String toString​(Charset charset)
        Description copied from interface: DataBuffer
        Return this buffer's data a String using the specified charset. Default implementation delegates to toString(readPosition(), readableByteCount(), charset).
        Specified by:
        toString in interface DataBuffer
        Parameters:
        charset - the character set to use
        Returns:
        a string representation of all this buffers data
      • toString

        public String toString​(int index,
                               int length,
                               Charset charset)
        Description copied from interface: DataBuffer
        Return a part of this buffer's data as a String using the specified charset.
        Specified by:
        toString in interface DataBuffer
        Parameters:
        index - the index at which to start the string
        length - the number of bytes to use for the string
        charset - the charset to use
        Returns:
        a string representation of a part of this buffers data