-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloading.java
494 lines (424 loc) · 19.2 KB
/
Downloading.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package downloader;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import javax.swing.*;
/**
*
* @developer Ashish Gupta
*/
public class Downloading extends JFrame {
private String inputU,outputU;
private String tempO;
private String incomingSize;
private String ElapsedTime;
private Thread timerThread,resThread;
private Thread dwnldThread;
private DownloadModule dn;
private long Elapsedtime=0;
private boolean hasStopped = false;
private boolean hasPaused=false;
private boolean allOK=false;
private long startbyte=0;
private int currProgress=0;
public Downloading(String iURL, String oURL) {
Elapsedtime=0;
// setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
inputU=iURL;
outputU=oURL+".bid";
tempO=oURL;
initComponents();
PauseButton.setEnabled(true);
ExitButton.setVisible(false);
setTitle(String.format("Downloading %s ",iURL));
setVisible(true);
urlText.setText(iURL);
ProgressBar.setStringPainted(true);
StopButton.addActionListener(new StopHandler());
PauseButton.addActionListener(new PauseButtonHandler());
dn=new DownloadModule();
dwnldThread=new Thread(dn);
dwnldThread.start();
}
private class DownloadModule implements Runnable{
Timer t1;
private final int BUFFER_SIZE=4096;
private final int CONNECT_TIMEOUT=5000;
public DownloadModule(){
// System.out.println("Thread Started: "+this.toString());
}
private String ParseSize(long B){
long GB=0,MB=0,KB=0;
String fixK,fixM,fixG;
boolean hasGB=false,hasMB=false,hasKB=false;
if(B>=1024){
KB=(int)(B>>10);
hasKB=true;
B%=1024;
}
if(KB>=1024){
hasMB=true;
MB=(int)(KB>>10);
KB%=1024;
}
if(MB>=1024){
hasGB=true;
GB=(int)(MB>>10);
MB%=1024;
}
if(KB<100.0)
fixK="0"+(int)(KB/10.24);
else
fixK=""+(int)(KB/10.24);
if(MB<100.0)
fixM="0"+(int)(MB/10.24);
else
fixM=""+(int)(MB/10.24);
if(hasGB)
return String.format(GB+"."+ fixM + " GB");
else if(hasMB)
return String.format(MB+"."+ fixK + " MB");
else
return String.format(KB+"."+(int)(B/10.24) + " KB");
}
public void dresume(){
resThread=new Thread(this);
resThread.start();
}
public void startDownload()throws IOException{
BufferedInputStream in=null;
// FileOutputStream fout=null;
RandomAccessFile ofile;
try{
int check;
long completed=0;
byte[] buffer;
addToConsole("Connecting to Server...");
URL u=new URL(inputU);
HttpURLConnection connection=(HttpURLConnection) u.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(CONNECT_TIMEOUT);
ofile=new RandomAccessFile(outputU,"rw");
ofile.seek(startbyte);
setTitle("Downloading "+u.getFile());
in=new BufferedInputStream(connection.getInputStream());
timerThread=new Thread(new Timer());
long size=connection.getContentLengthLong();
String ParsedTotSize=ParseSize(size);
SizeLabel.setText(ParseSize(size));
long startTime=System.nanoTime();
long initsize=ofile.length();
String speed="";
StopButton.setEnabled(true);
buffer=new byte[BUFFER_SIZE];
System.out.println("Started Timer");
timerThread.start();
ProgressBar.setValue(currProgress);
in.skip(startbyte);
while((check=in.read(buffer))!=-1&&!hasStopped&&!hasPaused){
if(hasPaused){
ElapsedTime=timeremainingLabel.getText();
return;
}
addToConsole("Read: "+(check>>10) +" KBs");
ofile.write(buffer, 0, check);
startbyte+=check;
completed=ofile.length();
if(System.nanoTime()-startTime>=1000000000){
speed=""+ParseSize(ofile.length()-initsize)+"/s";
initsize=ofile.length();
startTime=System.nanoTime();
}
transLabel.setText(ParseSize(completed)+" of "+ParsedTotSize+ " at " + speed);
// addToConsole(" Read "+check+"bytes " + "done with " + completed +" bytes");
currProgress=(int)(completed*100/size);
ProgressBar.setValue(currProgress);
}
System.out.println("outside loop");
if(ofile.length()==size){
System.out.println("ALl OK CHanged");
allOK=true;
rename();
// new File(outputU).renameTo(new File(outputU.substring(0,outputU.lastIndexOf('.')-1)));
}
in.close();
ElapsedTime=timeremainingLabel.getText();
incomingSize=ParseSize(ofile.length());
//hasStopped=true;
} catch (UnknownHostException E){
SizeLabel.setText("-");
addToConsole("Unable to connect.");
// E.printStackTrace();
} catch (ConnectException E){
addToConsole("Connection Timed Out");
}
catch (Exception E){
E.printStackTrace();
} finally {
ExitButton.setVisible(true);
}
System.out.println("Thread ended: "+this.toString());
}
public void run(){
try{
System.out.println("Thread Started: "+this.toString());
startDownload();
if(allOK){
System.out.println("Download Completed Successfully");
Biginttext.setForeground(Color.GREEN);
DownloadCompletedBox comp=new DownloadCompletedBox(inputU,outputU.substring(0,outputU.lastIndexOf('.')),incomingSize,ElapsedTime);
setVisible(false);
}
}
catch(Exception E){
E.printStackTrace();
}
}
}
private class Timer implements Runnable{
private long time=0;
private long donewith;
public Timer(){
}
//@Override
private String toProp(long sec){
boolean hashrs=false;
boolean hasmins=false;
long min=0,hour=0;
while(sec>=60){
hasmins=true;
sec/=60;
min++;
}
while(min>=60){
min/=60;
hashrs=true;
hour++;
}
if(!hasmins)
return String.format("%d seconds", sec);
else if(!hashrs)
return String.format("%d minutes and %d seconds", min,sec);
else
return String.format("%d hour(s) %d minute(s) and %d seconds", hour,min,sec);
}
public void run(){
time=System.nanoTime();
// timeremainingLabel.setText("Not");
while(!hasStopped&&!hasPaused){
donewith=(Elapsedtime+System.nanoTime()-time)/1000000000;
timeremainingLabel.setText(toProp(donewith));
Elapsedtime=donewith;
}
}
}
private class StopHandler implements ActionListener{
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// dwnldThread.
hasStopped=true;
File todelete=new File(outputU+".bid");
todelete.delete();
StopButton.setEnabled(false);
ProgressBar.setValue(0);
setTitle("Download Stopped");
Biginttext.setForeground(new Color(255,0,0));
ExitButton.setVisible(true);
}
}
private class ExitButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
CloseMe();
}
}
private class PauseButtonHandler implements ActionListener{
/*
dn = new DownloadModule(inputU,outputU);
dwnldThread=new Thread(dn);
dwnldThread.start();
*/
@Override
public void actionPerformed(ActionEvent e) {
if(!hasPaused){ //Previouly running
hasPaused=true;
PauseButton.setText("Resume");
addToConsole("Download Paused at "+currProgress+ "%");
} else {
hasPaused=false;
dn.dresume();
PauseButton.setText("Pause");
addToConsole("Download resumed!");
}
}
}
private void rename(){
/* File newFile,oldFile;
newFile=new File(tempO);
oldFile=new File(outputU);
System.out.println("inside rename folder");
if (!oldFile.renameTo(newFile)){
System.out.println("Unable to rename!");
tempO+='1';
rename();
}*/
}
public void addToConsole(String a){
ConsoleArea.setText(ConsoleArea.getText()+"\n"+a);
}
public int returnCurrentProg(){
return ProgressBar.getValue();
}
private void CloseMe(){
super.dispose();
}
public long getStartByte(){
return startbyte;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
ProgressBar = new javax.swing.JProgressBar();
URL_ = new javax.swing.JLabel();
StopButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
timeremainingLabel = new javax.swing.JLabel();
SizeLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
ConsoleArea = new javax.swing.JTextArea();
urlText = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
ExitButton = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
transLabel = new javax.swing.JLabel();
Biginttext = new javax.swing.JLabel();
PauseButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
setType(java.awt.Window.Type.UTILITY);
URL_.setText("URL:");
StopButton.setText("Stop");
StopButton.setEnabled(false);
jLabel2.setText("Elapsed Time:");
timeremainingLabel.setText("--:--");
SizeLabel.setText("-");
SizeLabel.setToolTipText("");
ConsoleArea.setColumns(20);
ConsoleArea.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
ConsoleArea.setRows(5);
jScrollPane1.setViewportView(ConsoleArea);
jLabel1.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
jLabel1.setText("Logs");
ExitButton.setText("Exit");
jLabel3.setText("Transferred: ");
transLabel.setText("0");
Biginttext.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
Biginttext.setForeground(new java.awt.Color(255, 51, 0));
Biginttext.setText("BigInt Downloader");
PauseButton.setText("Pause");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(ProgressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(StopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(PauseButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ExitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(URL_)
.addGap(18, 18, 18)
.addComponent(urlText, javax.swing.GroupLayout.PREFERRED_SIZE, 392, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(transLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(timeremainingLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(SizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(32, Short.MAX_VALUE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Biginttext)
.addGap(181, 181, 181))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Biginttext)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(urlText, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(URL_))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(timeremainingLabel)
.addComponent(SizeLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(transLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(StopButton)
.addComponent(ExitButton)
.addComponent(PauseButton))
.addGap(3, 3, 3)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel Biginttext;
private javax.swing.JTextArea ConsoleArea;
private javax.swing.JButton ExitButton;
private javax.swing.JButton PauseButton;
private javax.swing.JProgressBar ProgressBar;
private javax.swing.JLabel SizeLabel;
private javax.swing.JButton StopButton;
private javax.swing.JLabel URL_;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel timeremainingLabel;
private javax.swing.JLabel transLabel;
private javax.swing.JTextField urlText;
// End of variables declaration//GEN-END:variables
}