java.lang.Object
org.klomp.snark.Storage.TorrentFile
All Implemented Interfaces:
Comparable<Storage.TorrentFile>
Enclosing class:
Storage

private class Storage.TorrentFileextends Object implements Comparable<Storage.TorrentFile>
A single file in a torrent.
  • Field Summary

    Modifier and Type
    Field
    Description
    final File
    The final data-directory location of the file.
    final boolean
    BEP 47 padding placeholder; hashes as zeros and is never read from or written to disk.
    boolean
    Whether the file is empty and sparse; locking: this.
    final long
    The file's length in bytes.
    boolean
    Whether the file lives in the data directory; set when the copy out of the staging dir completes.
    final String
    The file's path relative to the base directory.
    int
    Priority by file; default 0.
    Null when closed; locking: this.
    The active file: the staging-dir file while the file is incomplete, the data-dir file after it has been moved.
    private long
    When the RAF was last accessed, or 0 if closed; locking: this.
    final File
    The staging-dir location while the file is incomplete, null once the file lives in the data directory (or when staging is disabled).
  • Constructor Summary

    Constructors
    Constructor
    Description
    TorrentFile(File base, File f)
    For new metainfo from files; use base == f for single-file torrent.
    TorrentFile(File base, File f, long len)
    For existing metainfo with specified file length; use base == f for single-file torrent.
    TorrentFile(File base, File f, long len, boolean padding)
    For existing metainfo; a padding file is a synthetic BEP 47 zero-fill entry whose pieces hash as zeros.
    TorrentFile(File base, File active, File finalFile, File workFile, long len, boolean padding)
    Full constructor supporting the staging feature.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Extend the file to its full length so reads don't fail with an IOE, and record whether the file is still sparse.
    private void
    This "balloons" the file with zeros to eliminate disk fragmentation., Overwrites the entire file with zeros.
    This must be called before using the RAF to ensure it is open locking: this.
    checkRAF(boolean forceRW)
    Like checkRAF(), but force a read-write open.
    void
    Can be called even if not open locking: this.
    void
    closeRAF(long cutoff)
    Close if last used time older than cutoff.
    int
     
    boolean
    Whether this equals the other.
    int
    Whether hash code is present.
    private void
    Locking: this.
    private void
    openRAF(boolean readonly)
    Locking: this.
    private void
    openRAF(boolean readonly, boolean forceRW)
    Locking: this.
    String form of the storage.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • length

      public final long length
      The file's length in bytes.
    • name

      public final String name
      The file's path relative to the base directory.
    • RAFfile

      public File RAFfile
      The active file: the staging-dir file while the file is incomplete, the data-dir file after it has been moved. Never null. Not final: replaced by the final file when the move completes. Locking: this.
    • finalFile

      public final File finalFile
      The final data-directory location of the file. Constant even while the data is still being written to the staging dir, so paths shown in the UI and used for file matching never change. Locking: none.
    • workFile

      public final File workFile
      The staging-dir location while the file is incomplete, null once the file lives in the data directory (or when staging is disabled). Locking: none.
    • movedToDataDir

      public volatile boolean movedToDataDir
      Whether the file lives in the data directory; set when the copy out of the staging dir completes. Locking: this.
    • RAFtime

      private long RAFtime
      When the RAF was last accessed, or 0 if closed; locking: this.
    • raf

      private RandomAccessFile raf
      Null when closed; locking: this.
    • isSparse

      public boolean isSparse
      Whether the file is empty and sparse; locking: this.
    • isPadding

      public final boolean isPadding
      BEP 47 padding placeholder; hashes as zeros and is never read from or written to disk.
    • priority

      public volatile int priority
      Priority by file; default 0.
  • Constructor Details

    • TorrentFile

      public TorrentFile(File base, File f)
      For new metainfo from files; use base == f for single-file torrent.
      Parameters:
      base - the data dir, or the base file for a single-file torrent
      f - the file to track
    • TorrentFile

      public TorrentFile(File base, File f, long len)
      For existing metainfo with specified file length; use base == f for single-file torrent.
      Parameters:
      base - the data dir, or the base file for a single-file torrent
      f - the file to track
      len - the file length from the metainfo, in bytes
    • TorrentFile

      public TorrentFile(File base, File f, long len, boolean padding)
      For existing metainfo; a padding file is a synthetic BEP 47 zero-fill entry whose pieces hash as zeros. The runtime never creates, allocates, or writes it to disk.
      Parameters:
      base - the data dir, or the base file for a single-file torrent
      f - the file to track
      len - the file length from the metainfo, in bytes
      padding - true for a synthetic BEP 47 padding file
    • TorrentFile

      public TorrentFile(File base, File active, File finalFile, File workFile, long len, boolean padding)
      Full constructor supporting the staging feature.
      Parameters:
      base - the data dir (or the base file for a single-file torrent)
      active - the file to read and write now (staging or data dir)
      finalFile - the data-directory location
      workFile - the staging-dir location, or null when the file is already in the data directory
      len - the file's byte length, which maps pieces onto it
      padding - whether this is a BEP 47 padding placeholder
  • Method Details

    • checkRAF

      public RandomAccessFile checkRAF() throws IOException
      This must be called before using the RAF to ensure it is open locking: this.
      Returns:
      the open handle, null only if the open failed
      Throws:
      IOException - if the file cannot be opened
    • checkRAF

      public RandomAccessFile checkRAF(boolean forceRW) throws IOException
      Like checkRAF(), but force a read-write open. When the storage is thought complete the files are opened read-only, and any write through such a handle fails with EBADF on a perfectly healthy disk. The write path calls this to force a true RW handle, closing any existing read-only one first, and attempts to make the file writable if permissions deny it.
      Parameters:
      forceRW - true to force a read-write handle
      Returns:
      the read-write handle
      Throws:
      IOException - if the file cannot be opened read-write
    • openRAF

      private void openRAF() throws IOException
      Locking: this.
      Throws:
      IOException - if the file cannot be opened
    • openRAF

      private void openRAF(boolean readonly) throws IOException
      Locking: this.
      Parameters:
      readonly - true to open the file read-only
      Throws:
      IOException - if the file cannot be opened
    • openRAF

      private void openRAF(boolean readonly, boolean forceRW) throws IOException
      Locking: this.
      Parameters:
      readonly - true to prefer a read-only open
      forceRW - true to force a read-write open regardless of writability
      Throws:
      IOException - if the file cannot be opened
    • closeRAF

      public void closeRAF(long cutoff)
      Close if last used time older than cutoff. locking: this.
      Parameters:
      cutoff - the time to compare against, in ms since epoch
    • closeRAF

      public void closeRAF() throws IOException
      Can be called even if not open locking: this.
      Throws:
      IOException - if the handle cannot be closed
    • allocateFile

      public void allocateFile() throws IOException
      Extend the file to its full length so reads don't fail with an IOE, and record whether the file is still sparse.

      The file MUST already exist or this throws an IOE. A sparse file is defragged later by balloonFile(), when the caller that is checking the storage gets there.

      Opens the file read-write through checkRAF(true); the caller must synchronize and call closeRAF().

      Throws:
      IOException - if the file does not exist or cannot be extended
    • balloonFile

      private void balloonFile() throws IOException
      This "balloons" the file with zeros to eliminate disk fragmentation., Overwrites the entire file with zeros. Sets isSparse[nr] = false.

      Caller must synchronize and call checkRAF() or openRAF().

      Throws:
      IOException - if the file cannot be written
    • compareTo

      public int compareTo(Storage.TorrentFile tf)
      Specified by:
      compareTo in interface Comparable<Storage.TorrentFile>
    • hashCode

      public int hashCode()
      Whether hash code is present.

      Keyed on the final data-directory path, which is constant while the file is moved from staging to the data dir.

      Overrides:
      hashCode in class Object
      Returns:
      whether h code is present
    • equals

      public boolean equals(Object o)
      Whether this equals the other.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      String form of the storage.
      Overrides:
      toString in class Object