Class BufferManager

java.lang.Object
uk.ac.starlink.fits.BufferManager

public class BufferManager extends Object
Manages access to a region of a file using monolithic byte buffers. The region is only mapped once, and subsequent buffer requests are handled by duplicating the original mapped buffer.

Buffers are acquired using createBuffer(), and should be disposed of using disposeBuffer(java.nio.ByteBuffer) when no longer in use. When none of the buffers are in use any more, close() should be called.

Note: the mapped buffer is unmapped using the supplied Unmapper when all created buffers have been disposed, or when close() is called. If any of the created buffers is used after this has happened, and unmapping is actually attempted, very bad consequences may ensue. Therefore this class, or at least its disposeBuffer(java.nio.ByteBuffer) and close() methods, should be used with extreme caution, or a null Unmapper should be supplied.

Since:
12 May 2022
Author:
Mark Taylor
  • Constructor Details

    • BufferManager

      public BufferManager(FileChannel channel, long offset, int leng, String logLabel, Unmapper unmapper)
      Constructor.
      Parameters:
      channel - file channel
      offset - offset into file of start of mapped region
      leng - length of mapped region
      logLabel - description of mapped region suitable for use in logging messages
      unmapper - used to unmap buffers, may be null for safety
  • Method Details

    • createBuffer

      public ByteBuffer createBuffer() throws IOException
      Returns a new read-only ByteBuffer providing access to the file region. The returned object should if possible be passed to disposeBuffer(java.nio.ByteBuffer) after it is known that it will no longer be used.
      Returns:
      buffer
      Throws:
      IOException
    • disposeBuffer

      public void disposeBuffer(ByteBuffer buf)
      Asserts that the supplied buffer, originally acquired from createBuffer(), will no longer be used. Resources may be reclaimed.
      Parameters:
      buf - previously created buffer
    • close

      public void close()
      Asserts that neither this manager, nor any of the buffers it has supplied, will be used again.