Package org.fairdatapipeline.file
Class CleanableFileChannel
- java.lang.Object
-
- org.fairdatapipeline.file.CleanableFileChannel
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.ByteChannel,java.nio.channels.Channel,java.nio.channels.GatheringByteChannel,java.nio.channels.ReadableByteChannel,java.nio.channels.ScatteringByteChannel,java.nio.channels.SeekableByteChannel,java.nio.channels.WritableByteChannel
public class CleanableFileChannel extends java.lang.Object implements java.lang.AutoCloseable, java.nio.channels.SeekableByteChannel, java.nio.channels.GatheringByteChannel, java.nio.channels.ScatteringByteChannelWrapper 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
Constructors Constructor Description CleanableFileChannel(java.nio.channels.FileChannel fileChannel, java.lang.Runnable runOnClose)Create a filechannel with a runnable closer to perform the close action.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidforce(boolean metaData)booleanisOpen()java.nio.channels.FileLocklock(long position, long size, boolean shared)java.nio.MappedByteBuffermap(java.nio.channels.FileChannel.MapMode mode, long position, long size)longposition()CleanableFileChannelposition(long newPosition)intread(java.nio.ByteBuffer dst)longread(java.nio.ByteBuffer[] dsts)longread(java.nio.ByteBuffer[] dsts, int offset, int length)intread(java.nio.ByteBuffer dst, long position)longsize()longtransferFrom(java.nio.channels.ReadableByteChannel src, long position, long count)longtransferTo(long position, long count, java.nio.channels.WritableByteChannel target)CleanableFileChanneltruncate(long size)java.nio.channels.FileLocktryLock(long position, long size, boolean shared)intwrite(java.nio.ByteBuffer src)longwrite(java.nio.ByteBuffer[] srcs)longwrite(java.nio.ByteBuffer[] srcs, int offset, int length)intwrite(java.nio.ByteBuffer src, long position)
-
-
-
Constructor Detail
-
CleanableFileChannel
public CleanableFileChannel(java.nio.channels.FileChannel fileChannel, java.lang.Runnable runOnClose)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 Detail
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Specified by:
readin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
read
public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException- Specified by:
readin interfacejava.nio.channels.ScatteringByteChannel- Throws:
java.io.IOException
-
read
public long read(java.nio.ByteBuffer[] dsts) throws java.io.IOException- Specified by:
readin interfacejava.nio.channels.ScatteringByteChannel- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException- Specified by:
writein interfacejava.nio.channels.SeekableByteChannel- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Throws:
java.io.IOException
-
write
public long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException- Specified by:
writein interfacejava.nio.channels.GatheringByteChannel- Throws:
java.io.IOException
-
write
public long write(java.nio.ByteBuffer[] srcs) throws java.io.IOException- Specified by:
writein interfacejava.nio.channels.GatheringByteChannel- Throws:
java.io.IOException
-
position
public long position() throws java.io.IOException- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
position
public CleanableFileChannel position(long newPosition) throws java.io.IOException
- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
size
public long size() throws java.io.IOException- Specified by:
sizein interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
truncate
public CleanableFileChannel truncate(long size) throws java.io.IOException
- Specified by:
truncatein interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
force
public void force(boolean metaData) throws java.io.IOException- Throws:
java.io.IOException
-
transferTo
public long transferTo(long position, long count, java.nio.channels.WritableByteChannel target) throws java.io.IOException- Throws:
java.io.IOException
-
transferFrom
public long transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count) throws java.io.IOException- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst, long position) throws java.io.IOException- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src, long position) throws java.io.IOException- Throws:
java.io.IOException
-
map
public java.nio.MappedByteBuffer map(java.nio.channels.FileChannel.MapMode mode, long position, long size) throws java.io.IOException- Throws:
java.io.IOException
-
lock
public java.nio.channels.FileLock lock(long position, long size, boolean shared) throws java.io.IOException- Throws:
java.io.IOException
-
tryLock
public java.nio.channels.FileLock tryLock(long position, long size, boolean shared) throws java.io.IOException- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()
- Specified by:
isOpenin interfacejava.nio.channels.Channel
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable
-
-