Skip to content

Commit

Permalink
Merge pull request #11 from vxzh/master
Browse files Browse the repository at this point in the history
L3
  • Loading branch information
CoderXLoong authored Mar 13, 2017
2 parents 6d5f7e8 + 4fc6948 commit 7f700c2
Show file tree
Hide file tree
Showing 10 changed files with 672 additions and 0 deletions.
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();
}

}
}
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;
}

}
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("下载完成!");
}
}
Loading

0 comments on commit 7f700c2

Please sign in to comment.