From b6913f9bf9502c651d1b36e0e8316402605b913a Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 25 Mar 2017 22:57:48 +0800 Subject: [PATCH 1/8] Task3 --- group22/2622819383/Task3/LinkedList.java | 190 ++++++++++++++++++ .../Task3/download/DownloadThread.java | 20 ++ .../Task3/download/FileDownloader.java | 73 +++++++ .../Task3/download/FileDownloaderTest.java | 59 ++++++ .../Task3/download/api/Connection.java | 23 +++ .../download/api/ConnectionException.java | 5 + .../Task3/download/api/ConnectionManager.java | 10 + .../Task3/download/api/DownloadListener.java | 5 + .../Task3/download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ 10 files changed, 427 insertions(+) create mode 100644 group22/2622819383/Task3/LinkedList.java create mode 100644 group22/2622819383/Task3/download/DownloadThread.java create mode 100644 group22/2622819383/Task3/download/FileDownloader.java create mode 100644 group22/2622819383/Task3/download/FileDownloaderTest.java create mode 100644 group22/2622819383/Task3/download/api/Connection.java create mode 100644 group22/2622819383/Task3/download/api/ConnectionException.java create mode 100644 group22/2622819383/Task3/download/api/ConnectionManager.java create mode 100644 group22/2622819383/Task3/download/api/DownloadListener.java create mode 100644 group22/2622819383/Task3/download/impl/ConnectionImpl.java create mode 100644 group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java diff --git a/group22/2622819383/Task3/LinkedList.java b/group22/2622819383/Task3/LinkedList.java new file mode 100644 index 0000000000..3c4d70b67c --- /dev/null +++ b/group22/2622819383/Task3/LinkedList.java @@ -0,0 +1,190 @@ +public class LinkedList { + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + int times = theSize; + int index = 0; + while (0 < --times) + add(index++, removeLast()); + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf() { + int times = theSize / 2; + while (0 < times--) + removeFirst(); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length) { + Node head = get(i).pred(); //删除(head, tail)之间元素 删除[i, i + length - 1]之间元素 + Node tail = get(i + length - 1).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= length; + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * list = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list) { + Iterator itSelf = iterator(); + Iterator itList = list.iterator(); + int[] ret = new int[list.size()]; + + int i = 0; //list中元素的值,代表当前列表中要取出元素的秩 + lastI = 0;//上一次取出元素的秩 + moveTimes = 0; + value = itSelf.next(); + index = 0;//要返回的数组中元素的秩 + + while (itList.hasNext()) { + i = itList.next(); + if (theSize <= i) throw new IndexOutOfBoundsException(); + + moveTimes = i - lastI; + while (0 < moveTimes--) + value = itSelf.next(); + + ret[index++] = value; + lastI = i; + } + + return ret; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + //返回与e相等的元素的秩;如果查找失败则返回-1 + private int find(Object e) { + Iterator it = iterator(); + int i = -1; //要返回的元素的秩 + Object value = null; + + while (it.hasNext()) { + value = it.next(); + i++; + if (value == e) return i; + if (e < value) return -1; + } + + return -1; + } + + public void subtract(LinkedList list) { + Iterator it = list.iterator(); + Object value = null; + int i = -1; + + while (it.hasNext()) { + value = it.next(); + i = find(value); + + //删去重复元素 + while (0 <= i) { + remove(i); + i = find(value); + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node current = header.succ(); + Node next = current; + int removedNum = 0; + + while ((next = next.succ()) != trailer) { + if (current.data() == next.data()) { + removedNum++; + } else { + current.succ = next; + next.pred = current; + current = next; + } + } + theSize -= removedNum; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + //[low, min]U[max, end] + + + public void removeRange(int min, int max) { + //删去(i, j] + int i = 0, j = 0; + Iterator it = iterator(); + while (it.hasNext()) { + Object value = it.next(); + if (value <= min) i++; + if (value < max) j++; + else break; //if(max <= value) break; + } + + Node head = get(i); + Node tail = get(j).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= (j - i); + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + //交集:属于A且属于B的元素的合集 + public LinkedList intersection(LinkedList list) { + LinkedList ret = new LinkedList(); + Iterator it = iterator(); + Iterator itList = list.iterator(); + Object value1 = null, value2 = null; + + if (it.hasNext() && itList.hasNext()) { + value1 = it.next(); + value2 = itList.next(); + } + //用null作为遍历完毕的标志 + //循环结束标志:其中一个LinkedList已经遍历完毕 + while (value1 != null && value2 != null) { + if (value1 < value2) value1 = it.hasNext() ? it.next() : null; + else if (value2 < value1) value2 = itList.hasNext() ? itList.next() : null; + else { + ret.add(value1); + value1 = it.hasNext() ? it.next() : null; + value2 = itList.hasNext() ? itList.next() : null; + } + } + return ret; + } +} diff --git a/group22/2622819383/Task3/download/DownloadThread.java b/group22/2622819383/Task3/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/group22/2622819383/Task3/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group22/2622819383/Task3/download/FileDownloader.java b/group22/2622819383/Task3/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/group22/2622819383/Task3/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException 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; + } + +} diff --git a/group22/2622819383/Task3/download/FileDownloaderTest.java b/group22/2622819383/Task3/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/group22/2622819383/Task3/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.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("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group22/2622819383/Task3/download/api/Connection.java b/group22/2622819383/Task3/download/api/Connection.java new file mode 100644 index 0000000000..58cbdd995d --- /dev/null +++ b/group22/2622819383/Task3/download/api/Connection.java @@ -0,0 +1,23 @@ + + +import java.io.IOException; + +public interface Connection { + /** + * 缁欏畾寮濮嬪拰缁撴潫浣嶇疆锛 璇诲彇鏁版嵁锛 杩斿洖鍊兼槸瀛楄妭鏁扮粍 + * @param startPos 寮濮嬩綅缃紝 浠0寮濮 + * @param endPos 缁撴潫浣嶇疆 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 寰楀埌鏁版嵁鍐呭鐨勯暱搴 + * @return + */ + public int getContentLength(); + + /** + * 鍏抽棴杩炴帴 + */ + public void close(); +} diff --git a/group22/2622819383/Task3/download/api/ConnectionException.java b/group22/2622819383/Task3/download/api/ConnectionException.java new file mode 100644 index 0000000000..4021776925 --- /dev/null +++ b/group22/2622819383/Task3/download/api/ConnectionException.java @@ -0,0 +1,5 @@ + + +public class ConnectionException extends Exception { + +} diff --git a/group22/2622819383/Task3/download/api/ConnectionManager.java b/group22/2622819383/Task3/download/api/ConnectionManager.java new file mode 100644 index 0000000000..d27612cb66 --- /dev/null +++ b/group22/2622819383/Task3/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ + + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group22/2622819383/Task3/download/api/DownloadListener.java b/group22/2622819383/Task3/download/api/DownloadListener.java new file mode 100644 index 0000000000..fea4b7b363 --- /dev/null +++ b/group22/2622819383/Task3/download/api/DownloadListener.java @@ -0,0 +1,5 @@ + + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group22/2622819383/Task3/download/impl/ConnectionImpl.java b/group22/2622819383/Task3/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/group22/2622819383/Task3/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java b/group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} From 1667c24c4af2f7160f617f0deb5ef6a8843e70fc Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 25 Mar 2017 22:59:00 +0800 Subject: [PATCH 2/8] change about Task1/LinkedList.java --- group22/2622819383/Task1/LinkedList.java | 138 ++++++++++++++++++++--- 1 file changed, 125 insertions(+), 13 deletions(-) diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 81c1db409c..57dfdef603 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -129,8 +129,11 @@ public Node insertAsSucc(Object data) { * 把该链表逆置 * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ - public void reverse(){ - + public void reverse(){ + int times = theSize; + int index = 0; + while (0 < --times) + add(index++, removeLast()); } /** @@ -139,8 +142,10 @@ public void reverse(){ * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 */ - public void removeFirstHalf(){ - + public void removeFirstHalf(){ + int times = theSize / 2; + while (0 < times--) + removeFirst(); } /** @@ -148,19 +153,46 @@ public void removeFirstHalf(){ * @param i * @param length */ - public void remove(int i, int length){ + public void remove(int i, int length){ + Node head = get(i).pred(); //删除(head, tail)之间元素 删除[i, i + length - 1]之间元素 + Node tail = get(i + length - 1).succ(); + head.succ = tail; + tail.pred = head; + theSize -= length; } /** * 假定当前链表和list均包含已升序排列的整数 * 从当前链表中取出那些list所指定的元素 * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 + * list = 1->3->4->6 * 返回的结果应该是[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ - return null; + public int[] getElements(LinkedList list){ + Iterator itSelf = iterator(); + Iterator itList = list.iterator(); + int[] ret = new int[list.size()]; + + int i = 0; //list中元素的值,代表当前列表中要取出元素的秩 + lastI = 0;//上一次取出元素的秩 + moveTimes = 0; + value = itSelf.next(); + index = 0;//要返回的数组中元素的秩 + + while (itList.hasNext()) { + i = itList.next(); + if (theSize <= i) throw new IndexOutOfBoundsException(); + + moveTimes = i - lastI; + while (0 < moveTimes--) + value = itSelf.next(); + + ret[index++] = value; + lastI = i; + } + + return ret; } /** @@ -169,9 +201,37 @@ public static int[] getElements(LinkedList list){ * @param list */ + //返回与e相等的元素的秩;如果查找失败则返回-1 + private int find(Object e) { + Iterator it = iterator(); + int i = -1; //要返回的元素的秩 + Object value = null; + + while (it.hasNext()) { + value = it.next(); + i++; + if (value == e) return i; + if (e < value) return -1; + } - public void subtract(LinkedList list){ - + return -1; + } + + public void subtract(LinkedList list){ + Iterator it = list.iterator(); + Object value = null; + int i = -1; + + while (it.hasNext()) { + value = it.next(); + i = find(value); + + //删去重复元素 + while (0 <= i) { + remove(i); + i = find(value); + } + } } /** @@ -179,7 +239,20 @@ public void subtract(LinkedList list){ * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + Node current = header.succ(); + Node next = current; + int removedNum = 0; + + while ((next = next.succ()) != trailer) { + if (current.data() == next.data()) { + removedNum++; + } else { + current.succ = next; + next.pred = current; + current = next; + } + } + theSize -= removedNum; } /** @@ -188,7 +261,26 @@ public void removeDuplicateValues(){ * @param min * @param max */ + //[low, min]U[max, end] + + public void removeRange(int min, int max){ + //删去(i, j] + int i = 0, j = 0; + Iterator it = iterator(); + while (it.hasNext()) { + Object value = it.next(); + if (value <= min) i++; + if (value < max) j++; + else break; //if(max <= value) break; + } + + Node head = get(i); + Node tail = get(j).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= (j - i); } @@ -197,7 +289,27 @@ public void removeRange(int min, int max){ * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 * @param list */ - public LinkedList intersection( LinkedList list){ - return null; + //交集:属于A且属于B的元素的合集 + public LinkedList intersection(LinkedList list){ + LinkedList ret = new LinkedList(); + Iterator it = iterator(); + Iterator itList = list.iterator(); + Object value1 = null, value2 = null; + + if (it.hasNext() && itList.hasNext()) { + value1 = it.next(); + value2 = itList.next(); + } + + while (value1 != null && value2 != null) { + if (value1 < value2) value1 = it.hasNext() ? it.next() : null; + else if (value2 < value1) value2 = itList.hasNext() ? itList.next() : null; + else { + ret.add(value1); + value1 = it.hasNext() ? it.next() : null; + value2 = itList.hasNext() ? itList.next() : null; + } + } + return ret; } } From a1fe085fd308355143fcba837df997579df42f0c Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sun, 26 Mar 2017 17:47:21 +0800 Subject: [PATCH 3/8] don't remember change what --- group22/2622819383/Task3/LinkedList.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/group22/2622819383/Task3/LinkedList.java b/group22/2622819383/Task3/LinkedList.java index 3c4d70b67c..5ec46b348f 100644 --- a/group22/2622819383/Task3/LinkedList.java +++ b/group22/2622819383/Task3/LinkedList.java @@ -4,9 +4,9 @@ public class LinkedList { * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse() { - int times = theSize; + int times = theSize - 1; //第一个元素自然移动到最后,所以只需进行theSize - 1次操作 int index = 0; - while (0 < --times) + while (0 < times--) add(index++, removeLast()); } @@ -68,7 +68,7 @@ public int[] getElements(LinkedList list) { return ret; } - + /** * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 * 从当前链表中中删除在list中出现的元素 From ac0dc015bdcabc829bc9f932264db5187ad1878e Mon Sep 17 00:00:00 2001 From: laoheihei Date: Tue, 28 Mar 2017 22:30:18 +0800 Subject: [PATCH 4/8] without listener --- .../2622819383/Task3/download/Connection.java | 60 +++++++++++ .../Task3/download/DownloadThread.java | 39 ++++++-- .../Task3/download/FileDownloader.java | 99 +++++++++---------- 3 files changed, 134 insertions(+), 64 deletions(-) create mode 100644 group22/2622819383/Task3/download/Connection.java diff --git a/group22/2622819383/Task3/download/Connection.java b/group22/2622819383/Task3/download/Connection.java new file mode 100644 index 0000000000..953db2e43b --- /dev/null +++ b/group22/2622819383/Task3/download/Connection.java @@ -0,0 +1,60 @@ +import java.net.URL; +import java.net.HttpURLConnection; +import java.io.IOException; +import java.io.InputStream; +import java.io.ByteArrayOutputStream; + +public class Connection { + + private URL url; + private HttpURLConnection conn; + private String urlName; + + public String getURLName() { + return urlName; + } + + //创建一个Connection对象,相应建立了一个HttpURLConnection连接 + public Connection(String url) { + urlName = url; + try { + this.url = new URL(urlName); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void initConnection() { + try { + conn = (HttpURLConnection)url.openConnection(); //throws IOException + } catch (IOException e) { + e.printStackTrace(); + } + } + + //从服务器下载startPos-endPos字节范围的资源内容到一个字节数组 + public byte[] read(int startPos, int endPos) throws IOException { + //Range: 用于客户端到服务器端的请求,可通过该字段指定下载文件的某一段大小,及其单位。 + initConnection(); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream in = conn.getInputStream(); //throws IOException + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + byte[] buf = new byte[1024]; + int hasRead = 0; + while ((hasRead = in.read(buf)) != -1) { + out.write(buf, 0, hasRead); + } + return out.toByteArray(); + } + + public int getContentLength() { + initConnection(); + return conn.getContentLength(); + } + + public void close() { + conn.disconnect(); + } +} \ No newline at end of file diff --git a/group22/2622819383/Task3/download/DownloadThread.java b/group22/2622819383/Task3/download/DownloadThread.java index 900a3ad358..a8c890b709 100644 --- a/group22/2622819383/Task3/download/DownloadThread.java +++ b/group22/2622819383/Task3/download/DownloadThread.java @@ -1,20 +1,39 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; - +import java.io.IOException; +import java.net.HttpURLConnection; +import java.io.RandomAccessFile; +import java.util.concurrent.locks.*; public class DownloadThread extends Thread{ Connection conn; int startPos; int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - + String targetURL; + final ReentrantLock lock = new ReentrantLock(); + volatile int i = 0; + + public DownloadThread(Connection conn, int startPos, int endPos, String targetURL) { this.conn = conn; this.startPos = startPos; - this.endPos = endPos; + this.endPos = endPos; + this.targetURL = targetURL; } - public void run(){ - + + public void run() { + System.out.println("线程" + getName() + "startPos:" + startPos + "; endPos:" + endPos); + lock.lock(); + + try { + + RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); + byte[] buf = conn.read(startPos, endPos); + raf.seek(startPos); + raf.write(buf); + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + lock.unlock(); + System.out.println("线程" + this.getName() + "下载完成."); + } } diff --git a/group22/2622819383/Task3/download/FileDownloader.java b/group22/2622819383/Task3/download/FileDownloader.java index c3c8a3f27d..cc8049b70a 100644 --- a/group22/2622819383/Task3/download/FileDownloader.java +++ b/group22/2622819383/Task3/download/FileDownloader.java @@ -1,61 +1,48 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - this.url = _url; - +import java.util.List; +import java.util.ArrayList; +import java.io.RandomAccessFile; +import java.io.IOException; +public class FileDownloader { + //DownloadListener listener; + String url; + + + public FileDownloader(String url) { + this.url = url; } - public void execute(){ - // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 - // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 - // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 - // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 - // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 - // 鍏蜂綋鐨勫疄鐜版濊矾锛 - // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 - // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 - // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 - // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 - // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 - - // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - - } catch (ConnectionException e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - - - + public void execute() throws IOException { + + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + conn = new Connection(url); + int length = conn.getContentLength(); + String targetURL = "E:/" + url.substring(url.lastIndexOf("/") + 1); + RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); + raf.setLength(length); + raf.close(); + + for (int i = 0; i < 3; i++) { + int part = length / 3; + int start = i * part; + int end = (i == 2) ? length - 1 : (i + 1) * part - 1; + DownloadThread t = new DownloadThread(conn, start, end, targetURL); + t.start(); + } } - + /* public void setListener(DownloadListener listener) { this.listener = listener; } @@ -69,5 +56,9 @@ public void setConnectionManager(ConnectionManager ucm){ public DownloadListener getListener(){ return this.listener; } + */ + public static void main(String[] args) throws IOException { + new FileDownloader("http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png").execute(); + } } From bb9b485b70c625b1c31aeb3a65a5d6dd19f6f3ba Mon Sep 17 00:00:00 2001 From: laoheihei Date: Tue, 28 Mar 2017 22:36:41 +0800 Subject: [PATCH 5/8] delete uselessfile --- .../Struts2\345\256\236\346\210\230.txt" | 125 ------------------ .../Task3/download/FileDownloaderTest.java | 59 --------- .../Task3/download/api/Connection.java | 23 ---- .../download/api/ConnectionException.java | 5 - .../Task3/download/api/ConnectionManager.java | 10 -- .../Task3/download/api/DownloadListener.java | 5 - .../Task3/download/impl/ConnectionImpl.java | 27 ---- .../download/impl/ConnectionManagerImpl.java | 15 --- 8 files changed, 269 deletions(-) delete mode 100644 "group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" delete mode 100644 group22/2622819383/Task3/download/FileDownloaderTest.java delete mode 100644 group22/2622819383/Task3/download/api/Connection.java delete mode 100644 group22/2622819383/Task3/download/api/ConnectionException.java delete mode 100644 group22/2622819383/Task3/download/api/ConnectionManager.java delete mode 100644 group22/2622819383/Task3/download/api/DownloadListener.java delete mode 100644 group22/2622819383/Task3/download/impl/ConnectionImpl.java delete mode 100644 group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java diff --git "a/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" "b/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" deleted file mode 100644 index e6997ab49a..0000000000 --- "a/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" +++ /dev/null @@ -1,125 +0,0 @@ -Struts2瀹炴垬 -----------------------------chapter1 Struts2锛氱幇浠eb妗嗘灦 -涓銆乄eb搴旂敤绋嬪簭锛氬揩閫熷涔 - HTTP锛氬熀浜庢枃鏈 + 鏃犵姸鎬 - Java Servlet锛氳В鍐冲簳灞傚鎴/鏈嶅姟鍣ㄩ棶棰樸 - 灏咹TTP鍏紑缁橨ava璇█锛氭帴鍙桯TTP璇锋眰瀵硅薄锛屾鏌ユ暟鎹紝鎵ц鍚庡彴閫昏緫锛屽悜瀹㈡埛杩斿洖鍝嶅簲銆 - 楂樼骇鍔熻兘锛氫細璇濇満鍒躲 - 妗嗘灦锛氬叧娉ㄥ簲鐢ㄧ▼搴忕骇鍒殑闂銆 - HTTP瀛楃涓插埌Java鏁版嵁绫诲瀷鐨勮浆鎹紱 - 楠岃瘉鏁版嵁锛氬瓧绗︿覆鍚堟硶锛涜浆鎹㈠悗鐨刯ava绫诲瀷鍚堟硶锛 - 璁块棶涓氬姟閫昏緫锛氱壒瀹氬伐浣滄祦锛 - 璁块棶鏁版嵁灞傦細鐗瑰畾宸ヤ綔娴侊紱 - 鍛堢幇琛ㄧず灞(HTML绛)锛 - 鎻愪緵鍥介檯鍖栧拰鏈湴鍖栨敮鎸併 - -浜屻乄eb搴旂敤绋嬪簭妗嗘灦 - - 妗嗘灦鑷姩鍖栦簡甯歌浠诲姟锛 - 妗嗘灦鎻愪緵浜嗘灦鏋勮В鍐虫柟妗堬紱 - -涓夈丼truts2妗嗘灦 - - 1.Struts2姒傝锛歁VC妯″紡 - Browser鈥斺旇姹傗斺>鎺у埗鍣(FilterDisPatcher)鈥斺旇皟鐢ㄥ姩浣溾斺>妯″瀷鈥斺旈夋嫨缁撴灉鈥斺>瑙嗗浘鈥斺斿憟鐜伴〉闈⑩斺>Browser - //璇锋眰URL鍒板姩浣滅殑鏄犲皠锛氱敱XML閰嶇疆鏂囦欢鎴杍ava娉ㄨВ瀹屾垚 - //鍔ㄤ綔锛氬皢涓氬姟閫昏緫璋冪敤灏佽鍒颁竴涓崟鐙殑宸ヤ綔鍗曞厓 + 鍔ㄤ綔鏄竴涓暟鎹紶杈撶殑鍦烘墍銆 - 2.Struts宸ヤ綔鍘熺悊 - 璋冪敤鍔ㄤ綔鈥斺旀嫤鎴櫒鈥斺>鍔ㄤ綔鈥斺旇皟鐢ㄧ粨鏋溾斺>缁撴灉鈥斺旀嫤鎴櫒鈥斺>鍙戝洖缁撴灉 - | | - OGNL OGNL - | | - __________________________________ - | ActionContext(ThreadLocal) | - |ValueStack銆佽姹傘佷細璇濄... | - 鎷︽埅鍣ㄥ畬鎴愪换鍔★細鏃ュ織銆佹暟鎹獙璇併佺被鍨嬭浆鎹€佹枃浠朵笂浼犵瓑銆 - ValueStack锛氫繚绠¤姹傚垱绔嬬浉鍏崇殑鎵鏈夋暟鎹殑涓涓瓨鍌ㄥ尯鍩熴 - OGNL锛氳闂瓨鍌ㄥ湪涓ぎ瀛樺偍搴揤alueStack涓暟鎹殑宸ュ叿锛堣〃杈惧紡璇█锛夈 - ValueStack涓殑鏁版嵁璺熺潃澶勭悊璇锋眰缁忚繃鎵鏈夐樁娈碉紝璐┛妗嗘灦鐨勬暣涓繃绋嬨傝繖鏄洜涓篤alueStack瀛樺湪鍦ㄤ竴涓嚎绋嬫湰鍦板璞★紙ThreadLocal锛変腑銆 - ActionContext: 鍖呭惈鎵鏈夌殑鏁版嵁锛岃繖浜涙暟鎹瀯鎴愪簡鍔ㄤ綔鎵ц鐨勭幆澧冦 - --------------------------chapter2 鍒濊瘑Struts -涓銆佸0鏄庢ф灦鏋勨斺斿簲鐢ㄧ▼搴忛厤缃細鍦ㄨ緝楂樼骇鍒弿杩板簲鐢ㄧ▼搴忕殑鏋舵瀯缁勪欢 - - 澹版槑閭d簺瀵硅薄浣滀负搴旂敤绋嬪簭鐨勫姩浣(action)銆佺粨鏋(result)銆佷互鍙婃嫤鎴櫒(interceptor) - 澹版槑杩囩▼涓昏鍖呮嫭鍒跺畾鍝釜Java绫诲疄鐜板摢涓帴鍙c - 涓ょ閰嶇疆 - 澹版槑鏋舵瀯鐨勪袱绉嶆柟寮 - 鏅鸿兘榛樿鍊 - -浜屻佺畝鍗曠殑HelloWorld绀轰緥 - - 閮ㄧ讲绀轰緥搴旂敤绋嬪簭 - 鎺㈢储HelloWorld搴旂敤绋嬪簭 - -涓夈佷娇鐢ㄦ敞瑙g殑HelloWorld - --------------------------chapter3 浣跨敤Struts2鍔ㄤ綔 -涓銆丼truts鍔ㄤ綔绠浠 -浜屻佹墦鍖呭姩浣 -涓夈佸疄鐜板姩浣 -鍥涖佸悜瀵硅薄浼犻佹暟鎹 -浜斻佹渚嬬爺绌讹細鏂囦欢涓婁紶 - --------------------------chapter4 浣跨敤鎷︽埅鍣ㄨ拷鍔犲伐浣滄祦 --------------------------chapter5 鏁版嵁杞Щ锛歄GNL鍜岀被鍨嬭浆鎹 --------------------------chapter6 鏋勫缓瑙嗗浘鈥斺旀爣绛 --------------------------chapter7 UI缁勪欢鏍囩 --------------------------chapter8 缁撴灉 --------------------------chapter9 缁ф壙Spring鍜孒ibernate/JPA --------------------------chapter10 鎺㈢储楠岃瘉妗嗘灦 --------------------------chapter11 鐞嗚В鍥介檯鍖 --------------------------chapter12 浣跨敤鎻掍欢鎵╁睍Struts2 --------------------------chapter13 鏈浣冲疄璺 --------------------------chapter14 浠庣粡鍏窼truts杩佺Щ --------------------------chapter15 楂樼骇涓婚 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/group22/2622819383/Task3/download/FileDownloaderTest.java b/group22/2622819383/Task3/download/FileDownloaderTest.java deleted file mode 100644 index 4ff7f46ae0..0000000000 --- a/group22/2622819383/Task3/download/FileDownloaderTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.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("涓嬭浇瀹屾垚锛"); - - - - } - -} diff --git a/group22/2622819383/Task3/download/api/Connection.java b/group22/2622819383/Task3/download/api/Connection.java deleted file mode 100644 index 58cbdd995d..0000000000 --- a/group22/2622819383/Task3/download/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ - - -import java.io.IOException; - -public interface Connection { - /** - * 缁欏畾寮濮嬪拰缁撴潫浣嶇疆锛 璇诲彇鏁版嵁锛 杩斿洖鍊兼槸瀛楄妭鏁扮粍 - * @param startPos 寮濮嬩綅缃紝 浠0寮濮 - * @param endPos 缁撴潫浣嶇疆 - * @return - */ - public byte[] read(int startPos,int endPos) throws IOException; - /** - * 寰楀埌鏁版嵁鍐呭鐨勯暱搴 - * @return - */ - public int getContentLength(); - - /** - * 鍏抽棴杩炴帴 - */ - public void close(); -} diff --git a/group22/2622819383/Task3/download/api/ConnectionException.java b/group22/2622819383/Task3/download/api/ConnectionException.java deleted file mode 100644 index 4021776925..0000000000 --- a/group22/2622819383/Task3/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ - - -public class ConnectionException extends Exception { - -} diff --git a/group22/2622819383/Task3/download/api/ConnectionManager.java b/group22/2622819383/Task3/download/api/ConnectionManager.java deleted file mode 100644 index d27612cb66..0000000000 --- a/group22/2622819383/Task3/download/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ - - -public interface ConnectionManager { - /** - * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/group22/2622819383/Task3/download/api/DownloadListener.java b/group22/2622819383/Task3/download/api/DownloadListener.java deleted file mode 100644 index fea4b7b363..0000000000 --- a/group22/2622819383/Task3/download/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ - - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/group22/2622819383/Task3/download/impl/ConnectionImpl.java b/group22/2622819383/Task3/download/impl/ConnectionImpl.java deleted file mode 100644 index 36a9d2ce15..0000000000 --- a/group22/2622819383/Task3/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} diff --git a/group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java b/group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index 172371dd55..0000000000 --- a/group22/2622819383/Task3/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} From 5ecdbc3a39c6e49be5a5945dd53d358af3252dd3 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Wed, 29 Mar 2017 17:50:12 +0800 Subject: [PATCH 6/8] without listener --- group22/2622819383/Task3/download/Connection.java | 6 ++++-- group22/2622819383/Task3/download/DownloadThread.java | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/group22/2622819383/Task3/download/Connection.java b/group22/2622819383/Task3/download/Connection.java index 953db2e43b..e7af266004 100644 --- a/group22/2622819383/Task3/download/Connection.java +++ b/group22/2622819383/Task3/download/Connection.java @@ -45,7 +45,9 @@ public byte[] read(int startPos, int endPos) throws IOException { int hasRead = 0; while ((hasRead = in.read(buf)) != -1) { out.write(buf, 0, hasRead); - } + } + out.close(); + in.close(); return out.toByteArray(); } @@ -55,6 +57,6 @@ public int getContentLength() { } public void close() { - conn.disconnect(); + } } \ No newline at end of file diff --git a/group22/2622819383/Task3/download/DownloadThread.java b/group22/2622819383/Task3/download/DownloadThread.java index a8c890b709..07a4222b59 100644 --- a/group22/2622819383/Task3/download/DownloadThread.java +++ b/group22/2622819383/Task3/download/DownloadThread.java @@ -20,7 +20,7 @@ public DownloadThread(Connection conn, int startPos, int endPos, String targetUR public void run() { System.out.println("线程" + getName() + "startPos:" + startPos + "; endPos:" + endPos); - lock.lock(); + try { @@ -32,7 +32,7 @@ public void run() { } catch (IOException e) { e.printStackTrace(); } - lock.unlock(); + System.out.println("线程" + this.getName() + "下载完成."); } From 156410630626662422d00810d1d819c491131f9c Mon Sep 17 00:00:00 2001 From: laoheihei Date: Wed, 29 Mar 2017 18:01:00 +0800 Subject: [PATCH 7/8] still no listener --- .../2622819383/Task3/download/Connection.java | 27 ++---- .../Task3/download/DownloadThread.java | 33 ++++---- .../Task3/download/FileDownloader.java | 84 ++++++++++--------- 3 files changed, 66 insertions(+), 78 deletions(-) diff --git a/group22/2622819383/Task3/download/Connection.java b/group22/2622819383/Task3/download/Connection.java index e7af266004..14eb68f232 100644 --- a/group22/2622819383/Task3/download/Connection.java +++ b/group22/2622819383/Task3/download/Connection.java @@ -4,21 +4,14 @@ import java.io.InputStream; import java.io.ByteArrayOutputStream; -public class Connection { - +public class Connection { private URL url; - private HttpURLConnection conn; - private String urlName; - - public String getURLName() { - return urlName; - } + private HttpURLConnection conn; //创建一个Connection对象,相应建立了一个HttpURLConnection连接 - public Connection(String url) { - urlName = url; + public Connection(String url) { try { - this.url = new URL(urlName); + this.url = new URL(url); } catch (IOException e) { e.printStackTrace(); } @@ -26,19 +19,18 @@ public Connection(String url) { public void initConnection() { try { - conn = (HttpURLConnection)url.openConnection(); //throws IOException + conn = (HttpURLConnection)url.openConnection(); } catch (IOException e) { e.printStackTrace(); } } //从服务器下载startPos-endPos字节范围的资源内容到一个字节数组 - public byte[] read(int startPos, int endPos) throws IOException { - //Range: 用于客户端到服务器端的请求,可通过该字段指定下载文件的某一段大小,及其单位。 + //Range: 用于客户端到服务器端的请求,可通过该字段指定下载文件的某一段大小,及其单位。 + public byte[] read(int startPos, int endPos) throws IOException { initConnection(); conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - InputStream in = conn.getInputStream(); //throws IOException - + InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; @@ -56,7 +48,6 @@ public int getContentLength() { return conn.getContentLength(); } - public void close() { - + public void close() { } } \ No newline at end of file diff --git a/group22/2622819383/Task3/download/DownloadThread.java b/group22/2622819383/Task3/download/DownloadThread.java index 07a4222b59..a04911cf7c 100644 --- a/group22/2622819383/Task3/download/DownloadThread.java +++ b/group22/2622819383/Task3/download/DownloadThread.java @@ -4,26 +4,23 @@ import java.util.concurrent.locks.*; public class DownloadThread extends Thread{ - Connection conn; - int startPos; - int endPos; + Connection conn; + int startPos; + int endPos; String targetURL; - final ReentrantLock lock = new ReentrantLock(); - volatile int i = 0; - - public DownloadThread(Connection conn, int startPos, int endPos, String targetURL) { - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; + //final ReentrantLock lock = new ReentrantLock(); + + + public DownloadThread(Connection conn, int startPos, int endPos, String targetURL) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; this.targetURL = targetURL; - } - - public void run() { + } + + public void run() { System.out.println("线程" + getName() + "startPos:" + startPos + "; endPos:" + endPos); - - try { - RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); byte[] buf = conn.read(startPos, endPos); raf.seek(startPos); @@ -32,8 +29,6 @@ public void run() { } catch (IOException e) { e.printStackTrace(); } - System.out.println("线程" + this.getName() + "下载完成."); - - } + } } diff --git a/group22/2622819383/Task3/download/FileDownloader.java b/group22/2622819383/Task3/download/FileDownloader.java index cc8049b70a..c4d8fa167d 100644 --- a/group22/2622819383/Task3/download/FileDownloader.java +++ b/group22/2622819383/Task3/download/FileDownloader.java @@ -3,37 +3,35 @@ import java.io.RandomAccessFile; import java.io.IOException; public class FileDownloader { - //DownloadListener listener; - String url; - - public FileDownloader(String url) { - this.url = url; - } - - public void execute() throws IOException { - - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; + String url; + + public FileDownloader(String url) { + this.url = url; + } + + public void execute() throws IOException { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; conn = new Connection(url); int length = conn.getContentLength(); String targetURL = "E:/" + url.substring(url.lastIndexOf("/") + 1); RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); raf.setLength(length); raf.close(); - + for (int i = 0; i < 3; i++) { int part = length / 3; int start = i * part; @@ -41,24 +39,28 @@ public void execute() throws IOException { DownloadThread t = new DownloadThread(conn, start, end, targetURL); t.start(); } - } - /* - public void setListener(DownloadListener listener) { - this.listener = listener; - } + } + + /* + DownloadListener listener; + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } - - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } + public DownloadListener getListener(){ + return this.listener; + } */ - public static void main(String[] args) throws IOException { - new FileDownloader("http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png").execute(); + + public static void main(String[] args) { + try { + new FileDownloader("http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png").execute(); + } catch (IOException e) { + e.printStackTrace(); + } } - } From 7ef185bab5352e9c3dcaaf1ebf2eb2f851c2046c Mon Sep 17 00:00:00 2001 From: laoheihei Date: Wed, 29 Mar 2017 18:31:14 +0800 Subject: [PATCH 8/8] change LinkedList --- group22/2622819383/Task3/LinkedList.java | 93 +++++++++++------------- 1 file changed, 42 insertions(+), 51 deletions(-) diff --git a/group22/2622819383/Task3/LinkedList.java b/group22/2622819383/Task3/LinkedList.java index 5ec46b348f..02ef7abc52 100644 --- a/group22/2622819383/Task3/LinkedList.java +++ b/group22/2622819383/Task3/LinkedList.java @@ -6,8 +6,10 @@ public class LinkedList { public void reverse() { int times = theSize - 1; //第一个元素自然移动到最后,所以只需进行theSize - 1次操作 int index = 0; - while (0 < times--) + while (0 < times) { add(index++, removeLast()); + times--; + } } /** @@ -44,28 +46,19 @@ public void remove(int i, int length) { * @param list */ public int[] getElements(LinkedList list) { - Iterator itSelf = iterator(); - Iterator itList = list.iterator(); + Iterator it = iterator(); int[] ret = new int[list.size()]; + int start = -1; + int value = 0; + int i = 0; //数组ret的索引 - int i = 0; //list中元素的值,代表当前列表中要取出元素的秩 - lastI = 0;//上一次取出元素的秩 - moveTimes = 0; - value = itSelf.next(); - index = 0;//要返回的数组中元素的秩 - - while (itList.hasNext()) { - i = itList.next(); - if (theSize <= i) throw new IndexOutOfBoundsException(); - - moveTimes = i - lastI; - while (0 < moveTimes--) - value = itSelf.next(); - - ret[index++] = value; - lastI = i; + for (Integer num : list) { + while (start < num && it.hasNext()) { + value = it.next(); + start++; + } + ret[i++] = value; } - return ret; } @@ -74,36 +67,17 @@ public int[] getElements(LinkedList list) { * 从当前链表中中删除在list中出现的元素 * @param list - */ - //返回与e相等的元素的秩;如果查找失败则返回-1 - private int find(Object e) { - Iterator it = iterator(); - int i = -1; //要返回的元素的秩 - Object value = null; - - while (it.hasNext()) { - value = it.next(); - i++; - if (value == e) return i; - if (e < value) return -1; - } - - return -1; - } - + */ public void subtract(LinkedList list) { - Iterator it = list.iterator(); - Object value = null; - int i = -1; - - while (it.hasNext()) { - value = it.next(); - i = find(value); - - //删去重复元素 - while (0 <= i) { - remove(i); - i = find(value); + Object current = null; + for (Object e : list) { + Iterator it = iterator(); + while (it.hasNext()) { + current = it.next(); + if (current.compareTo(e) == 0) + it.remove(); + if (current.compareTo(e) > 0) + break; } } } @@ -135,15 +109,31 @@ public void removeDuplicateValues() { * @param min * @param max */ - //[low, min]U[max, end] public void removeRange(int min, int max) { + //双链表删去(p, q)间的节点 + Node p = header; + Node q = null; + int removedNum = 0; //要删去节点的数目 + while ((p = p.succ()) != trailer && (p.data() <= min)) + ; + p = p.prev(); + q = p; + while ((q = q.succ()) != trailer && (q.data() < max)) + removedNum++; + p.succ = q; + q.prev = p; + theSize -= removedNum; + + + + /* //删去(i, j] int i = 0, j = 0; Iterator it = iterator(); while (it.hasNext()) { - Object value = it.next(); + int value = it.next(); if (value <= min) i++; if (value < max) j++; else break; //if(max <= value) break; @@ -155,6 +145,7 @@ public void removeRange(int min, int max) { head.succ = tail; tail.pred = head; theSize -= (j - i); + */ }