Package org.fairdatapipeline.file
Class CleanableFileChannel
java.lang.Object
org.fairdatapipeline.file.CleanableFileChannel
- All Implemented Interfaces:
Closeable,AutoCloseable,ByteChannel,Channel,GatheringByteChannel,ReadableByteChannel,ScatteringByteChannel,SeekableByteChannel,WritableByteChannel
public class CleanableFileChannel
extends Object
implements AutoCloseable, SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel
Wrapper around standard FileChannel with additional safety net for closing if user does not close
and ability to run custom function during cleanup/closing. Users should either use with
try-with-resources or ensure they manually call .close() once finished. As a safety net, .close()
will be called by a cleaner if and when the instance of CleanableFileChannel is going to be
collected by GC.
-
Constructor Summary
ConstructorsConstructorDescriptionCleanableFileChannel(FileChannel fileChannel, Runnable runOnClose) Create a filechannel with a runnable closer to perform the close action. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidforce(boolean metaData) booleanisOpen()lock(long position, long size, boolean shared) map(FileChannel.MapMode mode, long position, long size) longposition()position(long newPosition) intread(ByteBuffer dst) longread(ByteBuffer[] dsts) longread(ByteBuffer[] dsts, int offset, int length) intread(ByteBuffer dst, long position) longsize()longtransferFrom(ReadableByteChannel src, long position, long count) longtransferTo(long position, long count, WritableByteChannel target) truncate(long size) tryLock(long position, long size, boolean shared) intwrite(ByteBuffer src) longwrite(ByteBuffer[] srcs) longwrite(ByteBuffer[] srcs, int offset, int length) intwrite(ByteBuffer src, long position)
-
Constructor Details
-
CleanableFileChannel
Create a filechannel with a runnable closer to perform the close action.- Parameters:
fileChannel- the fileChannel to wrap around.runOnClose- the runnable to run upon close.
-
-
Method Details
-
read
- Specified by:
readin interfaceReadableByteChannel- Specified by:
readin interfaceSeekableByteChannel- Throws:
IOException
-
read
- Specified by:
readin interfaceScatteringByteChannel- Throws:
IOException
-
read
- Specified by:
readin interfaceScatteringByteChannel- Throws:
IOException
-
write
- Specified by:
writein interfaceSeekableByteChannel- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-
write
- Specified by:
writein interfaceGatheringByteChannel- Throws:
IOException
-
write
- Specified by:
writein interfaceGatheringByteChannel- Throws:
IOException
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException
-
size
- Specified by:
sizein interfaceSeekableByteChannel- Throws:
IOException
-
truncate
- Specified by:
truncatein interfaceSeekableByteChannel- Throws:
IOException
-
force
- Throws:
IOException
-
transferTo
- Throws:
IOException
-
transferFrom
- Throws:
IOException
-
read
- Throws:
IOException
-
write
- Throws:
IOException
-
map
- Throws:
IOException
-
lock
- Throws:
IOException
-
tryLock
- Throws:
IOException
-
isOpen
public boolean isOpen() -
close
public void close()
-