-
Notifications
You must be signed in to change notification settings - Fork 641
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 #38 from kwy126/master
第三次作业,包括针对linklist的数据结构操作,以及多线程下载
- Loading branch information
Showing
12 changed files
with
1,082 additions
and
0 deletions.
There are no files selected for viewing
414 changes: 414 additions & 0 deletions
414
group03/58555264/src/main/java/com/circle/collection/LinkedListV2.java
Large diffs are not rendered by default.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
group03/58555264/src/main/java/com/circle/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,54 @@ | ||
package com.circle.download; | ||
|
||
import com.circle.download.api.Connection; | ||
import com.circle.download.api.ConnectionException; | ||
import com.circle.download.api.DownloadListener; | ||
|
||
import java.io.*; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
public class DownloadThread extends Thread { | ||
|
||
private Connection conn; | ||
private int startPos; | ||
private int endPos; | ||
static int threadFinished = 0; | ||
|
||
private DownloadListener listener; | ||
private int threadNum; | ||
|
||
|
||
public DownloadThread(Connection conn, int startPos, int endPos, DownloadListener listener, int threadNum) { | ||
|
||
this.conn = conn; | ||
this.startPos = startPos; | ||
this.endPos = endPos; | ||
this.listener = listener; | ||
this.threadNum = threadNum; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
this.conn.read(startPos, endPos); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} catch (ConnectionException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
synchronized (this) { | ||
threadFinished++; | ||
if (threadFinished == threadNum) { | ||
listener.notifyFinished(); | ||
} | ||
|
||
} | ||
|
||
} | ||
System.out.println("线程:" + Thread.currentThread().getId() + " , startPos:" + startPos + ",endPos:" + endPos); | ||
|
||
} | ||
} | ||
|
106 changes: 106 additions & 0 deletions
106
group03/58555264/src/main/java/com/circle/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,106 @@ | ||
package com.circle.download; | ||
|
||
import com.circle.download.api.Connection; | ||
import com.circle.download.api.ConnectionException; | ||
import com.circle.download.api.ConnectionManager; | ||
import com.circle.download.api.DownloadListener; | ||
import com.circle.download.impl.ConnectionManagerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
public class FileDownloader { | ||
private String url; | ||
private DownloadListener listener; | ||
private ConnectionManager cm; | ||
private int threadNum; | ||
|
||
public FileDownloader(String url, int threadNum) { | ||
this.threadNum = threadNum; | ||
this.url = url; | ||
} | ||
|
||
public DownloadListener getListener() { | ||
return listener; | ||
} | ||
|
||
public void setListener(DownloadListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
/** | ||
* 具体的实现思路: | ||
* 1、需要调用ConnectionManager的open方法打开连接,然后通过Connection.getConnection.getContentLength方法获得文件的长度 | ||
* 2、至少启动3个线程下载,注意每个线程需要调用ConnectionManager的open方法 | ||
* 然后调用read方法,read方法中有读取文件的开始位置和结束位置的参数,返回值是byte[]数组 | ||
* 3、把byte数组写入到文件中 | ||
* 4、所有的线程都下载完成以后,需要调用listener的notifiedFinished方法 | ||
*/ | ||
public void execute() { | ||
Connection conn = null; | ||
int[] startPos = new int[threadNum]; | ||
int[] endPos = new int[threadNum]; | ||
RandomAccessFile raf = null; | ||
|
||
try { | ||
String[] ss = url.split("/"); | ||
Thread[] threads = new Thread[threadNum]; | ||
|
||
File file = new File(ss[ss.length - 1]); | ||
|
||
|
||
cm = ConnectionManagerFactory.getManager(file); | ||
conn = cm.open(this.url); | ||
int length = conn.getContentLength(); | ||
System.out.println("length:" + length); | ||
|
||
|
||
raf = new RandomAccessFile(file, "rwd"); | ||
raf.setLength(length); | ||
|
||
for (int i = 0; i < threadNum; i++) { | ||
int size = i * (length / threadNum); | ||
startPos[i] = size; | ||
|
||
if (i == threadNum - 1) { | ||
endPos[i] = length; | ||
} else { | ||
size = (i + 1) * (length / threadNum); | ||
endPos[i] = size - 1; | ||
} | ||
|
||
threads[i] = new DownloadThread(cm.open(this.url), startPos[i], endPos[i],listener,threadNum); | ||
threads[i].start(); | ||
} | ||
} catch (ConnectionException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
if (raf != null) { | ||
try { | ||
raf.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
String url2 = "http://hiphotos.baidu.com/240728057/pic/item/6a50e38242aad8f60cf4d2b3.jpg"; | ||
String url = "http://bcbang.oss-cn-qingdao.aliyuncs.com/TLAB-in-Eden-memory.png"; | ||
String url3 = "http://www.cnblogs.com/iwideal/p/6045118.html"; | ||
FileDownloader downloader = new FileDownloader(url2, 2); | ||
downloader.execute(); | ||
|
||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
group03/58555264/src/main/java/com/circle/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,31 @@ | ||
package com.circle.download.api; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
public interface Connection { | ||
|
||
/** | ||
* 给定开始和结束位置,读取数据,返回值是字节数组 | ||
* | ||
* @param startPos 开始位置,从0开始 | ||
* @param endPos 结束位置 | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public void read(int startPos, int endPos) throws IOException, ConnectionException; | ||
|
||
/** | ||
* 得到数据内容的长度 | ||
* @return | ||
*/ | ||
public int getContentLength(); | ||
|
||
/** | ||
* 关闭连接 | ||
*/ | ||
public void close(); | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
group03/58555264/src/main/java/com/circle/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,24 @@ | ||
package com.circle.download.api; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
public class ConnectionException extends Exception { | ||
|
||
public ConnectionException() { | ||
|
||
} | ||
|
||
/** | ||
* 描述异常 | ||
* @param msg | ||
*/ | ||
public ConnectionException(String msg) { | ||
super(msg); | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
|
12 changes: 12 additions & 0 deletions
12
group03/58555264/src/main/java/com/circle/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,12 @@ | ||
package com.circle.download.api; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.sql.*; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
public interface ConnectionManager { | ||
public Connection open(String url) throws ConnectionException, IOException; | ||
} |
8 changes: 8 additions & 0 deletions
8
group03/58555264/src/main/java/com/circle/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,8 @@ | ||
package com.circle.download.api; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
public interface DownloadListener { | ||
public void notifyFinished(); | ||
} |
77 changes: 77 additions & 0 deletions
77
group03/58555264/src/main/java/com/circle/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,77 @@ | ||
package com.circle.download.impl; | ||
|
||
import com.circle.download.api.Connection; | ||
import com.circle.download.api.ConnectionException; | ||
|
||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
class ConnectionImpl implements Connection { | ||
private String url; | ||
private HttpURLConnection conn; | ||
private File file; | ||
|
||
public ConnectionImpl(String url, File file) throws IOException, ConnectionException { | ||
this.file = file; | ||
this.url = url; | ||
this.conn = init(); | ||
} | ||
|
||
private HttpURLConnection init() throws IOException, ConnectionException { | ||
URL httpURL = new URL(url); | ||
this.conn = (HttpURLConnection) httpURL.openConnection(); | ||
|
||
this.conn.setRequestMethod("GET"); | ||
this.conn.setRequestProperty("Charset", "UTF-8"); | ||
return conn; | ||
} | ||
|
||
@Override | ||
public void read(int startPos, int endPos) throws IOException, ConnectionException { | ||
URL httpURL = new URL(url); | ||
this.conn = (HttpURLConnection) httpURL.openConnection(); | ||
|
||
this.conn.setRequestMethod("GET"); | ||
this.conn.setRequestProperty("Charset", "UTF-8"); | ||
this.conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); | ||
|
||
if (this.getContentLength() < 0) { | ||
throw new ConnectionException("连接内容小于0"); | ||
} | ||
|
||
int code = conn.getResponseCode(); | ||
RandomAccessFile raf = null; | ||
try{ | ||
InputStream is = conn.getInputStream(); | ||
raf = new RandomAccessFile(this.file, "rwd"); | ||
raf.seek(startPos); | ||
|
||
byte[] bs = new byte[1024]; | ||
int len = -1; | ||
|
||
while ((len = is.read(bs)) != -1) { | ||
raf.write(bs, 0, len); | ||
} | ||
}finally { | ||
raf.close(); | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
@Override | ||
public int getContentLength() { | ||
return this.conn.getContentLength(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// this.conn. | ||
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerFactory.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,31 @@ | ||
package com.circle.download.impl; | ||
|
||
import com.circle.download.api.Connection; | ||
import com.circle.download.api.ConnectionManager; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/11. | ||
*/ | ||
public class ConnectionManagerFactory { | ||
|
||
private static volatile ConnectionManager manager = null; | ||
|
||
private ConnectionManagerFactory() { | ||
|
||
} | ||
|
||
public static ConnectionManager getManager(File file) { | ||
|
||
if (manager == null) { | ||
synchronized (ConnectionManagerFactory.class) { | ||
if (manager == null) { | ||
manager = new ConnectionManagerImpl(file); | ||
} | ||
} | ||
} | ||
|
||
return manager; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
group03/58555264/src/main/java/com/circle/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,31 @@ | ||
package com.circle.download.impl; | ||
|
||
import com.circle.download.api.Connection; | ||
import com.circle.download.api.ConnectionException; | ||
import com.circle.download.api.ConnectionManager; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
|
||
/** | ||
* Created by keweiyang on 2017/3/10. | ||
*/ | ||
class ConnectionManagerImpl implements ConnectionManager { | ||
private File file; | ||
|
||
public ConnectionManagerImpl(File file) { | ||
|
||
this.file = file; | ||
|
||
} | ||
|
||
@Override | ||
public Connection open(String url) throws ConnectionException, IOException { | ||
return new ConnectionImpl(url, this.file); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.