forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from vxzh/master
L3
- Loading branch information
Showing
10 changed files
with
672 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
group13/2729382520/L3/src/io/github/vxzh/download/DownloadThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.vxzh.download; | ||
|
||
import io.github.vxzh.download.api.Connection; | ||
|
||
import java.io.RandomAccessFile; | ||
import java.util.concurrent.CyclicBarrier; | ||
|
||
public class DownloadThread extends Thread { | ||
|
||
private Connection conn; | ||
private int startPos; | ||
private int endPos; | ||
private CyclicBarrier barrier; | ||
|
||
public DownloadThread(Connection conn, int startPos, int endPos, CyclicBarrier barrier) { | ||
|
||
this.conn = conn; | ||
this.startPos = startPos; | ||
this.endPos = endPos; | ||
this.barrier = barrier; | ||
} | ||
|
||
public void run() { | ||
|
||
try { | ||
|
||
byte[] buffer = conn.read(startPos, endPos); | ||
RandomAccessFile raf = new RandomAccessFile("/Users/xuxiaoqing/Documents/demo.jpg", "rw"); | ||
raf.seek(startPos); | ||
raf.write(buffer, 0, buffer.length); | ||
//raf.write(buffer); | ||
raf.close(); | ||
barrier.await(); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
group13/2729382520/L3/src/io/github/vxzh/download/FileDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.github.vxzh.download; | ||
|
||
import io.github.vxzh.download.api.Connection; | ||
import io.github.vxzh.download.api.ConnectionManager; | ||
import io.github.vxzh.download.api.DownloadListener; | ||
|
||
import java.util.concurrent.CyclicBarrier; | ||
|
||
public class FileDownloader { | ||
|
||
private String path; | ||
|
||
private DownloadListener listener; | ||
|
||
private ConnectionManager cm; | ||
|
||
private static final int THREAD_NUM = 3; | ||
|
||
public FileDownloader(String path) { | ||
this.path = path; | ||
|
||
} | ||
|
||
public void execute() { | ||
|
||
CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() { | ||
@Override | ||
public void run() { | ||
listener.notifyFinished(); | ||
} | ||
}); | ||
|
||
Connection conn = null; | ||
try { | ||
|
||
conn = cm.open(this.path); | ||
//实际的文件长度 | ||
int length = conn.getContentLength(); | ||
//平均每一个线程下载的文件大小. | ||
int blockSize = length / THREAD_NUM; | ||
for (int threadId = 1; threadId <= THREAD_NUM; threadId++) { | ||
int startIndex = (threadId - 1) * blockSize; | ||
int endIndex = threadId * blockSize - 1; | ||
//最后一个线程下载的长度 | ||
if (threadId == THREAD_NUM) { | ||
endIndex = length - 1; | ||
} | ||
|
||
System.out.println("线程:" + threadId + "下载:---" + startIndex + "--->" + endIndex); | ||
new DownloadThread(conn, startIndex, endIndex, barrier).start(); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
public void setListener(DownloadListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
|
||
public void setConnectionManager(ConnectionManager ucm) { | ||
this.cm = ucm; | ||
} | ||
|
||
public DownloadListener getListener() { | ||
return this.listener; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
group13/2729382520/L3/src/io/github/vxzh/download/FileDownloaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.vxzh.download; | ||
|
||
import io.github.vxzh.download.api.ConnectionManager; | ||
import io.github.vxzh.download.api.DownloadListener; | ||
import io.github.vxzh.download.impl.ConnectionManagerImpl; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class FileDownloaderTest { | ||
private boolean downloadFinished = false; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testDownload() { | ||
|
||
String url = "http://c.hiphotos.baidu.com/zhidao/pic/item/29381f30e924b8999bfaab046d061d950b7bf6cc.jpg"; | ||
|
||
FileDownloader downloader = new FileDownloader(url); | ||
ConnectionManager cm = new ConnectionManagerImpl(); | ||
downloader.setConnectionManager(cm); | ||
|
||
downloader.setListener(new DownloadListener() { | ||
@Override | ||
public void notifyFinished() { | ||
downloadFinished = true; | ||
} | ||
|
||
}); | ||
|
||
downloader.execute(); | ||
|
||
// 等待多线程下载程序执行完毕 | ||
while (!downloadFinished) { | ||
try { | ||
System.out.println("还没有下载完成,休眠五秒"); | ||
//休眠5秒 | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
System.out.println("下载完成!"); | ||
} | ||
} |
Oops, something went wrong.