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 #26 from jiaxun1990/master
jiaxun1990 upload week03 download commit
- Loading branch information
Showing
11 changed files
with
827 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
group26/89460886/src/week03/source/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 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
101
group26/89460886/src/week03/source/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,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
16
group26/89460886/src/week03/source/download/api/Connection.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,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(); | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group26/89460886/src/week03/source/download/api/ConnectionException.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,10 @@ | ||
package coding.coderising.download.api; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public class ConnectionException extends Exception { | ||
|
||
|
||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group26/89460886/src/week03/source/download/api/ConnectionManager.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,10 @@ | ||
package coding.coderising.download.api; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public interface ConnectionManager { | ||
|
||
Connection open(String url) throws ConnectionException; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group26/89460886/src/week03/source/download/api/DownloadListener.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,10 @@ | ||
package coding.coderising.download.api; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public interface DownloadListener { | ||
|
||
void notifyFinished(); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
group26/89460886/src/week03/source/download/impl/ConnectionImpl.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,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(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
group26/89460886/src/week03/source/download/impl/ConnectionManagerImpl.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,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; | ||
} | ||
|
||
} |
Oops, something went wrong.