Package uk.ac.starlink.fits
Class BufferManager
java.lang.Object
uk.ac.starlink.fits.BufferManager
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 Summary
ConstructorsConstructorDescriptionBufferManager
(FileChannel channel, long offset, int leng, String logLabel, Unmapper unmapper) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Asserts that neither this manager, nor any of the buffers it has supplied, will be used again.Returns a new read-only ByteBuffer providing access to the file region.void
disposeBuffer
(ByteBuffer buf) Asserts that the supplied buffer, originally acquired fromcreateBuffer()
, will no longer be used.
-
Constructor Details
-
BufferManager
public BufferManager(FileChannel channel, long offset, int leng, 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 Details
-
createBuffer
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:
IOException
-
disposeBuffer
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.
-