-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
PartialDownload.java
65 lines (47 loc) · 2.13 KB
/
PartialDownload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.frostwire.jlibtorrent.demo;
import com.frostwire.jlibtorrent.*;
import com.frostwire.jlibtorrent.alerts.BlockFinishedAlert;
import com.frostwire.jlibtorrent.alerts.TorrentFinishedAlert;
import com.frostwire.jlibtorrent.swig.entry;
import com.frostwire.jlibtorrent.swig.entry_vector;
import java.io.File;
import java.util.concurrent.CountDownLatch;
/**
* @author gubatron
* @author aldenml
*/
public final class PartialDownload {
public static void main(String[] args) throws Throwable {
// comment this line for a real application
args = new String[]{"/Users/aldenml/Downloads/Kellee_Maize_The_5th_Element_FrostClick_FrostWire_MP3_April_14_2014.torrent"};
File torrentFile = new File(args[0]);
System.out.println("Using libtorrent version: " + LibTorrent.version());
byte[] data = Utils.readFileToByteArray(torrentFile);
entry e = entry.bdecode(Vectors.bytes2byte_vector(data));
entry_vector files = e.find_key("info").find_key("files").list().to_vector();
System.out.println("Files inside the torrent");
for (int i = 0; i < files.size(); i++) {
System.out.println(files.get(i).find_key("path").list().front().to_string());
}
// Download only the first file
Priority[] priorities = Priority.array(Priority.IGNORE, (int) files.size());
priorities[0] = Priority.NORMAL;
final Session s = new Session();
final TorrentHandle th = s.addTorrent(new TorrentInfo(torrentFile), torrentFile.getParentFile(), priorities, null);
th.resume();
final CountDownLatch signal = new CountDownLatch(1);
s.addListener(new TorrentAlertAdapter(th) {
@Override
public void blockFinished(BlockFinishedAlert alert) {
int p = (int) (th.getStatus().getProgress() * 100);
System.out.println("Progress: " + p);
}
@Override
public void torrentFinished(TorrentFinishedAlert alert) {
System.out.print("Torrent finished");
signal.countDown();
}
});
signal.await();
}
}