-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
…ntial progress of an individual file inside a download.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.frostwire.jlibtorrent; | ||
|
||
import sun.plugin.dom.exception.InvalidStateException; | ||
|
||
import java.util.Comparator; | ||
import java.util.PriorityQueue; | ||
|
||
/** | ||
* @author gubatron | ||
* @author aldenml | ||
*/ | ||
public final class FileSliceTracker { | ||
|
||
private final int fileIndex; | ||
|
||
private final SortedSlices sortedSlices; | ||
|
||
private boolean frozen; | ||
private FileSlice[] slices; | ||
private boolean[] complete; | ||
|
||
public FileSliceTracker(int fileIndex) { | ||
this.fileIndex = fileIndex; | ||
|
||
this.sortedSlices = new SortedSlices(); | ||
this.frozen = false; | ||
} | ||
|
||
public int getFileIndex() { | ||
return fileIndex; | ||
} | ||
|
||
public void addSlice(FileSlice slice) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
aldenml
Author
Collaborator
|
||
if (frozen) { | ||
throw new InvalidStateException("Can't track slice if already frozen"); | ||
} | ||
|
||
if (slice.getFileIndex() != fileIndex) { | ||
throw new IllegalArgumentException("Invalid file index"); | ||
} | ||
|
||
sortedSlices.add(slice); | ||
} | ||
|
||
public void freeze() { | ||
frozen = true; | ||
slices = sortedSlices.toArray(new FileSlice[0]); | ||
complete = new boolean[slices.length]; | ||
} | ||
|
||
public int getNumSlices() { | ||
checkFrozen(); | ||
|
||
return slices.length; | ||
} | ||
|
||
public boolean isComplete(int index) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
checkFrozen(); | ||
|
||
return complete[index]; | ||
} | ||
|
||
public void setComplete(int index, boolean complete) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
checkFrozen(); | ||
|
||
this.complete[index] = complete; | ||
} | ||
|
||
private void checkFrozen() { | ||
if (!frozen) { | ||
throw new InvalidStateException("Tracker needs to be frozen"); | ||
} | ||
} | ||
|
||
private static final class SortedSlices extends PriorityQueue<FileSlice> { | ||
|
||
public SortedSlices() { | ||
super(new FileSliceComparator()); | ||
} | ||
} | ||
|
||
private static final class FileSliceComparator implements Comparator<FileSlice> { | ||
|
||
@Override | ||
public int compare(FileSlice o1, FileSlice o2) { | ||
return Long.compare(o1.getOffset(), o2.getOffset()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.frostwire.jlibtorrent.demo; | ||
|
||
import com.frostwire.jlibtorrent.FileSlice; | ||
import com.frostwire.jlibtorrent.FileSliceTracker; | ||
import com.frostwire.jlibtorrent.TorrentInfo; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* @author gubatron | ||
* @author aldenml | ||
*/ | ||
public final class PieceMap { | ||
|
||
|
||
public static void main(String[] args) throws Throwable { | ||
|
||
// comment this line for a real application | ||
args = new String[]{"/Users/aldenml/Downloads/ReSet_Resynthformation_SOSEP051_FrostClick_FrostWire_6_28_2014.torrent"}; | ||
|
||
File torrentFile = new File(args[0]); | ||
|
||
TorrentInfo ti = new TorrentInfo(torrentFile); | ||
|
||
int numPieces = ti.getNumPieces(); | ||
|
||
System.out.println("Num Pieces: " + numPieces); | ||
|
||
FileSliceTracker[] trackers = new FileSliceTracker[ti.getNumFiles()]; | ||
|
||
for (int i = 0; i < numPieces; i++) { | ||
ArrayList<FileSlice> slices = ti.mapBlock(i, 0, ti.getPieceSize(i)); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
for (FileSlice slice : slices) { | ||
int fileIndex = slice.getFileIndex(); | ||
|
||
if (trackers[fileIndex] == null) { | ||
trackers[fileIndex] = new FileSliceTracker(fileIndex); | ||
} | ||
|
||
trackers[fileIndex].addSlice(slice); | ||
} | ||
} | ||
|
||
int totalSlices = 0; | ||
|
||
for (FileSliceTracker tracker : trackers) { | ||
tracker.freeze(); | ||
int numSlices = tracker.getNumSlices(); | ||
System.out.println("File Index: " + tracker.getFileIndex() + ", slices: " + numSlices); | ||
totalSlices = totalSlices + numSlices; | ||
} | ||
|
||
System.out.println("Total Slices: " + totalSlices); | ||
} | ||
} |
Looking at the implementation, let's do this:
public void addSlice(FileSlice slice) throws InvalidStateException, IllegalArgumentException
Principle of Least Astonishment
Don't want to surprise API users with unexpected crashes.