Skip to content

Commit dcd38a8

Browse files
committed
refactor multi-thread downloading
1 parent 8402a97 commit dcd38a8

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

group01/895457260/code/src/download/FileDownloader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ public class FileDownloader {
1717
private final int[] completedThreadCount = new int[1];
1818

1919
/**
20-
* 下载目录见 <code>Config</code>
20+
* 下载一个url指向的文件,下载目录见 <code>Config</code>
21+
*
2122
* @see Config#targetDirectory
22-
* @see #setConnectionManager(ConnectionManager)
23-
* @see #setListener(DownloadListener)
2423
* @see #execute()
2524
*/
2625
public FileDownloader(String url) {
@@ -201,7 +200,7 @@ public void setListener(DownloadListener listener) {
201200

202201
/**
203202
*
204-
* @param manager 通过url,打开连接
203+
* @param manager 通过url打开连接
205204
* @see ConnectionManager#open(String)
206205
*/
207206
public void setConnectionManager(ConnectionManager manager) {

group01/895457260/code/src/download/impl/ConnectionImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ public class ConnectionImpl implements Connection {
1212
private InputStream inputStream;
1313

1414
ConnectionImpl(String url) throws ConnectionException {
15-
init(url);
16-
}
17-
18-
private void init(String url) {
1915
try {
20-
connection = new URL(url).openConnection();
21-
inputStream = new BufferedInputStream(connection.getInputStream());
22-
inputStream.mark(connection.getContentLength()); // 标记在开头
16+
init(url);
2317
} catch (IOException e) {
24-
e.printStackTrace();
18+
throw new ConnectionException();
2519
}
2620
}
2721

22+
private void init(String url) throws IOException {
23+
connection = new URL(url).openConnection();
24+
inputStream = new BufferedInputStream(connection.getInputStream());
25+
inputStream.mark(connection.getContentLength()); // 标记在开头
26+
}
27+
2828
@Override
2929
public byte[] read(int startPos, int endPos) throws IOException {
3030
inputStream.reset(); // reset回到标记处

0 commit comments

Comments
 (0)