-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloader.java
More file actions
134 lines (100 loc) · 2.74 KB
/
Downloader.java
File metadata and controls
134 lines (100 loc) · 2.74 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.net.URL;
import java.util.ArrayList;
import java.util.Observable;
public abstract class Downloader extends Observable implements Runnable{
protected URL mURL;
protected String mOutputFolder;
protected int mNumConnections;
protected String mFileName;
protected int mFileSize;
protected int mState;
protected int mDownloaded;
protected ArrayList<DownloadThread> mListDownloadThread;
protected static final int BLOCK_SIZE = 4096;
protected static final int BUFFER_SIZE = 4096;
protected static final int MIN_DOWNLOAD_SIZE = BLOCK_SIZE * 100;
public static final String STATUSES[] = {"Downloading","Paused", "Complete", "Cancelled", "Error"};
public static final int DOWNLOADING = 0;
public static final int PAUSED = 1;
public static final int COMPLETED = 2;
public static final int CANCELLED = 3;
public static final int ERROR = 4;
protected Downloader(URL url, String outputFolder, int numConnections) {
mURL = url;
mOutputFolder = outputFolder;
mNumConnections = numConnections;
String fileURL = url.getFile();
mFileName = fileURL.substring(fileURL.lastIndexOf('/') + 1);
System.out.println("ten file: " + mFileName);
mFileSize = -1;
mState = DOWNLOADING;
mDownloaded = 0;
mListDownloadThread = new ArrayList<DownloadThread>();
}
public void pause() {
setState(PAUSED);
}
public void resume() {
setState(DOWNLOADING);
download();
}
public void cancel() {
setState(CANCELLED);
}
public String getURL() {
return mURL.toString();
}
public int getFileSize() {
return mFileSize;
}
public float getProgress() {
return ((float)mDownloaded / mFileSize) * 100;
}
public int getState() {
return mState;
}
protected void setState(int value) {
mState = value;
stateChanged();
}
protected void download() {
Thread t = new Thread(this);
t.start();
}
protected synchronized void downloaded(int value) {
mDownloaded += value;
stateChanged();
}
protected void stateChanged() {
setChanged();
notifyObservers();
}
protected abstract class DownloadThread implements Runnable {
protected int mThreadID;
protected URL mURL;
protected String mOutputFile;
protected int mStartByte;
protected int mEndByte;
protected boolean mIsFinished;
protected Thread mThread;
public DownloadThread(int threadID, URL url, String outputFile, int startByte, int endByte) {
mThreadID = threadID;
mURL = url;
mOutputFile = outputFile;
mStartByte = startByte;
mEndByte = endByte;
mIsFinished = false;
download();
}
public boolean isFinished() {
return mIsFinished;
}
public void download() {
mThread = new Thread(this);
mThread.start();
}
public void waitFinish() throws InterruptedException {
mThread.join();
}
}
}