Package loci.common

Class NIOByteBufferProvider


  • public class NIOByteBufferProvider
    extends Object
    Provides a facade to byte buffer allocation that enables FileChannel.map() usage on platforms where it's unlikely to give us problems and heap allocation where it is. References:
    • http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5092131
    • http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6417205
    Author:
    Chris Allan (callan at blackcat dot ca)
    • Field Detail

      • MINIMUM_JAVA_VERSION

        public static final int MINIMUM_JAVA_VERSION
        The minimum Java version we know is safe for memory mapped I/O.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NIOByteBufferProvider

        public NIOByteBufferProvider​(FileChannel channel,
                                     FileChannel.MapMode mapMode)
        Default constructor.
        Parameters:
        channel - File channel to allocate or map byte buffers from.
        mapMode - The map mode. Required but only used if memory mapped I/O is to occur.
    • Method Detail

      • allocate

        public ByteBuffer allocate​(long bufferStartPosition,
                                   int newSize)
                            throws IOException
        Allocates or maps the desired file data into memory.
        Parameters:
        bufferStartPosition - The absolute position of the start of the buffer.
        newSize - The buffer size.
        Returns:
        A newly allocated or mapped NIO byte buffer.
        Throws:
        IOException - If there is an issue mapping, aligning or allocating the buffer.
      • allocateDirect

        protected ByteBuffer allocateDirect​(long bufferStartPosition,
                                            int newSize)
                                     throws IOException
        Allocates memory and copies the desired file data into it.
        Parameters:
        bufferStartPosition - The absolute position of the start of the buffer.
        newSize - The buffer size.
        Returns:
        A newly allocated NIO byte buffer.
        Throws:
        IOException - If there is an issue aligning or allocating the buffer.
      • allocateMappedByteBuffer

        protected ByteBuffer allocateMappedByteBuffer​(long bufferStartPosition,
                                                      int newSize)
                                               throws IOException
        Memory maps the desired file data into memory.
        Parameters:
        bufferStartPosition - The absolute position of the start of the buffer.
        newSize - The buffer size.
        Returns:
        A newly mapped NIO byte buffer.
        Throws:
        IOException - If there is an issue mapping, aligning or allocating the buffer.