diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java index d10c40a563..7afb0e5500 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java @@ -171,7 +171,18 @@ private static class Node{ * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ - + if(null==head){ + return; + } + Node tempNode=new Node(); + Node currentNode=head.next; + head.next=null; + while(null!=currentNode){ + tempNode=currentNode.next; + currentNode.next=head; + head=currentNode; + currentNode=tempNode; + } } /** @@ -181,7 +192,17 @@ public void reverse(){ */ public void removeFirstHalf(){ - + if(null==head){ + return; + } + Node tempNode; + int size=size(); + size=size/2; + for(int i=0;i=oriSize){ + break; + } + temp=next-temp; + for(int i=0;inext){ + continue; + } + if(currentNode==head){ + head=head.next; + }else{ + preNode.next=currentNode.next; + } + tempNode=currentNode; + currentNode=currentNode.next; + tempNode.next=null; + if(null==currentNode){ + break; + } + tempData=(Integer)currentNode.data; + } } /** @@ -220,17 +319,91 @@ public void subtract(LinkedList list){ * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + Node currentNode=head; + Node tempNode=head; + int tempData; + while(null!=currentNode && null!=currentNode.next){ + tempData=(Integer)currentNode.data; + if(tempData==(Integer)currentNode.next.data){ + if(null!=currentNode.next.next){ + tempNode=currentNode.next; + currentNode.next=currentNode.next.next; + tempNode.next=null; + }else{ + currentNode.next=null; + } + }else{ + currentNode=currentNode.next; + } + } } /** * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * 试写一高效的算法,删除表中所有值大于等于min且小于等于max的元素(若表中存在这样的元素) * @param min * @param max */ public void removeRange(int min, int max){ - + if(null==head){ + return; + } + int size=size(); + int headData=(Integer)head.data; + int endData=(Integer)get(size-1); + if(headData>=min && endData<=max){ + head=null; + return; + } + if(headData>max || endData=arm){ + return beginIndex; + } + tempData=(Integer)get(endIndex); + if(tempData<=arm){ + return endIndex; + } + int middleIndex=0; + while(beginIndexarm){ + endIndex=middleIndex; + }else{ + break; + } + } + return middleIndex; } /** @@ -239,6 +412,31 @@ public void removeRange(int min, int max){ * @param list */ public LinkedList rsection( LinkedList list){ - return null; + LinkedList armList=new LinkedList(); + Node tempNode=head; + if(null==list || list.size()==0){ + while(null!=tempNode){ + armList.add(tempNode.data); + tempNode=tempNode.next; + } + }else{ + Iterator iterator = list.iterator(); + Integer next; + Integer data; + while(iterator.hasNext()){ + next = (Integer)iterator.next(); + while(null!=tempNode){ + data = (Integer)tempNode.data; + if(data 0){ + FileInputStream fileInputStream =new FileInputStream(file); + InputStreamReader inputStreamReader =new InputStreamReader(fileInputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String content = bufferedReader.readLine(); + int currentPosition = Integer.parseInt(content); + startIndex = currentPosition; + } + + int endIndex = (threadId+1)*blockSize-1; + if(threadId == threadCount-1){ + endIndex = length-1; + } + Connection connection = cm.open(url); + new DownloadThread(connection,startIndex,endIndex,threadId,listener).start(); + } + } + + 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/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/FileDownloaderTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..10e9dbfa46 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/FileDownloaderTest.java @@ -0,0 +1,66 @@ +package com.github.ipk2015.coding2017.coderising.download; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.coderising.download.api.ConnectionManager; +import com.github.ipk2015.coding2017.coderising.download.api.DownloadListener; +import com.github.ipk2015.coding2017.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://dldir1.qq.com/qqfile/qq/QQ8.9.1/20437/QQ8.9.1.exe"; + int threadCount=3; + FileDownloader downloader = new FileDownloader(url,threadCount); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + System.out.println("下载完成啦"); + } + + }); + + + try { + downloader.execute(); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + // 等待多线程下载程序执行完毕 +// while (!downloadFinished) { +// try { +// System.out.println("还没有下载完成,休眠五秒"); +// //休眠5秒 +// Thread.sleep(5000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// System.out.println("下载完成!"); + + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/Connection.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/Connection.java new file mode 100644 index 0000000000..c7a61ac781 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/Connection.java @@ -0,0 +1,32 @@ +package com.github.ipk2015.coding2017.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; + /** + * 给定开始和结束位置, 读取数据,适用于多线程下载 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public void read(int startPos,int endPos,int threadId,DownloadListener listener) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/ConnectionException.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..780ed30029 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.github.ipk2015.coding2017.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/ConnectionManager.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..cfa76e4ea6 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.github.ipk2015.coding2017.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/DownloadListener.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..97ceef6416 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.github.ipk2015.coding2017.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/impl/ConnectionImpl.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..bf60948390 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,97 @@ +package com.github.ipk2015.coding2017.coderising.download.impl; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; + +import com.github.ipk2015.coding2017.coderising.download.api.Connection; +import com.github.ipk2015.coding2017.coderising.download.api.DownloadListener; + + +public class ConnectionImpl implements Connection{ + private String path; + public static int threadCount=0; + public ConnectionImpl(String path){ + super(); + this.path=path; + } + + + @Override + public int getContentLength() { + URL url; + int length=0; + try { + url = new URL(path); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(3000); + int code = conn.getResponseCode(); + System.out.println("code:"+code+""); + if(code == 200){ + // 获得服务器端文件的大小 + length = conn.getContentLength(); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return length; + } + + @Override + public void close() { + + + } + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + + @Override + public void read(int startPos, int endPos,int threadId,DownloadListener listener) throws IOException { + URL url=new URL(path); + HttpURLConnection conn=(HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(3000); + conn.setRequestProperty("Range", "bytes="+startPos+"-"+endPos); + int code=conn.getResponseCode(); + if(code==206){ + InputStream inputStream = conn.getInputStream(); + int lastIndexOf = path.lastIndexOf("/"); + RandomAccessFile randomAccessFile = new RandomAccessFile(path.substring(lastIndexOf+1),"rwd"); + RandomAccessFile tempPositionFile = new RandomAccessFile(threadId+".txt","rwd"); + randomAccessFile.seek(startPos); + int length=-1,total=0; + byte[] buffer=new byte[1024*500]; + while((length=inputStream.read(buffer))!=-1){ + randomAccessFile.write(buffer,0,length); + total=total+length; + tempPositionFile.write(((startPos+total)+"").getBytes()); + } + tempPositionFile.close(); + randomAccessFile.close(); + inputStream.close(); + synchronized (ConnectionImpl.class) { + //把当前线程的临时文件删除 + File file = new File(threadId+".txt"); + if(file.exists()){ + file.delete(); + } + threadCount--; + if(threadCount==0){ + listener.notifyFinished(); + } + } + } + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..50169258f1 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,18 @@ +package com.github.ipk2015.coding2017.coderising.download.impl; + +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; + +import com.github.ipk2015.coding2017.coderising.download.api.Connection; +import com.github.ipk2015.coding2017.coderising.download.api.ConnectionException; +import com.github.ipk2015.coding2017.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection conn=new ConnectionImpl(url); + return conn; + } +}