Skip to content

Commit

Permalink
update assignment of download
Browse files Browse the repository at this point in the history
  • Loading branch information
fei9009 committed Mar 20, 2017
1 parent 0298c1d commit f1b7e90
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.fei9009.coderising0305.download;

import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;

import com.github.fei9009.coderising0305.download.api.Connection;

Expand All @@ -9,30 +10,28 @@ public class DownloadThread extends Thread{
Connection conn;
int startPos;
int endPos;

public DownloadThread( Connection conn, int startPos, int endPos){
CyclicBarrier barrier;
String localFile;

public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){

this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
this.localFile = localFile;
this.barrier = barrier;
}
public void run(){
RandomAccessFile out = null;
try{
byte[] buffer = conn.read(startPos, endPos);
out = new RandomAccessFile("src/com/github/fei9009/coderising0305/download/fruits.jpg","rwd");
out.seek(startPos);
out.write(buffer);
FileDownloader fileloader = new FileDownloader("");
fileloader.getListener();
RandomAccessFile file = new RandomAccessFile(localFile,"rw");
file.seek(startPos);
file.write(buffer);
file.close();
conn.close();
barrier.await();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.github.fei9009.coderising0305.download;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;

import com.github.fei9009.coderising0305.download.api.Connection;
import com.github.fei9009.coderising0305.download.api.ConnectionException;
import com.github.fei9009.coderising0305.download.api.ConnectionManager;
import com.github.fei9009.coderising0305.download.api.DownloadListener;

public class FileDownloader {

String url;

private String url;
private String localFile;
DownloadListener listener;

ConnectionManager cm;

//At most 3 threads
private static final int DOWNLOAD_TRHEAD_NUM = 3;

public FileDownloader(String _url) {
public FileDownloader(String _url, String localFile) {
this.url = _url;

this.localFile = localFile;
}

public void execute(){
Expand All @@ -33,24 +38,28 @@ public void execute(){
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法

// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){
public void run(){
listener.notifyFinished();
}
});
Connection conn = null;

try {

conn = cm.open(this.url);

int length = conn.getContentLength();
int count = 6;
int block = length/10024/count;
for (int i = 1;i <= count; i++){
int startPos = (i-1) * block;
int endPos = i * block - 1;
if (i == count) {
endPos = length;
}
createPlaceHolderFile(this.localFile, length);

int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length);

for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){
DownloadThread thread = new DownloadThread(cm.open(url), ranges[i][0], ranges[i][1], localFile, barrier);
thread.start();
}
new DownloadThread(conn,0,length-1).start();

} catch (ConnectionException e) {
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn != null){
Expand All @@ -59,6 +68,33 @@ public void execute(){
}
}

private void createPlaceHolderFile(String fileName, int contentLen) throws IOException{

RandomAccessFile file = new RandomAccessFile(fileName,"rw");
for(int i=0; i<contentLen ;i++){
file.write(0);
}
file.close();
}

private int[][] allocateDownloadRange(int threadNum, int contentLen){
int[][] ranges = new int[threadNum][2];

int eachThreadSize = contentLen / threadNum;
int left = contentLen % threadNum;

for(int i=0;i<threadNum;i++){
int startPos = i * eachThreadSize;
int endPos = (i + 1) * eachThreadSize - 1;
if ((i == (threadNum - 1))) {
endPos += left;
}
ranges[i][0] = startPos;
ranges[i][1] = endPos;
}
return ranges;
}

public void setListener(DownloadListener listener) {
this.listener = listener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public void tearDown() throws Exception {
@Test
public void testDownload() {

String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000";
String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png";

FileDownloader downloader = new FileDownloader(url);
FileDownloader downloader = new FileDownloader(url,"c:\\ctest.jpg");


ConnectionManager cm = new ConnectionManagerImpl();
Expand Down Expand Up @@ -54,7 +54,6 @@ public void notifyFinished() {
}
System.out.println("下载完成!");



}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,66 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

import com.github.fei9009.coderising0305.download.api.Connection;
import com.github.fei9009.coderising0305.download.api.ConnectionException;


public class ConnectionImpl implements Connection{

private String url;
URL url;
static final int BUFFER_SIZE = 1024;

public ConnectionImpl(String url){
this.url=url;
ConnectionImpl(String _url) throws ConnectionException{
try {
url = new URL(_url);
} catch (MalformedURLException e) {
throw new ConnectionException();
}
}

@Override
public byte[] read(int startPos, int endPos) throws IOException {

URL url = new URL(this.url);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Range", "bytes="+startPos+"-"+endPos);
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int size = 0;
while ((size = in.read(buffer)) != -1) {
out.write(buffer, 0, size);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);
InputStream is = httpConn.getInputStream();
//is.skip(startPos);
byte[] buff = new byte[BUFFER_SIZE];
int totalLen = endPos - startPos + 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();

while(baos.size() < totalLen){
int len = is.read(buff);
if (len < 0) {
break;
}
baos.write(buff,0, len);
}
if(baos.size() > totalLen){
byte[] data = baos.toByteArray();
return Arrays.copyOf(data, totalLen);
}
byte[] b = out.toByteArray();
out.close();
in.close();
return b;
return baos.toByteArray();
}

@Override
public int getContentLength() {

try{
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
return conn.getContentLength();
}catch(Exception e){
URLConnection con;
try {
con = url.openConnection();
return con.getContentLength();

} catch (IOException e) {
e.printStackTrace();
}
}
return -1;
}

Expand Down

0 comments on commit f1b7e90

Please sign in to comment.