-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadEntry.java
More file actions
248 lines (220 loc) · 8.79 KB
/
Copy pathDownloadEntry.java
File metadata and controls
248 lines (220 loc) · 8.79 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Task;
import javafx.scene.control.ProgressIndicator;
public class DownloadEntry extends Task<Void> {
public SimpleStringProperty fileName;
public SimpleStringProperty urlText;
public SimpleIntegerProperty fileSize;
public SimpleDoubleProperty speed;
public URL url;
public int downloaded;
public int size = -1;
public URL refererUrl = null;
final int MAX_BUFFER_SIZE=50*1024;
private int status;
public static final String statuses[]={"Downloading","Error","Paused","Cancelled","Complete"};
public static final int Downloading=0;
public static final int Error=1;
public static final int Paused=2;
public static final int Cancelled=3;
public static final int Complete=4;
public boolean isPaused = false;
public boolean pauseCancel;
public final boolean PAUSE = false;
public final boolean CANCEL = true;
RandomAccessFile file=null;
InputStream stream=null;
//Constructor
public DownloadEntry(URL ur) throws Exception{
url = ur;
fileName = new SimpleStringProperty(url.toString().substring(url.toString().lastIndexOf("/")+1));
urlText = new SimpleStringProperty(url.toString());
speed = new SimpleDoubleProperty(0.0);
HttpURLConnection connect=null, sizecon=null, con=null;
try {
connect=(HttpURLConnection)url.openConnection();
connect.setRequestProperty("Range","bytes"+downloaded+"-");
connect.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
connect.connect();
if (connect.getResponseCode()/100!=2) {
this.updateMessage("Error");
return;
}
if (size==-1) {
sizecon = (HttpURLConnection)url.openConnection();
sizecon.setRequestMethod("HEAD");
sizecon.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
sizecon.getInputStream();
size=sizecon.getContentLength();
}
con = (HttpURLConnection)url.openConnection();
con.setRequestProperty("Range","bytes"+downloaded+"-");
if (size==-1) {
refererUrl = connect.getURL();
con.setRequestProperty("Referer",String.valueOf(refererUrl));
}
con.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
con.connect();
if (size==-1) {
sizecon=(HttpURLConnection)url.openConnection();
sizecon.setRequestMethod("HEAD");
sizecon.setRequestProperty("Referer",String.valueOf(refererUrl));
sizecon.getInputStream();
size = sizecon.getContentLength();
}
fileSize = new SimpleIntegerProperty(size);
}
catch(Exception e) {
System.out.println("constructor: " + e);
}
finally {
if(connect!=null) { connect.disconnect(); }
if(con!=null) { con.disconnect(); }
if(sizecon!=null) { sizecon.disconnect(); }
}
status = Downloading;
this.updateMessage("Downloading");
}
@Override
protected Void call() {
try {
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestProperty("Range","bytes"+downloaded+"-");
con.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
con.setRequestProperty("Referer", refererUrl.toString());
con.connect();
if (con.getResponseCode()/100!=2) {
this.updateMessage("Error");
return null;
}
updateProgress(ProgressIndicator.INDETERMINATE_PROGRESS, 1);
String filename = url.getFile();
filename = filename.substring(filename.lastIndexOf("/")+1);
file = new RandomAccessFile(filename, "rw");
file.seek(downloaded);
stream = con.getInputStream();
long currTime = System.currentTimeMillis();
int pastDownloaded = downloaded;
while (status==Downloading) {
byte buffer[];
if (size-downloaded>MAX_BUFFER_SIZE) {
buffer=new byte[MAX_BUFFER_SIZE];
}
else {
buffer=new byte[size-downloaded];
}
int c = stream.read(buffer);
if (downloaded==size){ //previous c==-1
break;
}
file.write(buffer,0,c);
downloaded += c;
updateProgress((1.0 *downloaded)/size, 1);
updateMessage("Downloading");
status = Downloading;
long elapsedTime = System.currentTimeMillis() - currTime;
speed.set((1.0 * (downloaded - pastDownloaded)) / elapsedTime); //inkBsps
if(this.isCancelled()) {
speed.set(0.0);
if(pauseCancel==PAUSE) {
status = Paused;
updateMessage("Paused");
}
else {
status = Cancelled;
downloaded = 0;
updateMessage("Cancelled");
}
return null;
}
}
if (status==Downloading) {
status = Complete;
updateMessage("Complete");
String temp = String.format(fileName.get() + " downloaded successfuly.");
//All interactions with JavaFX objects (including creation) must be done on JFX thread,
//if you want to access those JFX objects from another thread - use runLater or runAndWait methods.
Platform.runLater(() -> {
new AlertBox("Success", temp);
});
}
}
catch(Exception e) {
status = Error;
this.updateMessage("Error");
System.out.println("call: " + e);
}
finally {
if (file!=null) {
try{file.close();}catch(Exception E){}
}
if (stream!=null) {
try{stream.close();}catch(Exception E){}
}
}
return null;
}
void pause() {
if(status==Downloading) {
pauseCancel = PAUSE;
//sends interrupt, carries out following lines immediately, interrupt is handled in near future using isCancelled()
cancel();
System.out.println(statuses[status]);
}
}
public void cancelIt() {
System.out.println(statuses[status]);
if(status==Downloading || status==Paused) {
pauseCancel = CANCEL;
cancel();
downloaded = 0;
updateMessage("Cancelled"); //required if state was puased
}
}
public String getFileName() {
return fileName.get();
}
public void setFileName(String fileName) {
this.fileName.set(fileName);
}
public StringProperty fileNameProperty() {
return fileName;
}
public String getUrlText() {
return urlText.get();
}
public void setUrlText(String urlText) {
this.urlText.set(urlText);
}
public StringProperty urlTextProperty() {
return urlText;
}
public Integer getFileSize() {
return fileSize.get();
}
public void setFileSize(Integer fileSize) {
this.fileSize.set(fileSize);
}
public IntegerProperty fileSizeProperty() {
return fileSize;
}
public Double getSpeed() {
return speed.get();
}
public void setSpeed(Double speed) {
this.speed.set(speed);
}
public DoubleProperty speedProperty() {
return speed;
}
}