-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recreated the PiecesTracker for optimal performance.
- Loading branch information
Showing
4 changed files
with
103 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.frostwire.jlibtorrent; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* @author gubatron | ||
* @author aldenml | ||
*/ | ||
public final class PiecesTracker { | ||
|
||
private final int numFiles; | ||
private final int numPieces; | ||
|
||
private final int[][] files; | ||
private final long[][] sizes; | ||
private final boolean[] complete; | ||
|
||
public PiecesTracker(TorrentInfo ti) { | ||
this.numFiles = ti.getNumFiles(); | ||
this.numPieces = ti.getNumPieces(); | ||
|
||
this.files = new int[numFiles][]; | ||
this.sizes = new long[numFiles][]; | ||
this.complete = new boolean[numPieces]; | ||
|
||
for (int fileIndex = 0; fileIndex < numFiles; fileIndex++) { | ||
FileEntry fe = ti.getFileAt(fileIndex); | ||
long size = fe.getSize(); | ||
int firstPiece = ti.mapFile(fileIndex, 0, 1).getPiece(); | ||
int lastPiece = ti.mapFile(fileIndex, size - 1, 1).getPiece(); | ||
|
||
int numSlices = lastPiece - firstPiece + 1; | ||
files[fileIndex] = new int[numSlices]; | ||
sizes[fileIndex] = new long[numSlices]; | ||
|
||
for (int pieceIndex = firstPiece; pieceIndex <= lastPiece; pieceIndex++) { | ||
int index = pieceIndex - firstPiece; | ||
|
||
files[fileIndex][index] = pieceIndex; | ||
|
||
ArrayList<FileSlice> slices = ti.mapBlock(pieceIndex, 0, ti.getPieceSize(pieceIndex)); | ||
for (FileSlice slice : slices) { | ||
if (slice.getFileIndex() == fileIndex) { | ||
sizes[fileIndex][index] = slice.getSize(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public int getNumFiles() { | ||
return numFiles; | ||
} | ||
|
||
public int getNumPieces() { | ||
return numPieces; | ||
} | ||
|
||
public boolean isComplete(int pieceIndex) throws IllegalArgumentException { | ||
return complete[pieceIndex]; | ||
} | ||
|
||
public void setComplete(int pieceIndex, boolean complete) throws IllegalArgumentException { | ||
this.complete[pieceIndex] = complete; | ||
} | ||
|
||
public long getSequentialDownloaded(int fileIndex) { | ||
int[] pieces = files[fileIndex]; | ||
|
||
long downloaded = 0; | ||
|
||
for (int i = 0; i < pieces.length; i++) { | ||
int pieceIndex = pieces[i]; | ||
|
||
if (complete[pieceIndex]) { | ||
downloaded += sizes[fileIndex][i]; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return downloaded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters