-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpDownloader.java
More file actions
155 lines (125 loc) · 4.02 KB
/
HttpDownloader.java
File metadata and controls
155 lines (125 loc) · 4.02 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
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDownloader extends Downloader{
public HttpDownloader(URL url, String outputFolder, int numConnections) {
super(url, outputFolder, numConnections);
download();
}
private void error() {
System.out.println("ERROR");
setState(ERROR);
}
@Override
public void run() {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)mURL.openConnection();
conn.setConnectTimeout(10000);
conn.connect();
if (conn.getResponseCode() / 100 != 2) {
error();
}
int contentLength = conn.getContentLength();
if (contentLength < 1) {
error();
}
if (mFileSize == -1) {
mFileSize = contentLength;
stateChanged();
System.out.println("Dung luong file: " + mFileSize);
}
if (mState == DOWNLOADING) {
if (mListDownloadThread.size() == 0)
{
if (mFileSize > MIN_DOWNLOAD_SIZE) {
int partSize = Math.round(((float)mFileSize / mNumConnections) / BLOCK_SIZE) * BLOCK_SIZE;
System.out.println("kich thuoc tung phan: " + partSize);
int startByte = 0;
int endByte = partSize - 1;
HttpDownloadThread aThread = new HttpDownloadThread(1, mURL, mOutputFolder + mFileName, startByte, endByte);
mListDownloadThread.add(aThread);
int i = 2;
while (endByte < mFileSize) {
startByte = endByte + 1;
endByte += partSize;
aThread = new HttpDownloadThread(i, mURL, mOutputFolder + mFileName, startByte, endByte);
mListDownloadThread.add(aThread);
++i;
}
} else
{
HttpDownloadThread aThread = new HttpDownloadThread(1, mURL, mOutputFolder + mFileName, 0, mFileSize);
mListDownloadThread.add(aThread);
}
} else {
for (int i=0; i<mListDownloadThread.size(); ++i) {
if (!mListDownloadThread.get(i).isFinished())
mListDownloadThread.get(i).download();
}
}
for (int i=0; i<mListDownloadThread.size(); ++i) {
mListDownloadThread.get(i).waitFinish();
}
if (mState == DOWNLOADING) {
setState(COMPLETED);
}
}
} catch (Exception e) {
error();
} finally {
if (conn != null)
conn.disconnect();
}
}
private class HttpDownloadThread extends DownloadThread {
public HttpDownloadThread(int threadID, URL url, String outputFile, int startByte, int endByte) {
super(threadID, url, outputFile, startByte, endByte);
}
@Override
public void run() {
BufferedInputStream in = null;
RandomAccessFile raf = null;
try {
HttpURLConnection conn = (HttpURLConnection)mURL.openConnection();
String byteRange = mStartByte + "-" + mEndByte;
conn.setRequestProperty("Range", "bytes=" + byteRange);
System.out.println("bytes=" + byteRange);
conn.connect();
if (conn.getResponseCode() / 100 != 2) {
error();
}
in = new BufferedInputStream(conn.getInputStream());
raf = new RandomAccessFile(mOutputFile, "rw");
raf.seek(mStartByte);
byte data[] = new byte[BUFFER_SIZE];
int numRead;
while((mState == DOWNLOADING) && ((numRead = in.read(data,0,BUFFER_SIZE)) != -1))
{
raf.write(data,0,numRead);
mStartByte += numRead;
downloaded(numRead);
}
if (mState == DOWNLOADING) {
mIsFinished = true;
}
} catch (IOException e) {
error();
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
}
System.out.println("ket thuc thread " + mThreadID);
}
}
}