Class FileHandle
- All Implemented Interfaces:
Closeable,AutoCloseable,ByteChannel,Channel,ReadableByteChannel,SeekableByteChannel,WritableByteChannel
A representation of an open file. Designed to ensure predictable cleanup for open file descriptors.
This class is basically just a wrapper around a FileChannel that only exposes some of its methods.
It serves two purposes:
- It creates an extra layer of indirection between the FileChannel and application code, to allow for reachability-sensitive cleanup.
- It's a convenient place to add instrumentation and/or modified implementations when necessary.
The current implementation adds a post-close procedure for integration with caches/trackers, and stats for all operations.
Note that positional methods, e.g. position(), position(long), read(ByteBuffer), and
write(ByteBuffer) may require external synchronization if used concurrently by more than one thread.
-
Constructor Summary
ConstructorsConstructorDescriptionFileHandle(FileChannel fileChannel, com.fishlib.base.Procedure.Nullary postCloseProcedure) Wrap the suppliedFileChannel. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Close this file handle and release underlying resources.final voidforce()Force updates (including metadata) to the underlying file to be written to *local* storage.final booleanisOpen()Tells whether this file handle is open.final longposition()Get this file handle's position.final FileHandleposition(long newPosition) Advance the position of this file handle to the specified new position.final intread(ByteBuffer destination) Attempt to readdestination.remaining()bytes, beginning at the handle's current position and updating that position by the number of bytes read.final intread(ByteBuffer destination, long position) Attempt to readdestination.remaining()bytes, starting fromposition(0-indexed) in the file.final longsize()Get the current size of the file.final FileHandletruncate(long size) Truncate this file to the supplied size.final intwrite(ByteBuffer source) Attempt to writesource.remaining()bytes to this file handle, beginning at the handle's current position (which is first advanced to the end of the file, if the underlyingFileChannelwas opened withStandardOpenOption.APPEND), and updating that position by the number of bytes written.final intwrite(ByteBuffer source, long position) Attempt to writesource.remaining()bytes, starting fromposition(0-indexed) in the file.
-
Constructor Details
-
FileHandle
public FileHandle(@NotNull FileChannel fileChannel, @NotNull com.fishlib.base.Procedure.Nullary postCloseProcedure) Wrap the supplied
FileChannel.If the
postCloseProcedurethrows an exception, that exception may suppressClosedChannelExceptions that triggerpostCloseProcedureinvocation.- Parameters:
fileChannel- TheFileChannelpostCloseProcedure- A procedure to invoke if its detected that theFileChannelis closed - must be idempotent
-
-
Method Details
-
size
Get the current size of the file.
See
FileChannel.size().- Specified by:
sizein interfaceSeekableByteChannel- Returns:
- The current size of the file
- Throws:
IOException
-
position
Get this file handle's position.
- Specified by:
positionin interfaceSeekableByteChannel- Returns:
- This file handle's position
- Throws:
IOException
-
position
Advance the position of this file handle to the specified new position.
- Specified by:
positionin interfaceSeekableByteChannel- Parameters:
newPosition- The new position- Returns:
- This file handle
- Throws:
IOException
-
read
Attempt to read
destination.remaining()bytes, starting fromposition(0-indexed) in the file.- Parameters:
destination- The destination to read toposition- The position in the file to start reading from- Returns:
- The number of bytes read, or -1 if end of file is reached
- Throws:
IOException
-
read
Attempt to read
destination.remaining()bytes, beginning at the handle's current position and updating that position by the number of bytes read.- Specified by:
readin interfaceReadableByteChannel- Specified by:
readin interfaceSeekableByteChannel- Parameters:
destination- The destination to read to- Returns:
- The number of bytes read, or -1 of end of file is reached
- Throws:
IOException
-
write
Attempt to write
source.remaining()bytes, starting fromposition(0-indexed) in the file.- Parameters:
source- The source to write fromposition- The position in the file to start writing at- Returns:
- The number of bytes written
- Throws:
IOException
-
write
Attempt to write
source.remaining()bytes to this file handle, beginning at the handle's current position (which is first advanced to the end of the file, if the underlyingFileChannelwas opened withStandardOpenOption.APPEND), and updating that position by the number of bytes written.- Specified by:
writein interfaceSeekableByteChannel- Specified by:
writein interfaceWritableByteChannel- Parameters:
source- The source to write from- Returns:
- The number of bytes written
- Throws:
IOException
-
truncate
Truncate this file to the supplied size.
- Specified by:
truncatein interfaceSeekableByteChannel- Parameters:
size- The new size- Returns:
- This handle
- Throws:
IOException
-
force
Force updates (including metadata) to the underlying file to be written to *local* storage.
- Throws:
IOException
-
isOpen
public final boolean isOpen()Tells whether this file handle is open.
-
close
Close this file handle and release underlying resources.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Throws:
IOException
-