From fbf13438ced757f0b164b54aca8e2b42b37aa507 Mon Sep 17 00:00:00 2001 From: Kelly <205301442@qq.com> Date: Sun, 19 Mar 2017 22:31:24 +0800 Subject: [PATCH] week3 --- group10/205301442/src/api/Connection.java | 23 ++ .../src/api/ConnectionException.java | 5 + .../205301442/src/api/ConnectionManager.java | 10 + .../205301442/src/api/DownloadListener.java | 6 + .../src/com/coding/week1/LinkedList1.java | 280 ++++++++++++++++++ .../src/download/DownloadThread.java | 28 ++ .../src/download/FileDownloader.java | 157 ++++++++++ .../src/download/FileDownloaderTest.java | 50 ++++ .../src/download/api/Connection.java | 23 ++ .../src/download/api/ConnectionException.java | 5 + .../src/download/api/ConnectionManager.java | 10 + .../src/download/api/DownloadListener.java | 6 + .../src/download/impl/ConnectionImpl.java | 76 +++++ .../download/impl/ConnectionManagerImpl.java | 34 +++ .../src/download/impl/DoloadListenerImpl.java | 16 + .../205301442/src/impl/ConnectionImpl.java | 76 +++++ .../src/impl/ConnectionManagerImpl.java | 34 +++ .../src/impl/DoloadListenerImpl.java | 16 + 18 files changed, 855 insertions(+) create mode 100644 group10/205301442/src/api/Connection.java create mode 100644 group10/205301442/src/api/ConnectionException.java create mode 100644 group10/205301442/src/api/ConnectionManager.java create mode 100644 group10/205301442/src/api/DownloadListener.java create mode 100644 group10/205301442/src/com/coding/week1/LinkedList1.java create mode 100644 group10/205301442/src/download/DownloadThread.java create mode 100644 group10/205301442/src/download/FileDownloader.java create mode 100644 group10/205301442/src/download/FileDownloaderTest.java create mode 100644 group10/205301442/src/download/api/Connection.java create mode 100644 group10/205301442/src/download/api/ConnectionException.java create mode 100644 group10/205301442/src/download/api/ConnectionManager.java create mode 100644 group10/205301442/src/download/api/DownloadListener.java create mode 100644 group10/205301442/src/download/impl/ConnectionImpl.java create mode 100644 group10/205301442/src/download/impl/ConnectionManagerImpl.java create mode 100644 group10/205301442/src/download/impl/DoloadListenerImpl.java create mode 100644 group10/205301442/src/impl/ConnectionImpl.java create mode 100644 group10/205301442/src/impl/ConnectionManagerImpl.java create mode 100644 group10/205301442/src/impl/DoloadListenerImpl.java diff --git a/group10/205301442/src/api/Connection.java b/group10/205301442/src/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group10/205301442/src/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +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/group10/205301442/src/api/ConnectionException.java b/group10/205301442/src/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group10/205301442/src/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group10/205301442/src/api/ConnectionManager.java b/group10/205301442/src/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group10/205301442/src/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group10/205301442/src/api/DownloadListener.java b/group10/205301442/src/api/DownloadListener.java new file mode 100644 index 0000000000..41c4907246 --- /dev/null +++ b/group10/205301442/src/api/DownloadListener.java @@ -0,0 +1,6 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(boolean isFinish); + public boolean getIsFinished(); +} diff --git a/group10/205301442/src/com/coding/week1/LinkedList1.java b/group10/205301442/src/com/coding/week1/LinkedList1.java new file mode 100644 index 0000000000..9583a6505c --- /dev/null +++ b/group10/205301442/src/com/coding/week1/LinkedList1.java @@ -0,0 +1,280 @@ +package com.coding.week1; +import java.util.List; +import java.util.ArrayList; + +public class LinkedList1 { + private Node head; + private int size; + public void add(Object o){ + Node node = new Node(o,null); + node(size-1).next = node; + size++; + } + public void add(int index , Object o){ + if(index<0){ + return; + } + Node node = new Node(o,null); + if(index==0){ + head=node; + node.next = node(0); + }else{ + node(index-1).next = node; + node.next = node(index+1); + } + size++; + } + public Object get(int index){ + if(index<0){ + return null; + } + return node(index); + } + public Object remove(int index){ + if(index<0){ + return null; + } + Object o =node(index).data; + node(index-1).next = node(index+1); + size--; + return o; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + Node n = new Node(o,null); + head = n; + if(size>0){ + n.next = node(0); + } + size++; + + } + public void addLast(Object o){ + Node n = new Node(o,null); + if(size>0){ + node(size-1).next = n; + }else{ + head = n; + } + size++; + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return new Ito(); + } + public class Ito implements Iterator{ + int cursor; + @Override + public boolean hasNext() { + if(cursor!=size){ + return true; + } + return false; + } + + @Override + public Object next() { + if(cursor>=size-1){ + return null; + } + Object o=node(cursor).data; + cursor++; + return o; + + } + + } + + + private static class Node{ + Object data; + Node next; + public Node(Object data,Node next){ + this.data = data; + this.next = next; + } + + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node x = node(size-1); + head = x; + for(int i=size-2;i>=0;i--){ + x.next = node(i); + x = node(i); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int newSize = size/2+size%2; + head = node(newSize-1); + size = newSize; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + if(i==0){ + head = node(length); + } + node(i-1).next = node(i+length); + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] temp = new int[size]; + for(int i=0;i-1){ + temp[0]=Integer.parseInt(o); + j++; + } + } + + } + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + int[] temp = new int[size]; + int newSize = 0; + for(int i=0;i lists = new ArrayList(); + while(true){ + if((int)n.data>min&&(int)n.data0){ + if(nowLen+m>len){ + System.arraycopy(temp, 0, bt, nowLen, len-nowLen); + break; + } + System.arraycopy(temp, 0, bt, nowLen, m); + nowLen += m; + + } + return bt; + } + + @Override + public int getContentLength() { + if(is==null){ + return 0; + } + try { + int length=conn.getContentLength(); + return length; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return 0; + } + + @Override + public void close() { + if(is!=null){ + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + +} diff --git a/group10/205301442/src/download/impl/ConnectionManagerImpl.java b/group10/205301442/src/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..34e02aad97 --- /dev/null +++ b/group10/205301442/src/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,34 @@ +package com.coderising.download.impl; + +import java.io.DataInputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + //在connection里实现连接,不再这儿,这里只是返回 + @Override + public Connection open(String url) throws ConnectionException { + try { + + + URL uri = new URL(url); + HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(5*1000); + Connection connImpl = new ConnectionImpl(conn); + return connImpl; + } catch (IOException e) { + // TODO Auto-generated catch block + + } + return null; + } + +} diff --git a/group10/205301442/src/download/impl/DoloadListenerImpl.java b/group10/205301442/src/download/impl/DoloadListenerImpl.java new file mode 100644 index 0000000000..d448b817c3 --- /dev/null +++ b/group10/205301442/src/download/impl/DoloadListenerImpl.java @@ -0,0 +1,16 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.DownloadListener; + +public class DoloadListenerImpl implements DownloadListener{ + boolean isFinish = false; + @Override + public void notifyFinished(boolean isfinish) { + // TODO Auto-generated method stub + this.isFinish = isfinish; + } + public boolean getIsFinished(){ + return isFinish; + } + +} diff --git a/group10/205301442/src/impl/ConnectionImpl.java b/group10/205301442/src/impl/ConnectionImpl.java new file mode 100644 index 0000000000..612ef940c9 --- /dev/null +++ b/group10/205301442/src/impl/ConnectionImpl.java @@ -0,0 +1,76 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private HttpURLConnection conn; + private InputStream is; + public ConnectionImpl(HttpURLConnection conn) { + try { + this.conn = conn; + is = conn.getInputStream(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + if(is==null){ + return null; + } + + is.skip(startPos); + int len = endPos-startPos; + byte[] bt = new byte[len]; + byte[] temp = new byte[1024]; + int m=0; + int nowLen=0; + while((m=is.read(temp))>0){ + if(nowLen+m>len){ + System.arraycopy(temp, 0, bt, nowLen, len-nowLen); + break; + } + System.arraycopy(temp, 0, bt, nowLen, m); + nowLen += m; + + } + return bt; + } + + @Override + public int getContentLength() { + if(is==null){ + return 0; + } + try { + int length=conn.getContentLength(); + return length; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return 0; + } + + @Override + public void close() { + if(is!=null){ + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + +} diff --git a/group10/205301442/src/impl/ConnectionManagerImpl.java b/group10/205301442/src/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..34e02aad97 --- /dev/null +++ b/group10/205301442/src/impl/ConnectionManagerImpl.java @@ -0,0 +1,34 @@ +package com.coderising.download.impl; + +import java.io.DataInputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + //在connection里实现连接,不再这儿,这里只是返回 + @Override + public Connection open(String url) throws ConnectionException { + try { + + + URL uri = new URL(url); + HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(5*1000); + Connection connImpl = new ConnectionImpl(conn); + return connImpl; + } catch (IOException e) { + // TODO Auto-generated catch block + + } + return null; + } + +} diff --git a/group10/205301442/src/impl/DoloadListenerImpl.java b/group10/205301442/src/impl/DoloadListenerImpl.java new file mode 100644 index 0000000000..d448b817c3 --- /dev/null +++ b/group10/205301442/src/impl/DoloadListenerImpl.java @@ -0,0 +1,16 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.DownloadListener; + +public class DoloadListenerImpl implements DownloadListener{ + boolean isFinish = false; + @Override + public void notifyFinished(boolean isfinish) { + // TODO Auto-generated method stub + this.isFinish = isfinish; + } + public boolean getIsFinished(){ + return isFinish; + } + +}