Class Buffer


  • class Buffer
    extends java.lang.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).

    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void free()
      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 padToAlignment()
      Pad the buffer to the next alignment position.
      (package private) long pointer()
      The data pointer object for the native buffer.
      (package private) long pointer​(long offset, long expectedWrite)  
      (package private) int position()  
      (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 reset()
      Clear the bytes written.
      (package private) int size()  
      (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 Detail

      • PADDING

        static final byte[] PADDING
      • bufferSize

        final int bufferSize
      • buffer

        io.netty.buffer.ByteBuf buffer
      • byteBuffer

        java.nio.ByteBuffer byteBuffer
      • allocator

        io.netty.buffer.ByteBufAllocator allocator
      • pointer

        long pointer
    • Constructor Detail

      • Buffer

        Buffer​(NativeIO nativeIO,
               io.netty.buffer.ByteBufAllocator allocator,
               int bufferSize)
        throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • hasSpace

        boolean hasSpace​(int size)
                  throws java.io.IOException
        Returns:
        whether there is space in the buffer for size bytes.
        Throws:
        java.io.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 java.io.IOException
        Write an integer to buffer. Progresses the position of the buffer by 4 bytes.
        Throws:
        java.io.IOException
      • writeByteBuf

        void writeByteBuf​(io.netty.buffer.ByteBuf bytebuf)
                   throws java.io.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:
        java.io.IOException
      • readInt

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

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

        int readByteBuf​(io.netty.buffer.ByteBuf buffer,
                        int offset,
                        int size)
                 throws java.io.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:
        java.io.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)