Package uk.ac.starlink.fits
Class BufferManager
- java.lang.Object
-
- uk.ac.starlink.fits.BufferManager
-
public class BufferManager extends java.lang.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 usingdisposeBuffer(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 itsdisposeBuffer(java.nio.ByteBuffer)
andclose()
methods, should be used with extreme caution, or a null Unmapper should be supplied.- Since:
- 12 May 2022
- Author:
- Mark Taylor
-
-
Constructor Summary
Constructors Constructor Description BufferManager(java.nio.channels.FileChannel channel, long offset, int leng, java.lang.String logLabel, Unmapper unmapper)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Asserts that neither this manager, nor any of the buffers it has supplied, will be used again.java.nio.ByteBuffer
createBuffer()
Returns a new read-only ByteBuffer providing access to the file region.void
disposeBuffer(java.nio.ByteBuffer buf)
Asserts that the supplied buffer, originally acquired fromcreateBuffer()
, will no longer be used.
-
-
-
Constructor Detail
-
BufferManager
public BufferManager(java.nio.channels.FileChannel channel, long offset, int leng, java.lang.String logLabel, Unmapper unmapper)
Constructor.- Parameters:
channel
- file channeloffset
- offset into file of start of mapped regionleng
- length of mapped regionlogLabel
- description of mapped region suitable for use in logging messagesunmapper
- used to unmap buffers, may be null for safety
-
-
Method Detail
-
createBuffer
public java.nio.ByteBuffer createBuffer() throws java.io.IOException
Returns a new read-only ByteBuffer providing access to the file region. The returned object should if possible be passed todisposeBuffer(java.nio.ByteBuffer)
after it is known that it will no longer be used.- Returns:
- buffer
- Throws:
java.io.IOException
-
disposeBuffer
public void disposeBuffer(java.nio.ByteBuffer buf)
Asserts that the supplied buffer, originally acquired fromcreateBuffer()
, 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.
-
-