Class FileHandle

java.lang.Object
com.illumon.iris.db.util.file.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

    Constructors 
    Constructor Description
    FileHandle​(FileChannel fileChannel, com.fishlib.base.Procedure.Nullary postCloseProcedure)
    Wrap the supplied FileChannel.
  • Method Summary

    Modifier and Type Method Description
    void close()
    Close this file handle and release underlying resources.
    void force()
    Force updates (including metadata) to the underlying file to be written to *local* storage.
    boolean isOpen()
    Tells whether this file handle is open.
    long position()
    Get this file handle's position.
    FileHandle position​(long newPosition)
    Advance the position of this file handle to the specified new position.
    int read​(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.
    int read​(ByteBuffer destination, long position)
    Attempt to read destination.remaining() bytes, starting from position (0-indexed) in the file.
    long size()
    Get the current size of the file.
    FileHandle truncate​(long size)
    Truncate this file to the supplied size.
    int write​(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.
    int write​(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