Class FileStorage
FileStorage contains the list of files in a torrent and maps logical pieces
to physical file locations on disk. It's used both for:
- Inspecting torrents: Querying file names, sizes, and piece mapping
- Creating torrents: Adding files and setting piece size
- Modifying torrents: Renaming files or remapping pieces
Understanding Pieces and Files:
In BitTorrent, content is divided into fixed-size "pieces" (typically 16 KB to 16 MB each).
A piece might span across multiple files, and a file might contain data from multiple pieces.
FileStorage manages this mapping:
File1 (1 MB) | File2 (2 MB) | File3 (500 KB) Piece0 [====] | [===Piece1===] | [=Piece2====]
Reading Torrent Files:
TorrentInfo ti = new TorrentInfo(new File("download.torrent"));
FileStorage fs = ti.files();
long totalSize = fs.totalSize();
int numFiles = fs.numFiles();
int pieceLength = fs.pieceLength();
int numPieces = fs.numPieces();
// Iterate through files
for (int i = 0; i < numFiles; i++) {
String fileName = fs.fileName(i);
long fileSize = fs.fileSize(i);
int flags = fs.fileFlags(i);
System.out.println(fileName + " (" + fileSize + " bytes)");
}
Creating Torrents:
FileStorage fs = new FileStorage();
fs.setPieceLength(262144); // 256 KB pieces
fs.addFile("video.mp4", 1000000000L);
fs.addFile("readme.txt", 5000);
// Use this storage to create a torrent
TorrentBuilder tb = new TorrentBuilder(fs, ...);
File Path Conventions: Multi-file torrents have all files under a common root directory (the torrent name). Single-file torrents have no root directory - the path is the file name itself.
// Multi-file torrent
fs.addFile("MyTorrent/file1.txt", 1000);
fs.addFile("MyTorrent/subdir/file2.txt", 2000);
// Single-file torrent
fs.addFile("myfile.iso", 1000000);
File Operations:
FileStorage fs = ti.files();
// Get files that contain a specific piece
FileSlice slice = fs.mapBlock(pieceIndex, 0, pieceLength);
System.out.println("Piece in file: " + slice.fileIndex() + ", offset: " + slice.offset());
// Rename a file (creates a copy for modification)
TorrentInfo ti2 = new TorrentInfo(ti.swig());
ti2.renameFile(0, "new-name.txt");
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.frostwire.jlibtorrent.swig.file_flags_tThis file is executable (sets the executable bit on posix like systems).static final com.frostwire.jlibtorrent.swig.file_flags_tThis file is hidden (sets the hidden attribute on windows).static final com.frostwire.jlibtorrent.swig.file_flags_tThis file is a pad file.static final com.frostwire.jlibtorrent.swig.file_flags_tThis file is a symlink. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a file to the file storage.voidAdds a file to the file storage.voidAdds a file to the file storage.voidaddFile(String path, long size, com.frostwire.jlibtorrent.swig.file_flags_t flags, int mtime, String symlink) Adds a file to the file storage.booleanfileAbsolutePath(int index) Returns true if the file at the specified index has been renamed to have an absolute path, i.e.com.frostwire.jlibtorrent.swig.file_flags_tfileFlags(int index) Returns a bitmask of flags fromfile_flags_tthat apply to file atindex.intfileIndexAtOffset(long offset) Returns the index of the file at the given offset in the torrent.fileName(int index) Returns only the name of the file, whereas returns the path (inside the torrent file) with the filename appended.longfileOffset(int index) returns the byte offset within the torrent file where this file starts.filePath(int index) Returns the full path to a file.returns the full path to a file.longfileSize(int index) returns the size of a file.hash(int index) Is a sha-1 hash of the file, or 0 if none was provided in the torrent file.booleanisValid()Returns true if the piece length has been initialized on the file_storage.mapBlock(int piece, long offset, int size) Returns a list ofFileSliceobjects representing the portions of files the specified piece index, byte offset and size range overlaps.mapFile(int file, long offset, int size) Returns aPeerRequestrepresenting the piece index, byte offset and size the specified file range overlaps.name()Get the name of this torrent.voidSet the name of this torrent.intnumFiles()Returns the number of files in the file_storage.intReturns the number of pieces in the torrent.voidnumPieces(int n) Set the number of pieces in the torrent.booleanpadFileAt(int index) returns true if the file at the given index is a pad-file.paths()intGet the size of each piece in this torrent.voidpieceLength(int l) Set the size of each piece in this torrent.intpieceSize(int index) Returns the piece size ofindex.voidrenameFile(int index, String newFilename) Renames the file atindextonewFilename.voidreserve(int numFiles) Allocates space fornumFilesin the internal file list.com.frostwire.jlibtorrent.swig.file_storageswig()com.frostwire.jlibtorrent.swig.torrent_infoti()This methods returns the internal torrent info or null if it was constructed without one.longReturns the total number of bytes all the files in this torrent spans.
-
Field Details
-
FLAG_PAD_FILE
public static final com.frostwire.jlibtorrent.swig.file_flags_t FLAG_PAD_FILEThis file is a pad file. The creator of the torrent promises the file is entirely filled with zeroes and does not need to be downloaded. The purpose is just to align the next file to either a block or piece boundary. -
FLAG_HIDDEN
public static final com.frostwire.jlibtorrent.swig.file_flags_t FLAG_HIDDENThis file is hidden (sets the hidden attribute on windows). -
FLAG_EXECUTABLE
public static final com.frostwire.jlibtorrent.swig.file_flags_t FLAG_EXECUTABLEThis file is executable (sets the executable bit on posix like systems). -
FLAG_SYMLINK
public static final com.frostwire.jlibtorrent.swig.file_flags_t FLAG_SYMLINKThis file is a symlink. The symlink target is specified in a separate field
-
-
Constructor Details
-
FileStorage
public FileStorage(com.frostwire.jlibtorrent.swig.file_storage fs) - Parameters:
fs- the native object
-
-
Method Details
-
swig
public com.frostwire.jlibtorrent.swig.file_storage swig()- Returns:
- the native object
-
ti
public com.frostwire.jlibtorrent.swig.torrent_info ti()This methods returns the internal torrent info or null if it was constructed without one.This also prevent premature garbage collection in case the storage was created from a torrent info.
- Returns:
- the pinned torrent info
-
isValid
public boolean isValid()Returns true if the piece length has been initialized on the file_storage. This is typically taken as a proxy of whether the file_storage as a whole is initialized or not.- Returns:
- true if valid
-
reserve
public void reserve(int numFiles) Allocates space fornumFilesin the internal file list. This can be used to avoid reallocating the internal file list when the number of files to be added is known up-front.- Parameters:
numFiles- the number of files
-
addFile
public void addFile(String path, long size, com.frostwire.jlibtorrent.swig.file_flags_t flags, int mtime, String symlink) Adds a file to the file storage. Theflagsargument sets attributes on the file. The file attributes is an extension and may not work in all bittorrent clients.If more files than one are added, certain restrictions to their paths apply. In a multi-file file storage (torrent), all files must share the same root directory.
That is, the first path element of all files must be the same. This shared path element is also set to the name of the torrent. It can be changed by calling .
The built in functions to traverse a directory to add files will make sure this requirement is fulfilled.
- Parameters:
path- the pathsize- the file sizeflags- the file flagsmtime- the timesymlink- the symlink
-
addFile
public void addFile(String path, long size, com.frostwire.jlibtorrent.swig.file_flags_t flags, int mtime) Adds a file to the file storage. Theflagsargument sets attributes on the file. The file attributes is an extension and may not work in all bittorrent clients.If more files than one are added, certain restrictions to their paths apply. In a multi-file file storage (torrent), all files must share the same root directory.
That is, the first path element of all files must be the same. This shared path element is also set to the name of the torrent. It can be changed by calling .
The built in functions to traverse a directory to add files will make sure this requirement is fulfilled.
- Parameters:
path- the pathsize- the file sizeflags- the file flagsmtime- the time
-
addFile
Adds a file to the file storage. Theflagsargument sets attributes on the file. The file attributes is an extension and may not work in all bittorrent clients.If more files than one are added, certain restrictions to their paths apply. In a multi-file file storage (torrent), all files must share the same root directory.
That is, the first path element of all files must be the same. This shared path element is also set to the name of the torrent. It can be changed by calling .
The built in functions to traverse a directory to add files will make sure this requirement is fulfilled.
- Parameters:
path- the pathsize- the file sizeflags- the file flags
-
addFile
Adds a file to the file storage.If more files than one are added, certain restrictions to their paths apply. In a multi-file file storage (torrent), all files must share the same root directory.
That is, the first path element of all files must be the same. This shared path element is also set to the name of the torrent. It can be changed by calling .
The built in functions to traverse a directory to add files will make sure this requirement is fulfilled.
- Parameters:
p-size-
-
renameFile
Renames the file atindextonewFilename. Keep in mind that filenames are expected to be UTF-8 encoded.- Parameters:
index-newFilename-
-
mapBlock
-
mapFile
Returns aPeerRequestrepresenting the piece index, byte offset and size the specified file range overlaps. This is the inverse mapping of .Note that the return type is meant to hold bittorrent block requests, which may not be larger than 16 kiB. Mapping a range larger than that may return an overflown integer.
- Parameters:
file-offset-size-- Returns:
-
numFiles
public int numFiles()Returns the number of files in the file_storage.- Returns:
-
totalSize
public long totalSize()Returns the total number of bytes all the files in this torrent spans.- Returns:
-
numPieces
public int numPieces()Returns the number of pieces in the torrent.- Returns:
- the number of pieces in the torrent
-
numPieces
public void numPieces(int n) Set the number of pieces in the torrent.- Parameters:
n-
-
pieceLength
public int pieceLength()Get the size of each piece in this torrent. This size is typically an even power of 2. It doesn't have to be though. It should be divisible by 16kiB however.- Returns:
-
pieceLength
public void pieceLength(int l) Set the size of each piece in this torrent. This size is typically an even power of 2. It doesn't have to be though. It should be divisible by 16kiB however.- Parameters:
l-
-
pieceSize
public int pieceSize(int index) Returns the piece size ofindex. This will be the same as , except for the last piece, which may be shorter.- Parameters:
index-- Returns:
-
name
Get the name of this torrent. For multi-file torrents, this is also the name of the root directory all the files are stored in.- Returns:
-
name
Set the name of this torrent. For multi-file torrents, this is also the name of the root directory all the files are stored in.- Parameters:
name-
-
hash
Is a sha-1 hash of the file, or 0 if none was provided in the torrent file. This can potentially be used to join a bittorrent network with other file sharing networks.- Parameters:
index-- Returns:
-
filePath
-
filePath
Returns the full path to a file.- Parameters:
index- the file index- Returns:
- the full path
-
fileName
Returns only the name of the file, whereas returns the path (inside the torrent file) with the filename appended.- Parameters:
index- the file index- Returns:
- the file name
-
fileSize
public long fileSize(int index) returns the size of a file.- Parameters:
index-- Returns:
-
padFileAt
public boolean padFileAt(int index) returns true if the file at the given index is a pad-file.- Parameters:
index-- Returns:
-
fileOffset
public long fileOffset(int index) returns the byte offset within the torrent file where this file starts. It can be used to map the file to a piece index (given the piece size).- Parameters:
index-- Returns:
-
paths
-
fileFlags
public com.frostwire.jlibtorrent.swig.file_flags_t fileFlags(int index) Returns a bitmask of flags fromfile_flags_tthat apply to file atindex.- Parameters:
index-- Returns:
- the flags
-
fileAbsolutePath
public boolean fileAbsolutePath(int index) Returns true if the file at the specified index has been renamed to have an absolute path, i.e. is not anchored in the save path of the torrent.- Parameters:
index-- Returns:
-
fileIndexAtOffset
public int fileIndexAtOffset(long offset) Returns the index of the file at the given offset in the torrent.- Parameters:
offset-- Returns:
-