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
ConstructorDescriptionFileHandle
(@NotNull FileChannel fileChannel, @NotNull Runnable postCloseProcedure) Wrap the suppliedFileChannel
. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
close()
Close this file handle and release underlying resources.final void
force()
Force updates (including metadata) to the underlying file to be written to *local* storage.final boolean
isOpen()
Tells whether this file handle is open.final long
position()
Get this file handle's position.final FileHandle
position
(long newPosition) Advance the position of this file handle to the specified new position.final int
read
(@NotNull 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 int
read
(@NotNull ByteBuffer destination, long position) Attempt to readdestination.remaining()
bytes, starting fromposition
(0-indexed) in the file.final long
size()
Get the current size of the file.final FileHandle
truncate
(long size) Truncate this file to the supplied size.final int
write
(@NotNull 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 underlyingFileChannel
was opened withStandardOpenOption.APPEND
), and updating that position by the number of bytes written.final int
write
(@NotNull ByteBuffer source, long position) Attempt to writesource.remaining()
bytes, starting fromposition
(0-indexed) in the file.
-
Constructor Details
-
FileHandle
public FileHandle(@NotNull @NotNull FileChannel fileChannel, @NotNull @NotNull Runnable postCloseProcedure) Wrap the supplied
FileChannel
.If the
postCloseProcedure
throws an exception, that exception may suppressClosedChannelException
s that triggerpostCloseProcedure
invocation.- Parameters:
fileChannel
- TheFileChannel
postCloseProcedure
- A procedure to invoke if its detected that theFileChannel
is closed - must be idempotent
-
-
Method Details
-
size
Get the current size of the file.
See
FileChannel.size()
.- Specified by:
size
in interfaceSeekableByteChannel
- Returns:
- The current size of the file
- Throws:
IOException
-
position
Get this file handle's position.
- Specified by:
position
in interfaceSeekableByteChannel
- Returns:
- This file handle's position
- Throws:
IOException
-
position
Advance the position of this file handle to the specified new position.
- Specified by:
position
in 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:
read
in interfaceReadableByteChannel
- Specified by:
read
in 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 underlyingFileChannel
was opened withStandardOpenOption.APPEND
), and updating that position by the number of bytes written.- Specified by:
write
in interfaceSeekableByteChannel
- Specified by:
write
in 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:
truncate
in 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-