Class ByteBufferWriteBuffer

  • All Implemented Interfaces:
    WriteBuffer

    public final class ByteBufferWriteBuffer
    extends AbstractWriteBuffer
    implements WriteBuffer
    A WriteBuffer implementation on top of a Java NIO ByteBuffer.
    Author:
    cp 2006.04.05
    • Constructor Detail

      • ByteBufferWriteBuffer

        public ByteBufferWriteBuffer​(ByteBuffer buf)
        Construct a ByteBufferWriteBuffer on an NIO ByteBuffer.
        Parameters:
        buf - the underlying NIO ByteBuffer
      • ByteBufferWriteBuffer

        protected ByteBufferWriteBuffer​(ByteBufferWriteBuffer that)
        Perform a shallow clone of the supplied ByteBufferWriteBuffer.
        Parameters:
        that - the buffer to shallow clone
    • Method Detail

      • getByteBuffer

        public ByteBuffer getByteBuffer()
        Obtain the ByteBuffer that this WriteBuffer is based on.
        Returns:
        the underlying ByteBuffer
      • write

        public void write​(int ofDest,
                          byte b)
        Store the specified byte at the specified offset within the buffer.

        For purposes of side-effects and potential exceptions, this method is functionally equivalent to the following code:

        
         byte[] abSrc = new byte[1];
         abSrc[0] = b;
         write(ofDest, abSrc, 0, abSrc.length);
         

        Specified by:
        write in interface WriteBuffer
        Specified by:
        write in class AbstractWriteBuffer
        Parameters:
        ofDest - the offset within this buffer to store the passed data
        b - the byte to store in this buffer
      • write

        public void write​(int ofDest,
                          ReadBuffer bufSrc,
                          int ofSrc,
                          int cbSrc)
        Store the specified portion of the contents of the specified ReadBuffer at the specified offset within this buffer.

        For purposes of side-effects and potential exceptions, this method is functionally equivalent to the following code:

        
         byte[] abSrc = bufSrc.toByteArray(ofSrc, cbSrc);
         write(ofDest, abSrc, 0, abSrc.length);
         

        Specified by:
        write in interface WriteBuffer
        Overrides:
        write in class AbstractWriteBuffer
        Parameters:
        ofDest - the offset within this buffer to store the passed data
        bufSrc - the array of bytes to store in this buffer
        ofSrc - the offset within the passed ReadBuffer to copy from
        cbSrc - the number of bytes to copy from the passed ReadBuffer
      • length

        public int length()
        Determine the length of the data that is in the buffer. This is the actual number of bytes of data that have been written to the buffer, not the capacity of the buffer.
        Specified by:
        length in interface WriteBuffer
        Specified by:
        length in class AbstractWriteBuffer
        Returns:
        the number of bytes of data represented by this WriteBuffer
      • setLength

        public void setLength​(int cb)
        Reconfigure the length of the buffer. The length must not be longer than the available capacity.
        Parameters:
        cb - the new length of the buffer
      • retain

        public void retain​(int of,
                           int cb)
        Starting with the byte at offset of, retain cb bytes in this WriteBuffer, such that the byte at offset of is shifted to offset 0, the byte at offset of + 1 is shifted to offset 1, and so on up to the byte at offset of + cb - 1, which is shifted to offset cb - 1. After this method, the length of the buffer as indicated by the WriteBuffer.length() method will be equal to cb.

        Legal values for the offset of the first byte to retain of are (of >= 0 && of <= WriteBuffer.length()). Legal values for the number of bytes to retain cb are (cb >= 0 && cb <= WriteBuffer.length()), such that (of + cb <= WriteBuffer.length()).

        If cb is zero, then this method will have the same effect as clear. If of is zero, then this method will have the effect of truncating the data in the buffer, but no bytes will be shifted within the buffer.

        The effect on the capacity of the buffer is implementation-specific; some implementations are expected to retain the same capacity while others are expected to shrink accordingly.

        Specified by:
        retain in interface WriteBuffer
        Specified by:
        retain in class AbstractWriteBuffer
        Parameters:
        of - the offset of the first byte within the WriteBuffer that will be retained
        cb - the number of bytes to retain
      • getCapacity

        public int getCapacity()
        Determine the number of bytes that the buffer can hold without resizing itself. In other words, a WriteBuffer has getCapacity() - WriteBuffer.length() bytes that can be written to it without overflowing the current underlying buffer allocation. Since the buffer is an abstract concept, the actual mechanism for the underlying buffer is not known, but it could be a Java NIO buffer, or a byte array, etc.

        Note that if the maximum size returned by WriteBuffer.getMaximumCapacity() is greater than the current size returned by this method, then the WriteBuffer will automatically resize itself to allocate more space when the amount of data written to it passes the current size.

        Specified by:
        getCapacity in interface WriteBuffer
        Specified by:
        getCapacity in class AbstractWriteBuffer
        Returns:
        the number of bytes of data that this WriteBuffer can hold without resizing its underlying buffer
      • getBufferOutput

        public WriteBuffer.BufferOutput getBufferOutput​(int of)
        Get a BufferOutput object to write data to this buffer starting at a particular offset.

        Note that each call to this method will return a new BufferOutput object, with the possible exception being that a zero-length non-resizing WriteBuffer could always return the same instance (since it is not writable).

        This is functionally equivalent to:

        
         BufferOutput bufout = getBufferOutput();
         bufout.setOffset(of);
         return bufout;
         

        Specified by:
        getBufferOutput in interface WriteBuffer
        Overrides:
        getBufferOutput in class AbstractWriteBuffer
        Parameters:
        of - the offset of the first byte of this buffer that the BufferOutput will write to
        Returns:
        a BufferOutput that will write to this buffer
      • getReadBuffer

        public ReadBuffer getReadBuffer()
        Get a ReadBuffer object that is a snapshot of this WriteBuffer's data.

        This method is functionally equivalent to the following code:

        
         ReadBuffer buf = getUnsafeReadBuffer();
         byte[] ab = buf.toByteArray();
         return new ByteArrayReadBuffer(ab);
         

        Specified by:
        getReadBuffer in interface WriteBuffer
        Overrides:
        getReadBuffer in class AbstractWriteBuffer
        Returns:
        a ReadBuffer that reflects the point-in-time contents of this WriteBuffer
      • getUnsafeReadBuffer

        public ReadBuffer getUnsafeReadBuffer()
        Get a ReadBuffer object to read data from this buffer. This method is not guaranteed to return a snapshot of this buffer's data, nor is it guaranteed to return a live view of this buffer, which means that subsequent changes to this WriteBuffer may or may not affect the contents and / or the length of the returned ReadBuffer.

        To get a snapshot, use the WriteBuffer.getReadBuffer() method.

        Specified by:
        getUnsafeReadBuffer in interface WriteBuffer
        Specified by:
        getUnsafeReadBuffer in class AbstractWriteBuffer
        Returns:
        a ReadBuffer that reflects the contents of this WriteBuffer but whose behavior is undefined if the WriteBuffer is modified
      • toByteArray

        public byte[] toByteArray()
        Returns a new byte array that holds the complete contents of this WriteBuffer.

        This method is functionally equivalent to the following code:

        
         return getUnsafeReadBuffer().toByteArray();
         

        Specified by:
        toByteArray in interface WriteBuffer
        Overrides:
        toByteArray in class AbstractWriteBuffer
        Returns:
        the contents of this WriteBuffer as a byte[]
      • toBinary

        public Binary toBinary()
        Returns a new Binary object that holds the complete contents of this WriteBuffer.

        This method is functionally equivalent to the following code:

        
         return getUnsafeReadBuffer().toBinary();
         

        Specified by:
        toBinary in interface WriteBuffer
        Overrides:
        toBinary in class AbstractWriteBuffer
        Returns:
        the contents of this WriteBuffer as a Binary object
      • clone

        public Object clone()
        Create a clone of this WriteBuffer object. Changes to the clone will not affect the original, and vice-versa.
        Specified by:
        clone in interface WriteBuffer
        Overrides:
        clone in class AbstractWriteBuffer
        Returns:
        a WriteBuffer object with the same contents as this WriteBuffer object
      • getUnsafeWriteBuffer

        public ByteBufferWriteBuffer getUnsafeWriteBuffer()
        Create a "shallow clone" of the ByteBufferWriteBuffer that uses the same underlying memory but through a different (a duplicate) ByteBuffer. This method is primarily intended to allow multiple threads to be able to write to the same block of NIO memory, by creating shallow clones of the ByteBufferWriteBuffer, one for each thread, since the ByteBuffer itself is not thread safe.
        Returns:
        a ByteBufferWriteBuffer that shares the same underlying memory, but does not share the same NIO ByteBuffer