Skip to content

Commit

Permalink
Merge pull request #26 from jiaxun1990/master
Browse files Browse the repository at this point in the history
 jiaxun1990
upload week03 download commit
  • Loading branch information
BlindingDark authored Mar 26, 2017
2 parents 68c0542 + 7518a62 commit 82a3fbd
Show file tree
Hide file tree
Showing 11 changed files with 827 additions and 0 deletions.
40 changes: 40 additions & 0 deletions group26/89460886/src/week03/source/download/DownloadThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package coding.coderising.download;

import coding.coderising.download.api.Connection;

import java.io.IOException;

/**
* @author jiaxun
*/
public class DownloadThread extends Thread{

private Connection connection;
private int startPos;
private int endPos;
private byte[] downloadByte;

public DownloadThread(Connection connection, int startPos, int endPos) {
this.connection = connection;
this.startPos = startPos;
this.endPos = endPos;
}

@Override
public void run() {
try {
downloadByte = connection.read(startPos, endPos);
} catch (IOException e) {
e.printStackTrace();
}
}

public byte[] getDownloadByte() {
return downloadByte;
}

public int getStartPos() {
return startPos;
}

}
101 changes: 101 additions & 0 deletions group26/89460886/src/week03/source/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package coding.coderising.download;

import coding.coderising.download.api.Connection;
import coding.coderising.download.api.ConnectionManager;
import coding.coderising.download.api.DownloadListener;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
* @author jiaxun
*/
public class FileDownloader {

private static final int THREAD_COUNT = 3;

private int threadCount;
private String downloadUrl;
private DownloadListener downloadListener;
private ConnectionManager connectionManager;
private String savePath;

public FileDownloader(String downloadUrl, String savePath) {
this.downloadUrl = downloadUrl;
this.savePath = savePath;
this.threadCount = THREAD_COUNT;
}

public void execute() {
Connection connection = null;
RandomAccessFile out = null;
try {
connection = connectionManager.open(downloadUrl);
int length = connection.getContentLength();
connection.close();

int downloadOffset = 0;
List<DownloadThread> threadList = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
DownloadThread thread = new DownloadThread(connectionManager.open(downloadUrl), downloadOffset, downloadOffset + (i + 1) * (length / threadCount));
threadList.add(thread);
thread.start();
downloadOffset = (i + 1) * (length / threadCount) + 1;
}
if (downloadOffset < length) {
DownloadThread thread = new DownloadThread(connectionManager.open(downloadUrl), downloadOffset, length - 1);
threadList.add(thread);
thread.start();
}

for (DownloadThread thread : threadList) {
thread.join();
}

File file = new File(savePath);
out = new RandomAccessFile(file, "rwd");
for (DownloadThread thread : threadList) {
out.seek(thread.getStartPos());
out.write(thread.getDownloadByte());
}

if (downloadListener != null) {
downloadListener.notifyFinished();
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void setConnectionManager(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}

public void setDownloadListener(DownloadListener downloadListener) {
this.downloadListener = downloadListener;
}

public DownloadListener getDownloadListener() {
return this.downloadListener;
}

public void setThreadCount(int threadCount) {
this.threadCount = threadCount;
}

}
16 changes: 16 additions & 0 deletions group26/89460886/src/week03/source/download/api/Connection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package coding.coderising.download.api;

import java.io.IOException;

/**
* @author jiaxun
*/
public interface Connection {

byte[] read(int startPos, int endPos) throws IOException;

int getContentLength();

void close();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package coding.coderising.download.api;

/**
* @author jiaxun
*/
public class ConnectionException extends Exception {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package coding.coderising.download.api;

/**
* @author jiaxun
*/
public interface ConnectionManager {

Connection open(String url) throws ConnectionException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package coding.coderising.download.api;

/**
* @author jiaxun
*/
public interface DownloadListener {

void notifyFinished();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package coding.coderising.download.impl;

import coding.coderising.download.api.Connection;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

/**
* @author jiaxun
*/
public class ConnectionImpl implements Connection {

private HttpURLConnection urlConnection;

public ConnectionImpl(HttpURLConnection urlConnection) {
this.urlConnection = urlConnection;
}

@Override
public byte[] read(int startPos, int endPos) throws IOException {
urlConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
InputStream input = urlConnection.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
int length;
byte[] byteArray = new byte[1024];
while ((length = input.read(byteArray)) != -1) {
output.write(byteArray, 0, length);
}
return output.toByteArray();
}

@Override
public int getContentLength() {
return urlConnection.getContentLength();
}

@Override
public void close() {
urlConnection.disconnect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package coding.coderising.download.impl;

import coding.coderising.download.api.Connection;
import coding.coderising.download.api.ConnectionException;
import coding.coderising.download.api.ConnectionManager;

import java.net.HttpURLConnection;
import java.net.URL;

/**
* @author jiaxun
*/
public class ConnectionManagerImpl implements ConnectionManager {

@Override
public Connection open(String urlString) throws ConnectionException {

try {
URL url = new URL(urlString);
return new ConnectionImpl((HttpURLConnection) url.openConnection());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

}
Loading

0 comments on commit 82a3fbd

Please sign in to comment.