Class ContiguousBuffer

  • All Implemented Interfaces:
    SizedInBytes

    public class ContiguousBuffer
    extends Object
    implements SizedInBytes
    ContiguousBuffer is a more handy way to read and write to and from instances of ContiguousMemoryInterface. It holds a 'position' that is automatically incremented for read and writes of different primitive types. This position can be moved, pushed and popped. Range checks are only performed for block memory copies or sets, writing or reading for single primitive types is not protected.
    Author:
    royer
    • Constructor Detail

      • ContiguousBuffer

        public ContiguousBuffer​(ContiguousMemoryInterface pContiguousMemoryInterface)
        Constructs a ContiguousBuffer by wrapping a ContiguousMemoryInterface.
        Parameters:
        pContiguousMemoryInterface - contiguous memory to wrap
    • Method Detail

      • allocate

        public static ContiguousBuffer allocate​(long pLengthInBytes)
        Allocates a ContiguousBuffer (using OffHeapMemory) of given length.
        Parameters:
        pLengthInBytes - length in bytes
        Returns:
        contiguous buffer
      • wrap

        public static ContiguousBuffer wrap​(ContiguousMemoryInterface pContiguousMemoryInterface)
        Wraps a ContiguousMemoryInterface with a ContiguousBuffer.
        Parameters:
        pContiguousMemoryInterface - contiguous memory to wrap
        Returns:
        contiguous buffer
      • getContiguousMemory

        public ContiguousMemoryInterface getContiguousMemory()
        Returns the underlying ContiguousMemoryInterface.
        Returns:
        contiguous memory
      • getSizeInBytes

        public long getSizeInBytes()
        Description copied from interface: SizedInBytes
        Return this memory object's size in bytes.
        Specified by:
        getSizeInBytes in interface SizedInBytes
        Returns:
        size in bytes.
      • setPosition

        public void setPosition​(long pNewPosition)
        Sets the current position to a new value.
        Parameters:
        pNewPosition - new position value.
      • rewind

        public void rewind()
        Rewinds the position to the first valid position in the buffer.
      • clearStack

        public void clearStack()
        Clears the position stack.
      • pushPosition

        public void pushPosition()
        Pushes current position to stack.
      • popPosition

        public void popPosition()
        Pops position at the top of the stack and sets it as the new current position.
      • isPositionValid

        public boolean isPositionValid()
        Checks whether the current position is valid.
        Returns:
        true if valid, false otherwise.
      • remainingBytes

        public long remainingBytes()
        Returns the remaining bytes that can be written from the current position.
        Returns:
        remaining bytes
      • hasRemainingByte

        public boolean hasRemainingByte()
        Returns true if there are enough remaining bytes to read a single byte.
        Returns:
        true if at least byte remains
      • hasRemainingChar

        public boolean hasRemainingChar()
        Returns true if there are enough remaining bytes to read a single char.
        Returns:
        true if at least char remains
      • hasRemainingShort

        public boolean hasRemainingShort()
        Returns true if there are enough remaining bytes to read a single short.
        Returns:
        true if at least short remains
      • hasRemainingInt

        public boolean hasRemainingInt()
        Returns true if there are enough remaining bytes to read a single int.
        Returns:
        true if at least int remains
      • hasRemainingLong

        public boolean hasRemainingLong()
        Returns true if there are enough remaining bytes to read a single long.
        Returns:
        true if at least long remains
      • hasRemainingFloat

        public boolean hasRemainingFloat()
        Returns true if there are enough remaining bytes to read a single float.
        Returns:
        true if at least float remains
      • hasRemainingDouble

        public boolean hasRemainingDouble()
        Returns true if there are enough remaining bytes to read a single double.
        Returns:
        true if at least double remains
      • hasRemaining

        public boolean hasRemaining​(long pNumberOfBytes)
        Returns true if there are pNumberOfBytes remaining bytes to read.
        Parameters:
        pNumberOfBytes - number of bytes that would remain
        Returns:
        true if pNumberOfBytes remain in the buffer to read.
      • writeContiguousBuffer

        public void writeContiguousBuffer​(ContiguousBuffer pContiguousBuffer)
        Writes the entire contents of a ContiguousBuffer into this buffer. The position for _both buffers- is incremented accordingly.
        Parameters:
        pContiguousBuffer - buffer
      • writeContiguousMemory

        public void writeContiguousMemory​(ContiguousMemoryInterface pContiguousMemoryInterface)
        Writes the entire contents of a ContiguousMemoryInterface into this buffer. The position is incremented accordingly.
        Parameters:
        pContiguousMemoryInterface - memory
      • fillBytes

        public void fillBytes​(byte pByte)
        Fills the remaining space in the buffer with a given byte.
        Parameters:
        pByte - byte value.
      • writeBytes

        public void writeBytes​(long pNumberOfBytes,
                               byte pByte)
        Write a sequence of identical bytes into this buffer. The position is incremented accordingly.
        Parameters:
        pNumberOfBytes - number of bytes to write
        pByte - byte to write repeatedly.
      • writeByte

        public void writeByte​(byte pByte)
        Write a single byte. The position is incremented accordingly.
        Parameters:
        pByte - value
      • writeShort

        public void writeShort​(short pShort)
        Write a single short. The position is incremented accordingly.
        Parameters:
        pShort - value
      • writeChar

        public void writeChar​(char pChar)
        Write a single char. The position is incremented accordingly.
        Parameters:
        pChar - value
      • writeInt

        public void writeInt​(int pInt)
        Write a single int. The position is incremented accordingly.
        Parameters:
        pInt - value
      • writeLong

        public void writeLong​(long pLong)
        Write a single long. The position is incremented accordingly.
        Parameters:
        pLong - value
      • writeFloat

        public void writeFloat​(float pFloat)
        Write a single float. The position is incremented accordingly.
        Parameters:
        pFloat - value
      • writeDouble

        public void writeDouble​(double pDouble)
        Write a single double. The position is incremented accordingly.
        Parameters:
        pDouble - value
      • readByte

        public byte readByte()
        Reads a single byte. The position is incremented accordingly.
        Returns:
        value
      • readShort

        public short readShort()
        Reads a single short. The position is incremented accordingly.
        Returns:
        value
      • readChar

        public char readChar()
        Reads a single char. The position is incremented accordingly.
        Returns:
        value
      • readInt

        public int readInt()
        Reads a single int. The position is incremented accordingly.
        Returns:
        value
      • readLong

        public long readLong()
        Reads a single long. The position is incremented accordingly.
        Returns:
        value
      • readFloat

        public float readFloat()
        Reads a single float. The position is incremented accordingly.
        Returns:
        value
      • readDouble

        public double readDouble()
        Reads a single double. The position is incremented accordingly.
        Returns:
        value
      • skipBytes

        public void skipBytes​(long pNumberOfBytesToSkip)
        Skips multiple bytes. The position is incremented accordingly.
        Parameters:
        pNumberOfBytesToSkip - number of bytes to skip
      • skipShorts

        public void skipShorts​(long pNumberOfShortsToSkip)
        Skips multiple shorts. The position is incremented accordingly.
        Parameters:
        pNumberOfShortsToSkip - number of shorts to skip
      • skipChars

        public void skipChars​(long pNumberOfCharsToSkip)
        Skips multiple chars. The position is incremented accordingly.
        Parameters:
        pNumberOfCharsToSkip - number of chars to skip
      • skipInts

        public void skipInts​(long pNumberofIntsToSkip)
        Skips multiple ints. The position is incremented accordingly.
        Parameters:
        pNumberofIntsToSkip - number of ints to skip
      • skipLongs

        public void skipLongs​(long pNumberOfLongsToSkip)
        Skips multiple longs. The position is incremented accordingly.
        Parameters:
        pNumberOfLongsToSkip - number of longs to skip
      • skipFloats

        public void skipFloats​(long pNumberOfFloatsToSkip)
        Skips multiple floats. The position is incremented accordingly.
        Parameters:
        pNumberOfFloatsToSkip - number of floats to skip
      • skipDoubles

        public void skipDoubles​(long pNumberOfDoublesToSkip)
        Skips multiple doubles. The position is incremented accordingly.
        Parameters:
        pNumberOfDoublesToSkip - number of doubles to skip