-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelDownloader.java
More file actions
86 lines (65 loc) · 1.97 KB
/
ParallelDownloader.java
File metadata and controls
86 lines (65 loc) · 1.97 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
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ParallelDownloader implements Runnable {
String fileName, location, cleanName;
public ParallelDownloader(String fileName, String location, String cleanName) {
this.fileName = fileName;
this.location = location;
this.cleanName = cleanName;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public void run() {
int port = 0;
System.out.println("Parallel download for :" + fileName + " from "
+ location);
try {
// Open fresh port to receive file.
port = Utilities.getAvailablePort();
// Send pull request
String host[] = location.split(":");
CANMessage downloadRequest = new CANMessage("sendfile", fileName,
port);
Socket socket = new Socket(host[0], Integer.parseInt(host[1]));
ObjectOutputStream o = new ObjectOutputStream(
socket.getOutputStream());
o.writeObject(downloadRequest);
ServerSocket serv = new ServerSocket(port);
Socket receiver = serv.accept();
// Download file and write to ret folder.
File downloadedFile = new File(Peer.getDir() + "/" + Peer.hostName
+ "/ret/" + cleanName);
try {
FileOutputStream fos = new FileOutputStream(downloadedFile,
false);
byte[] buffer = new byte[1024];
int count;
InputStream in = receiver.getInputStream();
// System.out.println(in.available());
while ((count = in.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
fos.close();
socket.close();
} catch (Exception exp) {
exp.printStackTrace();
}
} catch (NumberFormatException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(2000, 0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}