-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadThread.java
More file actions
54 lines (47 loc) · 1.73 KB
/
DownloadThread.java
File metadata and controls
54 lines (47 loc) · 1.73 KB
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
import java.io.*;
import java.net.*;
public class DownloadThread extends Thread {
private String urlToDownload;
private String fileName;
private long startByte;
private long endByte;
private long downloadedBytes;
public DownloadThread(String urlToDownload, String fileName, long startByte, long endByte) {
this.urlToDownload = urlToDownload;
this.fileName = fileName;
this.startByte = startByte;
this.endByte = endByte;
}
public void run() {
try {
URL u = new URL(urlToDownload);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setRequestProperty("Range", "bytes=" + startByte + "-" + endByte);
InputStream in = uc.getInputStream();
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
raf.seek(startByte);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
synchronized (ConsoleIDM.class) {
while (ConsoleIDM.paused) {
try {
ConsoleIDM.class.wait();
} catch (InterruptedException e) {
e.printStackTrace(); // Handle interruption as needed
}
}
}
raf.write(buffer, 0, bytesRead);
downloadedBytes += bytesRead;
}
raf.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public long getDownloadedBytes() {
return downloadedBytes;
}
}