diff --git a/group24/330657387/src/main/week01/data_structure/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java index 9a2f4d2c8b..6ffbf377ef 100644 --- a/group24/330657387/src/main/week01/data_structure/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -2,6 +2,9 @@ import java.util.Arrays; +import main.week01.data_structure.api.Iterator; +import main.week01.data_structure.api.List; + public class ArrayList implements List { private int size = 0; @@ -48,7 +51,7 @@ public Object remove(int index) { Object dest = elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[size---1]=null;//ֹڴй© + elementData[size---1]=null;//��ֹ�ڴ�й© return dest; } diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/main/week01/data_structure/ArrayListTest.java similarity index 94% rename from group24/330657387/src/test/week01/data_structure/ArrayListTest.java rename to group24/330657387/src/main/week01/data_structure/ArrayListTest.java index d9d63339b2..d54bc0b73e 100644 --- a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayListTest.java @@ -1,7 +1,6 @@ -package test.week01.data_structure; +package main.week01.data_structure; import static org.junit.Assert.*; -import main.week01.data_structure.ArrayList; import main.week01.data_structure.ArrayList.ArrayListIterator; import org.junit.Before; diff --git a/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java b/group24/330657387/src/main/week01/data_structure/BinaryTreeNodeTest.java similarity index 83% rename from group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java rename to group24/330657387/src/main/week01/data_structure/BinaryTreeNodeTest.java index 515da2e7d4..68dcd18d87 100644 --- a/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java +++ b/group24/330657387/src/main/week01/data_structure/BinaryTreeNodeTest.java @@ -1,7 +1,6 @@ -package test.week01.data_structure; +package main.week01.data_structure; import static org.junit.Assert.*; -import main.week01.data_structure.BinaryTreeNode; import org.junit.Before; import org.junit.Test; diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index b61df9f254..aafe3654ca 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -2,6 +2,9 @@ import java.util.NoSuchElementException; +import main.week01.data_structure.api.Iterator; +import main.week01.data_structure.api.List; + public class LinkedList implements List { private Node head; @@ -49,7 +52,7 @@ public Object get(int index) { return dest.data; } - public Node getNode(int index) { + private Node getNode(int index) { rangeCheck(index); Node dest = head; for (int i = 0; i < index; i++) { @@ -62,7 +65,7 @@ public Object remove(int index) { rangeCheck(index); if (index == 0) { return removeFirst(); - }else if(index == size){ + } else if (index == size) { return removeLast(); } Node pre = getNode(index - 1); @@ -132,6 +135,10 @@ private LinkedListIterator(LinkedList list) { this.list = list; } + public void reset() { + position = 0; + } + @Override public boolean hasNext() { return position + 1 <= list.size; @@ -147,4 +154,215 @@ public Object next() { public LinkedListIterator iterator() { return new LinkedListIterator(this); } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (size <= 1) { + return; + } + Node a = head, b = head.next; + head.next = null; + Node temp; + while (null != b) { + temp = b.next; + b.next = a; + a = b; + b = temp; + } + head = a; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + if (size <= 1) { + return; + } + size = size % 2 == 0 ? size / 2 : size / 2 + 1; + head = getNode(size - 1); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + rangeCheck(i); + rangeCheck(i + length - 1); + if (i == 0) { + head = getNode(length); + size -= length; + } else { + Node pre = getNode(i - 1); + pre.next = getNode(i + length - 1).next; + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + * @throws Exception + */ + public int[] getElements(LinkedList list) throws Exception { + if (list == null) { + throw new Exception("传入链表为空?"); + } + int[] res = new int[list.size]; + for (int i = 0; i < list.size; i++) { + res[i] = Integer.parseInt(get( + Integer.parseInt(list.get(i).toString()) - 1).toString()); + } + return res; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + * @throws Exception + */ + + public void subtract(LinkedList list) throws Exception { + if (list == null) { + throw new Exception("传入链表为空?"); + } + LinkedListIterator beSub = this.iterator(), sub = list.iterator(); + while (sub.hasNext()) { + Object a = sub.next(); + while (beSub.hasNext()) { + Object b = beSub.next(); + if (a.equals(b)) { + this.remove(beSub.position - 1); + } + } + beSub.reset(); + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + LinkedListIterator iter = this.iterator(); + if (size <= 1) { + return; + } + Object a = iter.next(); + while (iter.hasNext()) { + Object b = iter.next(); + if (b.equals(a)) { + remove(iter.position - 1); + continue; + } else { + a = b; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + * @throws Exception + */ + public void removeRange(int min, int max) throws Exception { + if(min > max){ + throw new Exception("输入有问题!"); + } + if(max < Integer.parseInt(get(0).toString())){ + throw new Exception("全部太小!"); + } + if(min > Integer.parseInt(get(size-1).toString())){ + throw new Exception("全部太大!"); + } + int firstRemove = -1,lastRemove = -1; + LinkedListIterator iter = this.iterator(); + boolean hasmin = false; + while(iter.hasNext()){ + int n = Integer.parseInt(iter.next().toString()); + if(n>min && !hasmin){ + firstRemove = iter.position - 1; + hasmin = true; + } + if(n Integer.parseInt(b.data.toString())){ + res.add(b.data); + b = b.next; + continue; + } + if(Integer.parseInt(a.data.toString()) < Integer.parseInt(b.data.toString())){ + res.add(a.data); + a = a.next; + continue; + } + } + while(null != a){ + res.add(a.data); + a = a.next; + } + while(null != b){ + res.add(b.data); + b = b.next; + } + return res; + } + + public String ToString() { + LinkedListIterator iter = this.iterator(); + StringBuilder sb = new StringBuilder(); + while (iter.hasNext()) { + sb.append(iter.next()); + sb.append("->"); + } + sb.append("null"); + return sb.toString(); + } } diff --git a/group24/330657387/src/main/week01/data_structure/LinkedListTest.java b/group24/330657387/src/main/week01/data_structure/LinkedListTest.java new file mode 100644 index 0000000000..671cc20cd2 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/LinkedListTest.java @@ -0,0 +1,218 @@ +package main.week01.data_structure; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import main.week01.data_structure.LinkedList.LinkedListIterator; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + private LinkedList list; + private LinkedList[] lists = new LinkedList[3]; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + + lists[0] = new LinkedList(); + lists[1] = new LinkedList(); + lists[2] = new LinkedList(); + + lists[1].add("A"); + + lists[2].add("A"); + lists[2].add("B"); + lists[2].add("C"); + lists[2].add("D"); + lists[2].add("E"); + + } + + @Test + public void testGet() { + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertEquals("C", list.get(0)); + } + + @Test + public void testRemove() { + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add(0, "E"); + assertEquals("E", list.remove(0)); + assertEquals("D", list.remove(list.size() - 1)); + assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + LinkedListIterator iter = list.iterator(); + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertTrue(iter.hasNext()); + assertEquals("C", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("A", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("B", iter.next()); + assertFalse(iter.hasNext()); + } + + @Test + public void testReverse() { + LinkedList l = lists[2]; + l.reverse(); + LinkedListIterator iter = l.iterator(); + StringBuilder sb = new StringBuilder(); + while (iter.hasNext()) { + sb.append(iter.next()); + } + // assertEquals("", sb.toString()); + // assertEquals("A", sb.toString()); + assertEquals("EDCBA", sb.toString()); + + } + + @Test + public void testRemoveFirstHalf() { + LinkedList l = lists[0]; + l.removeFirstHalf(); + + LinkedListIterator iter = l.iterator(); + StringBuilder sb = new StringBuilder(); + while (iter.hasNext()) { + sb.append(iter.next()); + sb.append("->"); + } + sb.append("null"); + + assertEquals("null", sb.toString()); + //assertEquals("A->null", sb.toString()); + //assertEquals("C->D->E->null", sb.toString()); + } + + @Test + public void testRemoveByIndex() { + try{ + LinkedList l = lists[2]; + l.remove(0, 1); + System.out.println(l.ToString()); + }catch(Exception e){ + assertEquals(IndexOutOfBoundsException.class, e.getClass()); + } + } + + @Test + public void testGetElements() { + list.add(11); + list.add(22); + list.add(33); + list.add(44); + list.add(55); + list.add(66); + list.add(77); + list.add(88); + list.add(99); + + LinkedList l = new LinkedList(); + l.add(1); + l.add(3); + l.add(4); + l.add(6); + try{ + int[] res = list.getElements(l); + System.out.println(Arrays.toString(res)); + }catch(Exception e){ + assertEquals(e.getMessage(), "传入链表为空?"); + } + } + + @Test + public void testSubtract() { + list.add(11); + list.add(22); + list.add(33); + list.add(44); + list.add(55); + list.add(66); + list.add(77); + list.add(88); + list.add(99); + + LinkedList l = new LinkedList(); + l.add(11); + l.add(33); + l.add(44); + l.add(66); + try{ + list.subtract(l); + System.out.println(list.ToString()); + }catch(Exception e){ + assertEquals(e.getMessage(), "传入链表为空?"); + } + } + + @Test + public void testRemoveDuplicateValues() { + list.add(11); + list.add(11); + list.add(22); + list.add(33); + list.add(33); + + list.removeDuplicateValues(); + + System.out.println(list.ToString()); + } + + @Test + public void testRemoveRange() throws Exception { + list.add(11); + list.add(22); + list.add(33); + list.add(44); + list.add(55); + list.add(66); + list.add(77); + list.add(88); + list.add(99); + System.out.println(list.ToString()); + + try{ + list.removeRange(50, 80); + System.out.println(list.ToString()); + }catch(Exception e){ + assertEquals(e.getMessage(), "输入有问题!"); + } + } + + @Test + public void testIntersection() { + list.add(11); +// list.add(22); +// list.add(33); +// list.add(44); +// list.add(55); +// list.add(66); +// list.add(77); +// list.add(88); +// list.add(99); + + LinkedList l = new LinkedList(); + l.add(10); +// l.add(30); +// l.add(40); +// l.add(60); + + System.out.println(list.intersection(l).ToString()); + } +} diff --git a/group24/330657387/src/main/week01/data_structure/Iterator.java b/group24/330657387/src/main/week01/data_structure/api/Iterator.java similarity index 67% rename from group24/330657387/src/main/week01/data_structure/Iterator.java rename to group24/330657387/src/main/week01/data_structure/api/Iterator.java index 194891e56a..3ef869c5c8 100644 --- a/group24/330657387/src/main/week01/data_structure/Iterator.java +++ b/group24/330657387/src/main/week01/data_structure/api/Iterator.java @@ -1,4 +1,4 @@ -package main.week01.data_structure; +package main.week01.data_structure.api; public interface Iterator { public boolean hasNext(); diff --git a/group24/330657387/src/main/week01/data_structure/List.java b/group24/330657387/src/main/week01/data_structure/api/List.java similarity index 81% rename from group24/330657387/src/main/week01/data_structure/List.java rename to group24/330657387/src/main/week01/data_structure/api/List.java index 6cbc15f8ab..138f7bd018 100644 --- a/group24/330657387/src/main/week01/data_structure/List.java +++ b/group24/330657387/src/main/week01/data_structure/api/List.java @@ -1,4 +1,4 @@ -package main.week01.data_structure; +package main.week01.data_structure.api; public interface List { diff --git a/group24/330657387/src/main/week02/practice/ArrayUtil.java b/group24/330657387/src/main/week02/practice/ArrayUtil.java index d174fdbe27..ed0dd51a60 100644 --- a/group24/330657387/src/main/week02/practice/ArrayUtil.java +++ b/group24/330657387/src/main/week02/practice/ArrayUtil.java @@ -1,6 +1,7 @@ package main.week02.practice; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import com.sun.xml.internal.ws.org.objectweb.asm.Label; @@ -19,7 +20,6 @@ public void reverseArray(int[] origin) { return; } int i = 0, j = origin.length - 1; - int temp; while (j > i) { origin[i] = origin[i] ^ origin[j]; origin[j] = origin[i] ^ origin[j]; @@ -137,21 +137,36 @@ public int[] fibonacci(int max) { if (max <= 1) { return new int[0]; } - ArrayList list = new ArrayList(); - list.add(1); - list.add(1); - int index = 1; - while (list.get(index) + list.get(index - 1) < max) { - list.add(list.get(index) + list.get(index - 1)); - index++; + if(max == 2){ + return new int[]{1,1}; } - Iterator iter = list.iterator(); - int[] res = new int[list.size()]; - int i = 0; - while (iter.hasNext()) { - res[i++] = iter.next(); + int[] arr = new int[max]; + arr[0] = arr[1] = 1; + int count = 2; + for(; count < max; count++){ + arr[count] = arr[count-1] + arr[count-2]; + if(arr[count] >= max){ + break; + } } - return res; + + return Arrays.copyOf(arr, count); + +// ArrayList list = new ArrayList(); +// list.add(1); +// list.add(1); +// int index = 1; +// while (list.get(index) + list.get(index - 1) < max) { +// list.add(list.get(index) + list.get(index - 1)); +// index++; +// } +// Iterator iter = list.iterator(); +// int[] res = new int[list.size()]; +// int i = 0; +// while (iter.hasNext()) { +// res[i++] = iter.next(); +// } +// return res; } /** diff --git a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java b/group24/330657387/src/main/week02/practice/ArrayUtilTest.java similarity index 94% rename from group24/330657387/src/test/week02/practice/ArrayUtilTest.java rename to group24/330657387/src/main/week02/practice/ArrayUtilTest.java index 0e29853068..3c7ccbc4e5 100644 --- a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java +++ b/group24/330657387/src/main/week02/practice/ArrayUtilTest.java @@ -1,12 +1,6 @@ -package test.week02.practice; - -import static org.junit.Assert.*; +package main.week02.practice; import java.util.Arrays; -import java.util.HashMap; - -import main.week02.practice.ArrayUtil; - import org.junit.Before; import org.junit.Test; diff --git a/group24/330657387/src/main/week03/download/DownloadThread.java b/group24/330657387/src/main/week03/download/DownloadThread.java new file mode 100644 index 0000000000..f18bbf234c --- /dev/null +++ b/group24/330657387/src/main/week03/download/DownloadThread.java @@ -0,0 +1,55 @@ +package main.week03.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import main.week03.download.api.Connection; + +public class DownloadThread extends Thread { + + Connection conn; + + int startPos; + int endPos; + + CyclicBarrier barrier; + + String localFile; + + public DownloadThread(Connection conn, int startPos, int endPos, + String localFile, CyclicBarrier barrier) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + public void run() { + + try { + System.out.println("Begin to read [" + startPos + "-" + endPos + + "]"); + + byte[] data = conn.read(startPos, endPos); + //设置文件的读取权限 + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + + file.seek(startPos); + + file.write(data); + + file.close(); + + conn.close(); + + barrier.await(); // 等待别的线程完成 + + } catch (Exception e) { + e.printStackTrace(); + + } + + } +} diff --git a/group24/330657387/src/main/week03/download/FileDownloader.java b/group24/330657387/src/main/week03/download/FileDownloader.java new file mode 100644 index 0000000000..54d6c260ad --- /dev/null +++ b/group24/330657387/src/main/week03/download/FileDownloader.java @@ -0,0 +1,131 @@ +package main.week03.download; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import main.week03.download.api.Connection; +import main.week03.download.api.ConnectionException; +import main.week03.download.api.ConnectionManager; +import main.week03.download.api.DownloadListener; + +public class FileDownloader { + + private String url; + private String filePath = "/"; + + public DownloadListener listener; + + public ConnectionManager cm; + + private int DOWNLOAD_TRHEAD_NUM = 3; + + public FileDownloader(String _url, String filePath) { + this.url = _url; + this.filePath = filePath; + } + + 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方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + //当支线线程全部结束,即每个线程都调用了await方法,就会触发主线程,即listener的notify + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM, + new Runnable() { + public void run() { + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + //确保文件里有足够的空间? + createPlaceHolderFile(this.filePath, length); + + //每个线程的读取区间 + int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); + + for (int i = 0; i < DOWNLOAD_TRHEAD_NUM; i++) { + + DownloadThread thread = new DownloadThread(cm.open(url), + ranges[i][0], ranges[i][1], filePath, barrier); + + thread.start(); + } + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + } + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + int[][] ranges = new int[threadNum][2]; + + int eachThreadSize = contentLen / threadNum;// 每个线程需要下载的文件大小 + int left = contentLen % threadNum;// 剩下的归最后一个线程来处理 + + for (int i = 0; i < threadNum; i++) { + + int startPos = i * eachThreadSize; + + int endPos = (i + 1) * eachThreadSize - 1; + + if ((i == (threadNum - 1))) { + endPos += left; + } + ranges[i][0] = startPos; + ranges[i][1] = endPos; + + } + + return ranges; + } + + private void createPlaceHolderFile(String filePath, int contentLen) + throws IOException { + RandomAccessFile file = new RandomAccessFile(filePath, "rw"); + + for (int i = 0; i < contentLen; i++) { + file.write(1); + } + + file.close(); + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager cm) { + this.cm = cm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group24/330657387/src/main/week03/download/FileDownloaderTest.java b/group24/330657387/src/main/week03/download/FileDownloaderTest.java new file mode 100644 index 0000000000..cee0ae1f61 --- /dev/null +++ b/group24/330657387/src/main/week03/download/FileDownloaderTest.java @@ -0,0 +1,77 @@ +package main.week03.download; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import main.week03.download.api.ConnectionManager; +import main.week03.download.api.DownloadListener; +import main.week03.download.impl.ConnectionManagerImpl; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FileDownloaderTest { + boolean downloadFinished = false; + String _url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + String _filePath = "/"; + @Before + public void setUp() throws Exception { + File file = new File("."); + + String packageName = this.getClass().getPackage().getName(); + // 把包名转化成路径的一部分 + packageName = packageName.replace('.', '/'); + + _filePath = file.getCanonicalPath() + "/src/" + packageName + "/" + "test.jpg"; + try{ + System.out.println(file.getCanonicalPath());//获取标准的路径 + System.out.println(file.getAbsolutePath());//获取绝对路径 + System.out.println(file.getPath()); + System.out.println(packageName); + }catch(Exception e){} + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPath(){} + + @Test + public void testDownload() throws IOException { + + FileDownloader downloader = new FileDownloader(_url, _filePath); + + 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/group24/330657387/src/main/week03/download/api/Connection.java b/group24/330657387/src/main/week03/download/api/Connection.java new file mode 100644 index 0000000000..b75b565af5 --- /dev/null +++ b/group24/330657387/src/main/week03/download/api/Connection.java @@ -0,0 +1,24 @@ +package main.week03.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 + * @throws ConnectionException + */ + public int getContentLength() throws ConnectionException; + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group24/330657387/src/main/week03/download/api/ConnectionException.java b/group24/330657387/src/main/week03/download/api/ConnectionException.java new file mode 100644 index 0000000000..e929fd388a --- /dev/null +++ b/group24/330657387/src/main/week03/download/api/ConnectionException.java @@ -0,0 +1,19 @@ +package main.week03.download.api; + +import java.io.IOException; +import java.net.MalformedURLException; + +public class ConnectionException extends Exception { + + public ConnectionException(String errmsg){ + super(errmsg); + } + + public ConnectionException(MalformedURLException e) { + super(e); + } + + public ConnectionException(IOException e) { + super(e); + } +} diff --git a/group24/330657387/src/main/week03/download/api/ConnectionManager.java b/group24/330657387/src/main/week03/download/api/ConnectionManager.java new file mode 100644 index 0000000000..6df387d42b --- /dev/null +++ b/group24/330657387/src/main/week03/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package main.week03.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group24/330657387/src/main/week03/download/api/DownloadListener.java b/group24/330657387/src/main/week03/download/api/DownloadListener.java new file mode 100644 index 0000000000..86cdd29d70 --- /dev/null +++ b/group24/330657387/src/main/week03/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package main.week03.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java b/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..e725ff15a1 --- /dev/null +++ b/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java @@ -0,0 +1,81 @@ +package main.week03.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; + +import main.week03.download.FileDownloader; +import main.week03.download.api.Connection; +import main.week03.download.api.ConnectionException; + +public class ConnectionImpl implements Connection { + + URL url; + static final int BUFFER_SIZE = 1024; + + public ConnectionImpl(String _url) throws ConnectionException { + try { + //传入的字符串必须是url格式 + url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + int totalLen = endPos - startPos + 1; + + //是URLConnection的子类,负责http协议的链接 + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream inputStream = conn.getInputStream(); + + byte[] buffer = new byte[BUFFER_SIZE]; + + //输出流 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while (baos.size() < totalLen) { + + //一次读取1024字节 + int len = inputStream.read(buffer); + if (len < 0) { + break; + } + baos.write(buffer, 0, len); + } + + //防止这个线程过度读取 + if (baos.size() > totalLen) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + @Override + public int getContentLength() throws ConnectionException { + int res = -1; + try { + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + res = conn.getContentLength(); + conn.disconnect(); + return res; + } catch (IOException e) { + throw new ConnectionException(e); + } + } + + @Override + public void close() { + + } + +} diff --git a/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java b/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..2253f4fa06 --- /dev/null +++ b/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package main.week03.download.impl; + +import main.week03.download.api.Connection; +import main.week03.download.api.ConnectionException; +import main.week03.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection res = new ConnectionImpl(url); + + return res; + } + +} diff --git a/group24/330657387/src/main/week03/download/test.jpg b/group24/330657387/src/main/week03/download/test.jpg new file mode 100644 index 0000000000..3a052212e5 Binary files /dev/null and b/group24/330657387/src/main/week03/download/test.jpg differ diff --git a/group24/330657387/src/test/week01/data_structure/LinkedListTest.java b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java deleted file mode 100644 index cf374a6dcb..0000000000 --- a/group24/330657387/src/test/week01/data_structure/LinkedListTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package test.week01.data_structure; - -import static org.junit.Assert.*; -import main.week01.data_structure.LinkedList; -import main.week01.data_structure.LinkedList.LinkedListIterator; - -import org.junit.Before; -import org.junit.Test; - -public class LinkedListTest { - - private LinkedList list; - - @Before - public void setUp() throws Exception { - list = new LinkedList(); - } - - @Test - public void testGet() { - list.add("A"); - list.add("B"); - list.add(0, "C"); - assertEquals("C", list.get(0)); - } - - @Test - public void testRemove() { - list.add("A"); - list.add("B"); - list.add("C"); - list.add("D"); - list.add(0, "E"); - assertEquals("E", list.remove(0)); - assertEquals("D", list.remove(list.size()-1)); - assertEquals(3, list.size()); - } - - @Test - public void testIterator() { - LinkedListIterator iter = list.iterator(); - list.add("A"); - list.add("B"); - list.add(0, "C"); - assertTrue(iter.hasNext()); - assertEquals("C", iter.next()); - assertTrue(iter.hasNext()); - assertEquals("A", iter.next()); - assertTrue(iter.hasNext()); - assertEquals("B", iter.next()); - assertFalse(iter.hasNext()); - } - -} diff --git "a/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" "b/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" index 76ae770288..79237635d2 100644 --- "a/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" +++ "b/group24/330657387/\345\215\232\345\256\242\345\234\260\345\235\200.txt" @@ -1,6 +1,6 @@ һܲhttp://www.cnblogs.com/sargeles/p/6605493.html ڶܲhttp://www.cnblogs.com/sargeles/p/6605945.html -ܲ +ܲhttp://www.cnblogs.com/sargeles/p/6667002.html ܲ ܲ ܲ