Package db.buffers

Class DataBuffer

java.lang.Object
db.buffers.DataBuffer
All Implemented Interfaces:
Buffer, Externalizable, Serializable

public class DataBuffer extends Object implements Buffer, Externalizable
DataBuffer provides an accessible binary buffer for use with a BufferMgr and BufferFile.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    protected byte[]
     
    static final long
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Constructor for de-serialization
    protected
    DataBuffer(byte[] data)
    Construct a data buffer.
    protected
    DataBuffer(int bufsize)
    Construct a data buffer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Sets all the values in the buffer to 0;
    void
    copy(int offset, DataBuffer buf, int bufOffset, int length)
    Copy data from another buffer into this buffer.
    static void
     
    void
    get(int offset, byte[] bytes)
    Get the byte data located at the specified offset and store into the bytes array provided.
    void
    get(int offset, byte[] bytes, int dataOffset, int length)
    Get the byte data located at the specified offset and store into the data array at the specified data offset.
    byte[]
    get(int offset, int length)
    Get the byte data located at the specified offset.
    byte
    getByte(int offset)
    Get the 8-bit byte value located at the specified offset.
    protected byte[]
    Get the storage array associated with this buffer.
    int
    Get the ID associated with this buffer.
    int
    getInt(int offset)
    Get the 32-bit integer value located at the specified offset.
    long
    getLong(int offset)
    Get the 64-bit long value located at the specified offset.
    short
    getShort(int offset)
    Get the 16-bit short value located at the specified offset.
    boolean
    Return true if this buffer contains modified data.
    boolean
    Return true if this buffer is empty/unused.
    int
    Get the length of the buffer in bytes.
    void
    move(int src, int dest, int length)
    Move the data within this buffer.
    int
    put(int offset, byte[] bytes)
    Put the bytes provided into the buffer at the specified offset.
    int
    put(int offset, byte[] bytes, int dataOffset, int length)
    Put a specified number of bytes from the array provided into the buffer at the specified offset.
    int
    putByte(int offset, byte b)
    Put the 8-bit byte value into the buffer at the specified offset.
    int
    putInt(int offset, int v)
    Put the 32-bit integer value into the buffer at the specified offset.
    int
    putLong(int offset, long v)
    Put the 64-bit long value into the buffer at the specified offset.
    int
    putShort(int offset, short v)
    Put the 16-bit short value into the buffer at the specified offset.
    void
     
    protected void
    setData(byte[] data)
    Get the storage array associated with this buffer.
    protected void
    setDirty(boolean state)
    Set the dirty flag.
    protected void
    setEmpty(boolean state)
    Set the empty flag.
    protected void
    setId(int id)
    Set the ID associated with this buffer.
    int
    unsignedCompareTo(byte[] otherData, int offset, int len)
    Perform an unsigned data comparison
    static boolean
     
    void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • serialVersionUID

      public static final long serialVersionUID
      See Also:
    • COMPRESSED_SERIAL_OUTPUT_PROPERTY

      public static final String COMPRESSED_SERIAL_OUTPUT_PROPERTY
      See Also:
    • data

      protected byte[] data
  • Constructor Details

    • DataBuffer

      public DataBuffer()
      Constructor for de-serialization
    • DataBuffer

      protected DataBuffer(int bufsize)
      Construct a data buffer. A new binary buffer is created.
      Parameters:
      bufsize - buffer size
    • DataBuffer

      protected DataBuffer(byte[] data)
      Construct a data buffer.
      Parameters:
      data - binary storage array for this buffer.
  • Method Details

    • enableCompressedSerializationOutput

      public static void enableCompressedSerializationOutput(boolean enable)
    • usingCompressedSerializationOutput

      public static boolean usingCompressedSerializationOutput()
    • getData

      protected byte[] getData()
      Get the storage array associated with this buffer.
      Returns:
      byte storage array.
    • setData

      protected void setData(byte[] data)
      Get the storage array associated with this buffer.
    • getId

      public int getId()
      Get the ID associated with this buffer.
      Specified by:
      getId in interface Buffer
      Returns:
      buffer ID.
    • setId

      protected void setId(int id)
      Set the ID associated with this buffer.
      Parameters:
      id - buffer ID
    • isDirty

      public boolean isDirty()
      Return true if this buffer contains modified data. When this buffer is released to the BufferMgr, the data is consumed and this flag reset to false.
    • setDirty

      protected void setDirty(boolean state)
      Set the dirty flag.
      Parameters:
      state - flag state.
    • isEmpty

      public boolean isEmpty()
      Return true if this buffer is empty/unused. Writing to empty buffer does not change the state of this flag.
    • setEmpty

      protected void setEmpty(boolean state)
      Set the empty flag.
      Parameters:
      state - flag state
    • length

      public int length()
      Description copied from interface: Buffer
      Get the length of the buffer in bytes. The length reflects the number of bytes which have been allocated to the buffer.
      Specified by:
      length in interface Buffer
      Returns:
      length of allocated buffer.
    • get

      public void get(int offset, byte[] bytes, int dataOffset, int length) throws IndexOutOfBoundsException
      Description copied from interface: Buffer
      Get the byte data located at the specified offset and store into the data array at the specified data offset.
      Specified by:
      get in interface Buffer
      Parameters:
      offset - byte offset from the start of the buffer.
      bytes - byte array to store the data.
      dataOffset - offset into the data buffer
      length - amount of data to read
      Throws:
      IndexOutOfBoundsException - if an invalid offset, dataOffset, or length is specified.
    • get

      public void get(int offset, byte[] bytes)
      Description copied from interface: Buffer
      Get the byte data located at the specified offset and store into the bytes array provided.
      Specified by:
      get in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      bytes - byte array to store data
    • get

      public byte[] get(int offset, int length) throws IndexOutOfBoundsException
      Description copied from interface: Buffer
      Get the byte data located at the specified offset.
      Specified by:
      get in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      length - number of bytes to be read and returned
      Returns:
      the byte array.
      Throws:
      IndexOutOfBoundsException - is thrown if an invalid offset is specified or the end of the buffer was encountered while reading the data.
    • getByte

      public byte getByte(int offset)
      Description copied from interface: Buffer
      Get the 8-bit byte value located at the specified offset.
      Specified by:
      getByte in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      Returns:
      the byte value at the specified offset.
    • getInt

      public int getInt(int offset)
      Description copied from interface: Buffer
      Get the 32-bit integer value located at the specified offset.
      Specified by:
      getInt in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      Returns:
      the integer value at the specified offset.
    • getShort

      public short getShort(int offset)
      Description copied from interface: Buffer
      Get the 16-bit short value located at the specified offset.
      Specified by:
      getShort in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      Returns:
      the short value at the specified offset.
    • getLong

      public long getLong(int offset)
      Description copied from interface: Buffer
      Get the 64-bit long value located at the specified offset.
      Specified by:
      getLong in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      Returns:
      the long value at the specified offset.
    • put

      public int put(int offset, byte[] bytes, int dataOffset, int length)
      Description copied from interface: Buffer
      Put a specified number of bytes from the array provided into the buffer at the specified offset. The number of bytes stored is specified by the length specified.
      Specified by:
      put in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      bytes - the byte data to be stored.
      dataOffset - the starting offset into the data.
      length - the number of bytes to be stored.
      Returns:
      the next available offset into the buffer, or -1 if the buffer is full.
    • put

      public int put(int offset, byte[] bytes)
      Description copied from interface: Buffer
      Put the bytes provided into the buffer at the specified offset. The number of bytes stored is determined by the length of the bytes array.
      Specified by:
      put in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      bytes - the byte data to be stored.
      Returns:
      the next available offset into the buffer, or -1 if the buffer is full.
    • putByte

      public int putByte(int offset, byte b)
      Description copied from interface: Buffer
      Put the 8-bit byte value into the buffer at the specified offset.
      Specified by:
      putByte in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      b - the byte value to be stored.
      Returns:
      the next available offset into the buffer, or -1 if the buffer is full.
    • putInt

      public int putInt(int offset, int v)
      Description copied from interface: Buffer
      Put the 32-bit integer value into the buffer at the specified offset.
      Specified by:
      putInt in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      v - the integer value to be stored.
      Returns:
      the next available offset into the buffer, or -1 if the buffer is full.
    • putShort

      public int putShort(int offset, short v)
      Description copied from interface: Buffer
      Put the 16-bit short value into the buffer at the specified offset.
      Specified by:
      putShort in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      v - the short value to be stored.
      Returns:
      the next available offset into the buffer, or -1 if the buffer is full.
    • putLong

      public int putLong(int offset, long v)
      Description copied from interface: Buffer
      Put the 64-bit long value into the buffer at the specified offset.
      Specified by:
      putLong in interface Buffer
      Parameters:
      offset - byte offset from start of buffer.
      v - the long value to be stored.
      Returns:
      the next available offset into the buffer, or -1 if the buffer is full.
    • clear

      public void clear()
      Sets all the values in the buffer to 0;
    • move

      public void move(int src, int dest, int length)
      Move the data within this buffer.
      Parameters:
      src - source offset within this buffer
      dest - destination offset within this buffer
      length - length of data to be moved
      Throws:
      IndexOutOfBoundsException - is thrown if parameters result in data access beyond the buffer size.
    • copy

      public void copy(int offset, DataBuffer buf, int bufOffset, int length)
      Copy data from another buffer into this buffer.
      Parameters:
      offset - offset within this buffer.
      buf - source buffer
      bufOffset - source buffer offset
      length - amount of data to copy.
      Throws:
      IndexOutOfBoundsException - is thrown if parameters result in data access beyond the buffer size.
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException
    • unsignedCompareTo

      public int unsignedCompareTo(byte[] otherData, int offset, int len)
      Perform an unsigned data comparison
      Parameters:
      otherData - other data to be compared
      offset - offset within this buffer
      len - length of data within this buffer
      Returns:
      unsigned comparison result
      Throws:
      IndexOutOfBoundsException - if specified region is not contained within this buffer.