Class FileHandle

All Implemented Interfaces:
Closeable, AutoCloseable, ByteChannel, Channel, ReadableByteChannel, SeekableByteChannel, WritableByteChannel

public final class FileHandle extends Object implements SeekableByteChannel

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:

  1. It creates an extra layer of indirection between the FileChannel and application code, to allow for reachability-sensitive cleanup.
  2. 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

    FileHandle(@NotNull FileChannel fileChannel, @NotNull Runnable postCloseProcedure)
    Wrap the supplied FileChannel.
  • Method Summary

    Modifier and Type
    final void
    Close this file handle and release underlying resources.
    final void
    Force updates (including metadata) to the underlying file to be written to *local* storage.
    final boolean
    Tells whether this file handle is open.
    final long
    Get this file handle's position.
    position(long newPosition)
    Advance the position of this file handle to the specified new position.
    final int
    read(@NotNull ByteBuffer destination)
    Attempt to read destination.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 read destination.remaining() bytes, starting from position (0-indexed) in the file.
    final long
    Get the current size of the file.
    truncate(long size)
    Truncate this file to the supplied size.
    final int
    write(@NotNull ByteBuffer source)
    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 underlying FileChannel was opened with StandardOpenOption.APPEND), and updating that position by the number of bytes written.
    final int
    write(@NotNull ByteBuffer source, long position)
    Attempt to write source.remaining() bytes, starting from position (0-indexed) in the file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait