java.lang.Object
org.apache.bookkeeper.bookie.storage.directentrylogger.Buffer

class Buffer extends Object
A utility buffer class to be used with native calls.

Buffers are page aligned (4k pages).

The wrapper mostly handles writes between ByteBuffers and ByteBufs. It also provides a method for padding the buffer to the next alignment, so writes can have an aligned size also (as required by direct I/O). The padding is done with 0xF0, so that if it is read as an integer, or long, the value will be negative (assuming the read is a java read, and thus a signed int).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    (package private) io.netty.buffer.ByteBufAllocator
     
    (package private) io.netty.buffer.ByteBuf
     
    (package private) final int
     
    (package private) ByteBuffer
     
    (package private) final NativeIO
     
    (package private) static final byte[]
     
    static final byte
     
    (package private) long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Buffer(NativeIO nativeIO, io.netty.buffer.ByteBufAllocator allocator, int bufferSize)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Free the memory that backs this buffer.
    (package private) boolean
    hasData(int offset, int size)
     
    (package private) boolean
    hasSpace(int size)
     
    (package private) static boolean
    isAligned(long size)
     
    (package private) static int
    nextAlignment(int pos)
     
    (package private) int
    Pad the buffer to the next alignment position.
    (package private) long
    The data pointer object for the native buffer.
    (package private) long
    pointer(long offset, long expectedWrite)
     
    (package private) int
     
    (package private) int
    readByteBuf(io.netty.buffer.ByteBuf buffer, int offset, int size)
    Read a bytebuf of size from the buffer at the given offset.
    (package private) int
    readInt(int offset)
    Read an integer from the buffer at the given offset.
    (package private) long
    readLong(int offset)
    Read a long from the buffer at the given offset.
    (package private) void
    Clear the bytes written.
    (package private) int
     
    (package private) void
    writeByteBuf(io.netty.buffer.ByteBuf bytebuf)
    Write a btebuf to this buffer.
    (package private) void
    writeInt(int value)
    Write an integer to buffer.

    Methods inherited from class java.lang.Object

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

    • PADDING_BYTE

      public static final byte PADDING_BYTE
      See Also:
    • ALIGNMENT

      public static final int ALIGNMENT
      See Also:
    • PADDING

      static final byte[] PADDING
    • nativeIO

      final NativeIO nativeIO
    • bufferSize

      final int bufferSize
    • buffer

      io.netty.buffer.ByteBuf buffer
    • byteBuffer

      ByteBuffer byteBuffer
    • allocator

      io.netty.buffer.ByteBufAllocator allocator
    • pointer

      long pointer
  • Constructor Details

  • Method Details

    • hasSpace

      boolean hasSpace(int size) throws IOException
      Returns:
      whether there is space in the buffer for size bytes.
      Throws:
      IOException
    • hasData

      boolean hasData(int offset, int size)
      Returns:
      whether the buffer can honour a read of size at offset.
    • writeInt

      void writeInt(int value) throws IOException
      Write an integer to buffer. Progresses the position of the buffer by 4 bytes.
      Throws:
      IOException
    • writeByteBuf

      void writeByteBuf(io.netty.buffer.ByteBuf bytebuf) throws IOException
      Write a btebuf to this buffer. Progresses the position of the buffer by the number of readable bytes of the bytebuf. Progresses the readerIndex of the passed bytebuf by the number of bytes read (i.e. to the end).
      Throws:
      IOException
    • readInt

      int readInt(int offset) throws IOException
      Read an integer from the buffer at the given offset. The offset is in bytes.
      Throws:
      IOException
    • readLong

      long readLong(int offset) throws IOException
      Read a long from the buffer at the given offset. The offset is in bytes.
      Throws:
      IOException
    • readByteBuf

      int readByteBuf(io.netty.buffer.ByteBuf buffer, int offset, int size) throws IOException
      Read a bytebuf of size from the buffer at the given offset. If there are not enough bytes in the buffer to satisfy the read, some of the bytes are read into the byte buffer and the number of bytes read is returned.
      Throws:
      IOException
    • pointer

      long pointer()
      The data pointer object for the native buffer. This can be used by JNI method which take a char* or void*.
    • pointer

      long pointer(long offset, long expectedWrite)
    • position

      int position()
      Returns:
      the number of bytes which have been written to this buffer.
    • size

      int size()
      Returns:
      the size of the buffer (i.e. the max number of bytes writable, or the max offset readable)
    • padToAlignment

      int padToAlignment()
      Pad the buffer to the next alignment position.
      Returns:
      the position of the next alignment. This should be used as the size argument to make aligned writes.
    • reset

      void reset()
      Clear the bytes written. This doesn't actually destroy the data, but moves the position back to the start of the buffer.
    • free

      void free()
      Free the memory that backs this buffer.
    • isAligned

      static boolean isAligned(long size)
    • nextAlignment

      static int nextAlignment(int pos)