Class PiecesTracker

java.lang.Object
com.frostwire.jlibtorrent.PiecesTracker

public final class PiecesTracker extends Object
Tracks download completion status for files and pieces in a torrent.

PiecesTracker provides a convenient way to monitor which pieces are complete and calculate sequential download progress for individual files. It maps the relationship between files and pieces, allowing efficient queries about download status.

Understanding File-Piece Mapping:
In BitTorrent, files are divided into pieces (typically 256KB or 512KB each), and pieces may span multiple files. This tracker maintains:

  • File-Piece Map: Which pieces constitute each file
  • Piece Sizes: How many bytes of each piece belong to each file
  • Completion Status: Boolean array of piece completion

Creating a PiecesTracker:

 // Create from torrent metadata
 TorrentInfo ti = new TorrentInfo(torrentFile);
 PiecesTracker tracker = new PiecesTracker(ti);

 System.out.println(\"Files: \" + tracker.numFiles());
 System.out.println(\"Pieces: \" + tracker.numPieces());
 

Tracking Download Completion:

 PiecesTracker tracker = new PiecesTracker(ti);

 // Mark pieces as complete (typically done by alert listener)
 sm.addListener(new AlertListener() {
     public int[] types() {
         return new int[] {AlertType.PIECE_FINISHED.swig()};
     }

     public void alert(Alert<?> alert) {
         PieceFinishedAlert pfa = (PieceFinishedAlert) alert;
         int piece = pfa.pieceIndex();
         tracker.setComplete(piece, true);
     }
 });
 

Calculating Sequential Download Progress:

 PiecesTracker tracker = new PiecesTracker(ti);

 // For each file, find how much has been downloaded sequentially
 for (int fileIndex = 0; fileIndex < tracker.numFiles(); fileIndex++) {
     long downloaded = tracker.getSequentialDownloadedBytes(fileIndex);
     int completePieces = tracker.getSequentialDownloadedPieces(fileIndex);
     long totalSize = ti.files().fileSize(fileIndex);

     double percent = (double) downloaded / totalSize * 100;
     System.out.println(\"File \" + fileIndex + \": \" +\n *         String.format(\"%.1f%% (%d bytes, %d pieces)\",\n *             percent, downloaded, completePieces));\n * }
 

Sequential vs Total Progress:

This tracker focuses on sequential download progress - how much of the file has been downloaded starting from the beginning without gaps:

 // Example: File with 5 pieces
 // Pieces downloaded: [✓ ✓ ✗ ✓ ✓]
 //                     0 1 2 3 4

 // Sequential progress = 2 pieces (0, 1) - stops at first gap
 int sequential = tracker.getSequentialDownloadedPieces(fileIndex);\n * // Result: 2\n *
 // Total progress = 4 pieces downloaded (but not sequentially)
 

Monitoring Progress for Streaming:

 // Useful for video/audio streaming that requires sequential data
 PiecesTracker tracker = new PiecesTracker(ti);\n *
 while (torrentActive) {
     for (int i = 0; i < tracker.numFiles(); i++) {
         long seqBytes = tracker.getSequentialDownloadedBytes(i);
         long totalBytes = ti.files().fileSize(i);

         // Can stream once we have 10% downloaded sequentially
         if (seqBytes >= totalBytes * 0.1) {
             startStreaming(i);
         }
     }
     Thread.sleep(1000);  // Check every second
 }
 

File-Piece Structure Example:

 // 3 files, 4 pieces total
 // File 0 (10MB) → spans pieces 0-1
 // File 1 (5MB)  → spans pieces 1-2
 // File 2 (15MB) → spans pieces 2-3

 tracker.setComplete(0, true);
 tracker.setComplete(1, true);
 // Download stalled at piece 2

 // Sequential downloads:
 tracker.getSequentialDownloadedBytes(0);  // Full 10MB
 tracker.getSequentialDownloadedBytes(1);  // Partial 5MB (piece 1 done, piece 2 missing)
 tracker.getSequentialDownloadedBytes(2);  // 0 bytes (blocked on piece 2)
 

Performance Notes:

  • Initialization maps all files and pieces (O(n) operation)
  • Status queries are O(1) for individual pieces
  • Sequential download calculation is O(pieces_in_file)
  • Useful for priority-based downloading based on user streaming needs
See Also:
  • Constructor Details

  • Method Details

    • numFiles

      public int numFiles()
    • numPieces

      public int numPieces()
    • isComplete

      public boolean isComplete(int pieceIndex)
    • setComplete

      public void setComplete(int pieceIndex, boolean complete)
    • getSequentialDownloadedBytes

      public long getSequentialDownloadedBytes(int fileIndex)
    • getSequentialDownloadedPieces

      public int getSequentialDownloadedPieces(int fileIndex)