java.lang.Object
org.klomp.snark.Storage.TorrentFile
- All Implemented Interfaces:
Comparable<Storage.TorrentFile>
- Enclosing class:
Storage
A single file in a torrent.
Field Summary
Modifier and TypeFieldDescriptionfinal FileThe final data-directory location of the file.final booleanBEP 47 padding placeholder; hashes as zeros and is never read from or written to disk.booleanWhether the file is empty and sparse; locking: this.final longThe file's length in bytes.booleanWhether the file lives in the data directory; set when the copy out of the staging dir completes.final StringThe file's path relative to the base directory.intPriority by file; default 0.private RandomAccessFileNull 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 longWhen the RAF was last accessed, or 0 if closed; locking: this.final FileThe staging-dir location while the file is incomplete, null once the file lives in the data directory (or when staging is disabled).Constructor Summary
ConstructorsConstructorDescriptionTorrentFile(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 TypeMethodDescriptionvoidExtend the file to its full length so reads don't fail with an IOE, and record whether the file is still sparse.private voidThis "balloons" the file with zeros to eliminate disk fragmentation., Overwrites the entire file with zeros.checkRAF()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.voidcloseRAF()Can be called even if not open locking: this.voidcloseRAF(long cutoff) Close if last used time older than cutoff.intbooleanWhether this equals the other.inthashCode()Whether hash code is present.private voidopenRAF()Locking: this.private voidopenRAF(boolean readonly) Locking: this.private voidopenRAF(boolean readonly, boolean forceRW) Locking: this.toString()String form of the storage.
Field Details
length
public final long lengthThe file's length in bytes.name
The file's path relative to the base directory.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
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
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 movedToDataDirWhether the file lives in the data directory; set when the copy out of the staging dir completes. Locking: this.RAFtime
private long RAFtimeWhen the RAF was last accessed, or 0 if closed; locking: this.raf
Null when closed; locking: this.isSparse
public boolean isSparseWhether the file is empty and sparse; locking: this.isPadding
public final boolean isPaddingBEP 47 padding placeholder; hashes as zeros and is never read from or written to disk.priority
public volatile int priorityPriority by file; default 0.
Constructor Details
TorrentFile
TorrentFile
TorrentFile
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 torrentf- the file to tracklen- the file length from the metainfo, in bytespadding- 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 locationworkFile- the staging-dir location, or null when the file is already in the data directorylen- the file's byte length, which maps pieces onto itpadding- whether this is a BEP 47 padding placeholder
Method Details
checkRAF
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
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
Locking: this.- Throws:
IOException- if the file cannot be opened
openRAF
Locking: this.- Parameters:
readonly- true to open the file read-only- Throws:
IOException- if the file cannot be opened
openRAF
Locking: this.- Parameters:
readonly- true to prefer a read-only openforceRW- 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
Can be called even if not open locking: this.- Throws:
IOException- if the handle cannot be closed
allocateFile
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
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
- Specified by:
compareToin interfaceComparable<Storage.TorrentFile>
hashCode
equals
toString