From 189e34b440e2b6dea6355be38bab4178676711af Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Thu, 30 Mar 2017 15:43:08 +0800 Subject: [PATCH] Finish multiple thread file download --- .../src/com/basic/ArrayList.java | 48 ++-- .../src/com/basic/BinaryTreeNode.java | 57 ++--- .../src/com/basic/LinkedList.java | 230 ++++++------------ .../src/com/basic/Queue.java | 12 +- .../src/com/basic/Stack.java | 15 +- .../src/testcase/TestArrayList.java | 110 +++------ .../src/testcase/TestBinaryTreeNode.java | 3 +- .../src/testcase/TestLinkedList.java | 86 ++----- .../src/testcase/TestQueue.java | 6 +- .../src/testcase/TestStack.java | 6 +- .../src/com/basic/ArrayUtil.java | 127 ++++------ .../src/com/struts/DomXmlHelper.java | 34 +-- .../src/com/struts/LoginAction.java | 21 +- .../src/com/struts/Struts.java | 9 +- .../src/com/struts/View.java | 12 +- .../src/testcase/StrutsTest.java | 6 +- .../src/testcase/TestArrayUtil.java | 30 +-- .../coderising/download/DownloadThread.java | 107 ++++++++ .../coderising/download/FileDownloader.java | 124 ++++++++++ .../coderising/download/api/Connection.java | 29 +++ .../download/api/ConnectionException.java | 6 + .../download/api/ConnectionManager.java | 12 + .../download/api/DownloadListener.java | 6 + .../download/impl/ConnectionImpl.java | 93 +++++++ .../download/impl/ConnectionManagerImpl.java | 19 ++ .../src/com/coderising/helper/Tool.java | 59 +++++ .../src/testcase/FileDownloaderTest.java | 78 ++++++ 27 files changed, 795 insertions(+), 550 deletions(-) create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/DownloadThread.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/FileDownloader.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/api/Connection.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionException.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/api/DownloadListener.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group24/1525619747/homework_20170328/src/com/coderising/helper/Tool.java create mode 100644 group24/1525619747/homework_20170328/src/testcase/FileDownloaderTest.java diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java index 008b390255..d60c6b8c0c 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -9,36 +9,29 @@ public class ArrayList implements List private Object[] elementData = new Object[MAXNSIZE]; - public void add(Object o) - { - if (size > MAXNSIZE) - { + public void add(Object o) { + if (size > MAXNSIZE) { String errorInfo = "Out of max size" + MAXNSIZE; throw new ArrayIndexOutOfBoundsException(errorInfo); } elementData[size++] = o; } - public void add(int index, Object o) - { - if (index >= size && size > 0) - { + public void add(int index, Object o) { + if (index >= size && size > 0) { String errorInfo = "Index to add: " + index + " is out of current size: " + size; throw new ArrayIndexOutOfBoundsException(errorInfo); } - for (int i = size; i > index; i--) - { + for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = o; ++size; } - public Object get(int index) - { - if (index < 0 || index >= size) - { + public Object get(int index) { + if (index < 0 || index >= size) { String errorInfo = "Index to get: " + index + " is invalid, current range: 0 - " + (size - 1); throw new ArrayIndexOutOfBoundsException(errorInfo); @@ -46,45 +39,36 @@ public Object get(int index) return elementData[index]; } - public Object remove(int index) - { - if (index < 0 || index >= size) - { + public Object remove(int index) { + if (index < 0 || index >= size) { String errorInfo = "Index to remove: " + index + " is invalid, current range: 0 - " + (size - 1); throw new ArrayIndexOutOfBoundsException(errorInfo); } Object o = elementData[index]; - for (int i = index; i < size - 1; i++) - { + for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } elementData[size--] = null; return o; } - public int size() - { + public int size() { return size; } - public Iterator iterator() - { - return new Iterator() - { + public Iterator iterator() { + return new Iterator() { private int index = 0; - public boolean hasNext() - { + public boolean hasNext() { return (index < size); } - public Object next() - { - if (hasNext()) - { + public Object next() { + if (hasNext()) { return elementData[index++]; } return null; diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java index 63899d38ce..543b8bfb85 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -7,75 +7,57 @@ public class BinaryTreeNode private BinaryTreeNode left; private BinaryTreeNode right; - public BinaryTreeNode (Object data) - { + public BinaryTreeNode (Object data) { this.data = data; left = null; right = null; } public BinaryTreeNode (Object data, BinaryTreeNode left, - BinaryTreeNode right) - { + BinaryTreeNode right) { this.data = data; this.left = left; this.right = right; } - public Object getData() - { + public Object getData() { return data; } - public void setData(Object data) - { + public void setData(Object data) { this.data = data; } - public BinaryTreeNode getLeft() - { + public BinaryTreeNode getLeft() { return left; } - public void setLeft(BinaryTreeNode left) - { + public void setLeft(BinaryTreeNode left) { this.left = left; } - public BinaryTreeNode getRight() - { + public BinaryTreeNode getRight() { return right; } - public void setRight(BinaryTreeNode right) - { + public void setRight(BinaryTreeNode right) { this.right = right; } /* * 排序二叉树的插入 */ - public BinaryTreeNode insert(Object o) - { - if (((Integer) data) > ((Integer) o)) - { - if (left == null) - { + public BinaryTreeNode insert(Object o) { + if (((Integer) data) > ((Integer) o)) { + if (left == null) { setLeft(new BinaryTreeNode(o)); - } - else - { + } else { left.insert(o); } - } - else - { - if (right == null) - { + } else { + if (right == null) { setRight(new BinaryTreeNode(o)); - } - else - { + } else { right.insert(o); } } @@ -85,17 +67,14 @@ public BinaryTreeNode insert(Object o) /* * 前序遍历 */ - public void preOrderInterator() - { - if (left != null) - { + public void preOrderInterator() { + if (left != null) { left.preOrderInterator(); } System.out.print(data.toString() + " "); - if (right != null) - { + if (right != null) { right.preOrderInterator(); } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java index e741421aad..61be6bd5f6 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -5,17 +5,12 @@ public class LinkedList implements List private Node head; - public void add(Object o) - { - if (head == null) - { + public void add(Object o) { + if (head == null) { head = new Node(o, null); - } - else - { + } else { Node nodePointer = head; - while (nodePointer.next != null) - { + while (nodePointer.next != null) { nodePointer = nodePointer.next; } nodePointer.next = new Node(o, null); @@ -23,30 +18,25 @@ public void add(Object o) } - public void add(int index, Object o) - { + public void add(int index, Object o) { int size = size(); - if (index < 0 || index > size) - { + if (index < 0 || index > size) { String errorInfo = "Invalid index to add:" + index + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } int step = 0; Node nodePointer = head; - while (step < index) - { + while (step < index) { nodePointer = nodePointer.next; ++step; } nodePointer.next = new Node(o, nodePointer.next); } - public Object get(int index) - { + public Object get(int index) { int size = size(); - if (index < 0 || index > size) - { + if (index < 0 || index > size) { String errorInfo = "Invalid index to get:" + index + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); @@ -54,8 +44,7 @@ public Object get(int index) int step = 0; Node nodePointer = head; - while (step < index) - { + while (step < index) { nodePointer = nodePointer.next; ++step; } @@ -63,11 +52,9 @@ public Object get(int index) return nodePointer.data; } - public Object remove(int index) - { + public Object remove(int index) { int size = size(); - if (index < 0 || index > size) - { + if (index < 0 || index > size) { String errorInfo = "Invalid index to remove:" + index + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); @@ -77,8 +64,7 @@ public Object remove(int index) Node nodePointer = head; Node lastPointer = head; - while (step < index) - { + while (step < index) { lastPointer = nodePointer; nodePointer = nodePointer.next; ++step; @@ -86,15 +72,12 @@ public Object remove(int index) Object o = null; - if (lastPointer == nodePointer) - { + if (lastPointer == nodePointer) { Node toDelete = head; o = toDelete.data; head = head.next; toDelete = null; - } - else - { + } else { o = nodePointer.data; lastPointer.next = nodePointer.next; nodePointer = null; @@ -103,15 +86,12 @@ public Object remove(int index) return o; } - public int size() - { + public int size() { int size = 0; - if (head != null) - { + if (head != null) { ++size; Node nodePointer = head; - while (nodePointer.next != null) - { + while (nodePointer.next != null) { ++size; nodePointer = nodePointer.next; } @@ -119,37 +99,30 @@ public int size() return size; } - public void addFirst(Object o) - { - if (head == null) - { + public void addFirst(Object o) { + if (head == null) { head = new Node(o, null); return; } head = new Node(o, head); } - public void addLast(Object o) - { - if (head == null) - { + public void addLast(Object o) { + if (head == null) { head = new Node(o, null); return; } Node nodePointer = head; - while (nodePointer.next != null) - { + while (nodePointer.next != null) { nodePointer = nodePointer.next; } nodePointer.next = new Node(o, null); } @SuppressWarnings ("unused") - public Object removeFirst() - { - if (head == null) - { + public Object removeFirst() { + if (head == null) { return null; } @@ -161,18 +134,15 @@ public Object removeFirst() return o; } - public Object removeLast() - { - if (head == null) - { + public Object removeLast() { + if (head == null) { return null; } Node nodePointer = head; Node lastPointer = head; - while (nodePointer.next != null) - { + while (nodePointer.next != null) { lastPointer = nodePointer; nodePointer = nodePointer.next; } @@ -183,24 +153,19 @@ public Object removeLast() return o; } - public Iterator iterator() - { - return new Iterator() - { + public Iterator iterator() { + return new Iterator() { private Node nodePointer = head; - public boolean hasNext() - { + public boolean hasNext() { // TODO Auto-generated method stub return (nodePointer != null); } - public Object next() - { + public Object next() { // TODO Auto-generated method stub - if (hasNext()) - { + if (hasNext()) { Object o = nodePointer.data; nodePointer = nodePointer.next; return o; @@ -215,8 +180,7 @@ private static class Node Object data; Node next; - public Node (Object o, Node n) - { + public Node (Object o, Node n) { this.data = o; this.next = n; } @@ -225,16 +189,13 @@ public Node (Object o, Node n) /** * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ - public void reverse() - { - if (head == null) - { + public void reverse() { + if (head == null) { return; } Node reverse = null; - while (size() > 0) - { + while (size() > 0) { reverse = new Node(removeFirst(), reverse); } @@ -246,13 +207,11 @@ public void reverse() * ,删除以后的值为7,8,10 */ @SuppressWarnings ("unused") - public void removeFirstHalf() - { + public void removeFirstHalf() { int removeLength = size() / 2; int step = 0; Node toDelete = null; - while (step < removeLength) - { + while (step < removeLength) { toDelete = head; head = head.next; toDelete = null; @@ -266,18 +225,15 @@ public void removeFirstHalf() * @param i * @param length */ - public void remove(int i, int length) - { + public void remove(int i, int length) { int size = size(); - if (i >= size) - { + if (i >= size) { return; } Node nodePointer = head; Node lastPointer = head; int step = 0; - while (step < i) - { + while (step < i) { lastPointer = nodePointer; nodePointer = nodePointer.next; ++step; @@ -285,20 +241,15 @@ public void remove(int i, int length) step = 0; Node toDelete = null; - if (lastPointer == head) - { - while (step < length) - { + if (lastPointer == head) { + while (step < length) { toDelete = head; head = head.next; toDelete = null; ++step; } - } - else - { - while (step < length) - { + } else { + while (step < length) { toDelete = nodePointer; nodePointer = nodePointer.next; toDelete = null; @@ -316,13 +267,11 @@ public void remove(int i, int length) * * @param list */ - public int[] getElements(LinkedList list) - { + public int[] getElements(LinkedList list) { int[] elements = new int[list.size()]; Iterator it = list.iterator(); int index = 0; - for (; it.hasNext();) - { + for (; it.hasNext();) { elements[index++] = (Integer) get((Integer) (it.next())); } return elements; @@ -334,29 +283,22 @@ public int[] getElements(LinkedList list) * @param list */ - public void subtract(LinkedList list) - { - if (head == null) - { + public void subtract(LinkedList list) { + if (head == null) { return; } Node nodePointer = head; Node lastPointer = head; Node toDelte = null; - while (nodePointer != null) - { - if (list.contain(nodePointer.data)) - { - if (nodePointer == head) - { + while (nodePointer != null) { + if (list.contain(nodePointer.data)) { + if (nodePointer == head) { toDelte = head; head = head.next; toDelte = null; nodePointer = head; lastPointer = head; - } - else - { + } else { toDelte = nodePointer; lastPointer.next = nodePointer.next; toDelte = null; @@ -368,13 +310,10 @@ public void subtract(LinkedList list) } - private boolean contain(Object o) - { + private boolean contain(Object o) { Node nodePointer = head; - while (nodePointer != null) - { - if (nodePointer.data.equals(o)) - { + while (nodePointer != null) { + if (nodePointer.data.equals(o)) { return true; } nodePointer = nodePointer.next; @@ -385,20 +324,17 @@ private boolean contain(Object o) /** * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ - public void removeDuplicateValues() - { + public void removeDuplicateValues() { Node nodePointer = head; - if (nodePointer.next == null) - { + if (nodePointer.next == null) { return; } Node toDelete = null; - while (nodePointer.next != null) - { + while (nodePointer.next != null) { while (nodePointer.next != null - && nodePointer.data.equals(nodePointer.next.data)) - { // delete nodePointer.next + && nodePointer.data.equals(nodePointer.next.data)) { // delete + // nodePointer.next toDelete = nodePointer.next; nodePointer.next = nodePointer.next.next; toDelete = null; @@ -413,34 +349,27 @@ public void removeDuplicateValues() * @param min * @param max */ - public void removeRange(int min, int max) - { + public void removeRange(int min, int max) { Node nodePointer = head; Node lastPointer = head; - if (nodePointer == null) - { + if (nodePointer == null) { return; } while (nodePointer != null - && ((Integer) nodePointer.data) <= (new Integer(min))) - { + && ((Integer) nodePointer.data) <= (new Integer(min))) { lastPointer = nodePointer; nodePointer = nodePointer.next; } Node toDelete = null; while (nodePointer != null - && ((Integer) nodePointer.data) < (new Integer(max))) - { - if (nodePointer == head) - { + && ((Integer) nodePointer.data) < (new Integer(max))) { + if (nodePointer == head) { toDelete = head; head = head.next; toDelete = null; nodePointer = head; lastPointer = head; - } - else - { + } else { toDelete = nodePointer; lastPointer.next = nodePointer.next; nodePointer = nodePointer.next; @@ -455,39 +384,30 @@ public void removeRange(int min, int max) * * @param list */ - public LinkedList intersection(LinkedList list) - { + public LinkedList intersection(LinkedList list) { LinkedList linkedList = new LinkedList(); Iterator it1 = iterator(); Iterator it2 = list.iterator(); Object o1 = null; Object o2 = null; - if (size() == 0 || list.size() == 0) - { + if (size() == 0 || list.size() == 0) { return null; } o1 = it1.next(); o2 = it2.next(); - while (o1 != null && o2 != null) - { + while (o1 != null && o2 != null) { // System.out.println(o1 + " " + o2); - if (((Integer) o1) == ((Integer) o2)) - { + if (((Integer) o1) == ((Integer) o2)) { linkedList.add(o1); o1 = it1.next(); o2 = it2.next(); - } - else - { - if (((Integer) o1) > ((Integer) o2)) - { + } else { + if (((Integer) o1) > ((Integer) o2)) { o2 = it2.next(); - } - else - { + } else { o1 = it1.next(); } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/Queue.java b/group24/1525619747/homework_20170312/src/com/basic/Queue.java index b040c85722..67cde6b46b 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/Queue.java +++ b/group24/1525619747/homework_20170312/src/com/basic/Queue.java @@ -4,23 +4,19 @@ public class Queue { private LinkedList list = new LinkedList(); - public void enQueue(Object o) - { + public void enQueue(Object o) { list.addLast(o); } - public Object deQueue() - { + public Object deQueue() { return list.removeFirst(); } - public boolean isEmpty() - { + public boolean isEmpty() { return (list.size() == 0); } - public int size() - { + public int size() { return list.size(); } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/Stack.java b/group24/1525619747/homework_20170312/src/com/basic/Stack.java index db7ada4c53..09fe1a6f7d 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/Stack.java +++ b/group24/1525619747/homework_20170312/src/com/basic/Stack.java @@ -4,28 +4,23 @@ public class Stack { private ArrayList elementData = new ArrayList(); - public void push(Object o) - { + public void push(Object o) { elementData.add(0, o); } - public Object pop() - { + public Object pop() { return elementData.remove(0); } - public Object peek() - { + public Object peek() { return elementData.get(0); } - public boolean isEmpty() - { + public boolean isEmpty() { return (elementData.size() == 0); } - public int size() - { + public int size() { return elementData.size(); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java index 134a541265..643333b851 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -11,27 +11,19 @@ public class TestArrayList { @Test - public void testAdd() - { + public void testAdd() { ArrayList list = new ArrayList(); - try - { + try { list.add(3); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } assertTrue(list.get(0).equals(3)); - try - { - for (int i = 0; i < 100; i++) - { + try { + for (int i = 0; i < 100; i++) { list.add(i); } - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); // System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); @@ -43,21 +35,16 @@ public void testAdd() * test add(index, o) */ @Test - public void testIndexAdd() - { + public void testIndexAdd() { ArrayList list = new ArrayList(); - try - { - for (int i = 0; i < 10; i++) - { + try { + for (int i = 0; i < 10; i++) { list.add(i); } list.add(3, 20); list.add(0, 30); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } @@ -71,15 +58,11 @@ public void testIndexAdd() assertTrue(list.get(4).equals(20)); assertTrue(list.get(5).equals(3)); - try - { - for (int i = 0; i < 100; i++) - { + try { + for (int i = 0; i < 100; i++) { list.add(i, i); } - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); // System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); @@ -90,35 +73,24 @@ public void testIndexAdd() * test get(index) */ @Test - public void testGet() - { + public void testGet() { ArrayList list = new ArrayList(); - try - { - for (int i = 0; i < 10; i++) - { + try { + for (int i = 0; i < 10; i++) { list.add(i); } - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } - try - { + try { list.get(0); list.get(5); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } - try - { + try { list.get(10); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); // System.out.println(e.getMessage()); // System.out.println(list.size()); @@ -132,60 +104,44 @@ public void testGet() * test remove(index) and size */ @Test - public void testRemove() - { + public void testRemove() { ArrayList list = new ArrayList(); - try - { - for (int i = 0; i < 10; i++) - { + try { + for (int i = 0; i < 10; i++) { list.add(i); } assertTrue(list.size() == 10); list.remove(3); assertTrue(list.size() == 9); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } assertFalse(list.get(3).equals(3)); assertTrue(list.get(3).equals(4)); - try - { + try { list.remove(-3); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); // System.out.println(e.getMessage()); } - try - { + try { list.remove(20); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); // System.out.println(e.getMessage()); } } @Test - public void testInterator() - { + public void testInterator() { ArrayList list = new ArrayList(); - try - { - for (int i = 0; i < 10; i++) - { + try { + for (int i = 0; i < 10; i++) { list.add(i); } - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java index 472b9a7fb0..fa934ec025 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java @@ -7,8 +7,7 @@ public class TestBinaryTreeNode { @Test - public void testBinaryTree() - { + public void testBinaryTree() { BinaryTreeNode binNode = new BinaryTreeNode(5); binNode.insert(1); binNode.insert(10); diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java index 05ac118a90..0b733821a4 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -14,8 +14,7 @@ public class TestLinkedList * test add() and size() */ @Test - public void testAdd() - { + public void testAdd() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); @@ -23,8 +22,7 @@ public void testAdd() } @Test - public void testGet() - { + public void testGet() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); @@ -32,12 +30,9 @@ public void testGet() assertFalse(linkedList.get(0).equals("hello world")); assertTrue(linkedList.get(1).equals("hello world")); - try - { + try { linkedList.get(-1); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); String errorInfo = "Invalid index to get:" + -1 + " out of range: [0," + linkedList.size() + "]"; @@ -47,32 +42,25 @@ public void testGet() } @Test - public void testRemove() - { + public void testRemove() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); linkedList.add(4); linkedList.add("Leon Deng"); - try - { + try { linkedList.remove(-1); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); String errorInfo = "Invalid index to remove:" + -1 + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); } - try - { + try { linkedList.remove(10); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); String errorInfo = "Invalid index to remove:" + 10 + " out of range: [0," + linkedList.size() + "]"; @@ -80,22 +68,16 @@ public void testRemove() } Object o = null; - try - { + try { o = linkedList.remove(0); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } assertTrue(o.equals(3)); - try - { + try { o = linkedList.remove(2); - } - catch (ArrayIndexOutOfBoundsException e) - { + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } // System.out.println(o); @@ -103,8 +85,7 @@ public void testRemove() } @Test - public void testAddFirst() - { + public void testAddFirst() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); @@ -115,8 +96,7 @@ public void testAddFirst() } @Test - public void testAddLast() - { + public void testAddLast() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); @@ -126,8 +106,7 @@ public void testAddLast() } @Test - public void testRemoveFirst() - { + public void testRemoveFirst() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); @@ -139,8 +118,7 @@ public void testRemoveFirst() } @Test - public void testRemoveLast() - { + public void testRemoveLast() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); @@ -153,8 +131,7 @@ public void testRemoveLast() } @Test - public void testInterator() - { + public void testInterator() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("Leon Deng"); @@ -168,8 +145,7 @@ public void testInterator() } @Test - public void testReverse() - { + public void testReverse() { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add(4); @@ -210,8 +186,7 @@ public void testReverse() } @Test - public void testRemoveFirstHalf() - { + public void testRemoveFirstHalf() { LinkedList linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); @@ -239,8 +214,7 @@ public void testRemoveFirstHalf() } @Test - public void testRemoveIndexLength() - { + public void testRemoveIndexLength() { LinkedList linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); @@ -271,8 +245,7 @@ public void testRemoveIndexLength() } @Test - public void testGetElements() - { + public void testGetElements() { // 11->101->201->301->401->501->601->701 listB = 1->3->4->6 LinkedList linkedList = new LinkedList(); linkedList.add(11); @@ -304,8 +277,7 @@ public void testGetElements() } @Test - public void testSubstract() - { + public void testSubstract() { LinkedList linkedList = new LinkedList(); linkedList.add(11); linkedList.add(101); @@ -336,8 +308,7 @@ public void testSubstract() } @Test - public void testRemoveDuplicateValues() - { + public void testRemoveDuplicateValues() { LinkedList linkedList = new LinkedList(); linkedList.add(1); linkedList.add(1); @@ -363,11 +334,9 @@ public void testRemoveDuplicateValues() } @Test - public void testRemoveRange() - { + public void testRemoveRange() { LinkedList linkedList = new LinkedList(); - for (int i = 0; i < 10; i++) - { + for (int i = 0; i < 10; i++) { linkedList.add(i); } @@ -383,8 +352,7 @@ public void testRemoveRange() } @Test - public void testIntersection() - { + public void testIntersection() { LinkedList linkedList = new LinkedList(); linkedList.add(1); linkedList.add(2); diff --git a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java index 390238efc9..75b129ff17 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java @@ -12,14 +12,12 @@ public class TestQueue * test enQueue() isEmpty() and size() deQueue */ @Test - public void testQueue() - { + public void testQueue() { Queue queue = new Queue(); assertTrue(queue.isEmpty()); - for (int i = 10; i < 20; i++) - { + for (int i = 10; i < 20; i++) { queue.enQueue(i); } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestStack.java b/group24/1525619747/homework_20170312/src/testcase/TestStack.java index 2091577ec1..bf91470c5c 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestStack.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestStack.java @@ -9,14 +9,12 @@ public class TestStack { @Test - public void testStack() - { + public void testStack() { Stack stack = new Stack(); assertTrue(stack.isEmpty()); - for (int i = 10; i < 20; i++) - { + for (int i = 10; i < 20; i++) { stack.push(i); } diff --git a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java index 9b4b81c12d..42b712f5bc 100644 --- a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java +++ b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java @@ -12,21 +12,18 @@ public class ArrayUtil * @param origin * @return */ - public void reverseArray(int[] origin) - { + public void reverseArray(int[] origin) { int length = origin.length; int left = 0; int right = length - 1; int a = 0; int b = 0; - while (left < right) - { + while (left < right) { swap(origin, left++, right--); } } - private void swap(int[] origin, int i, int j) - { + private void swap(int[] origin, int i, int j) { // TODO Auto-generated method stub int tmp = origin[i]; origin[i] = origin[j]; @@ -41,29 +38,23 @@ private void swap(int[] origin, int i, int j) * @return */ - public int[] removeZero(int[] oldArray) - { + public int[] removeZero(int[] oldArray) { int length = oldArray.length; int[] newArray = new int[length]; int[] zeroArray = new int[length]; int zIndex = 0; int nzIndex = 0; - for (int i = 0; i < length; i++) - { - if (oldArray[i] == 0) - { + for (int i = 0; i < length; i++) { + if (oldArray[i] == 0) { zeroArray[zIndex++] = oldArray[i]; - } - else - { + } else { newArray[nzIndex++] = oldArray[i]; } } int[] newArray2 = new int[nzIndex]; - for (int i = 0; i < nzIndex; i++) - { + for (int i = 0; i < nzIndex; i++) { newArray2[i] = newArray[i]; } @@ -79,8 +70,7 @@ public int[] removeZero(int[] oldArray) * @return */ - public int[] merge(int[] array1, int[] array2) - { + public int[] merge(int[] array1, int[] array2) { int length1 = array1.length; int length2 = array2.length; int[] array3 = new int[length1 + length2]; @@ -89,56 +79,46 @@ public int[] merge(int[] array1, int[] array2) int index2 = 0; int index3 = 0; - while (index1 < length1 && index2 < length2) - { + while (index1 < length1 && index2 < length2) { - if (index3 > 0) - { - if (array3[index3 - 1] == array1[index1]) - { + if (index3 > 0) { + if (array3[index3 - 1] == array1[index1]) { ++index1; continue; } - if (array3[index3 - 1] == array2[index2]) - { + if (array3[index3 - 1] == array2[index2]) { ++index2; continue; } } - if (array1[index1] == array2[index2]) - { + if (array1[index1] == array2[index2]) { array3[index3++] = array1[index1]; ++index1; ++index2; continue; } - if (array1[index1] < array2[index2]) - { + if (array1[index1] < array2[index2]) { array3[index3++] = array1[index1]; ++index1; continue; } - if (array1[index1] > array2[index2]) - { + if (array1[index1] > array2[index2]) { array3[index3++] = array2[index2]; ++index2; continue; } } - while (index1 < length1) - { + while (index1 < length1) { array3[index3++] = array1[index1++]; } - while (index2 < length2) - { + while (index2 < length2) { array3[index3++] = array1[index2++]; } int[] newArray = new int[index3]; - for (int i = 0; i < index3; i++) - { + for (int i = 0; i < index3; i++) { newArray[i] = array3[i]; } @@ -154,16 +134,13 @@ public int[] merge(int[] array1, int[] array2) * @param size * @return */ - public int[] grow(int[] oldArray, int size) - { + public int[] grow(int[] oldArray, int size) { int length = oldArray.length; int[] newArr = new int[length + size]; - for (int i = 0; i < length; i++) - { + for (int i = 0; i < length; i++) { newArr[i] = oldArray[i]; } - for (int i = length; i < length + size; i++) - { + for (int i = length; i < length + size; i++) { newArr[i] = 0; } return newArr; @@ -176,10 +153,8 @@ public int[] grow(int[] oldArray, int size) * @param max * @return */ - public int[] fibonacci(int max) - { - if (max == 1) - { + public int[] fibonacci(int max) { + if (max == 1) { return null; } int[] arr = new int[max / 2]; @@ -191,8 +166,7 @@ public int[] fibonacci(int max) arr[1] = 1; int index = 2; - while (a + b < max) - { + while (a + b < max) { arr[index++] = a + b; c = b; b = a + b; @@ -200,8 +174,7 @@ public int[] fibonacci(int max) } int[] newArr = new int[index]; - for (int i = 0; i < index; i++) - { + for (int i = 0; i < index; i++) { newArr[i] = arr[i]; } @@ -214,34 +187,27 @@ public int[] fibonacci(int max) * @param max * @return */ - public int[] getPrimes(int max) - { + public int[] getPrimes(int max) { int size = max / 2 + 1; int[] arr = new int[size]; int index = 0; - for (int i = 2; i < max; i++) - { - if (isPrime(i)) - { + for (int i = 2; i < max; i++) { + if (isPrime(i)) { arr[index++] = i; } } int[] newArr = new int[index]; - for (int i = 0; i < index; i++) - { + for (int i = 0; i < index; i++) { newArr[i] = arr[i]; } return newArr; } - public boolean isPrime(int i) - { - for (int j = 2; j < i / 2; j++) - { - if (i % j == 0) - { + public boolean isPrime(int i) { + for (int j = 2; j < i / 2; j++) { + if (i % j == 0) { return false; } } @@ -254,35 +220,28 @@ public boolean isPrime(int i) * @param max * @return */ - public int[] getPerfectNumbers(int max) - { + public int[] getPerfectNumbers(int max) { int size = max / 2 + 1; int[] arr = new int[size]; int index = 0; - for (int i = 2; i < max; i++) - { - if (isPerfectNumber(i)) - { + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { arr[index++] = i; } } int[] newArr = new int[index]; - for (int i = 0; i < index; i++) - { + for (int i = 0; i < index; i++) { newArr[i] = arr[i]; } return newArr; } - public boolean isPerfectNumber(int num) - { + public boolean isPerfectNumber(int num) { int sum = 0; - for (int i = 1; i <= num / 2; i++) - { - if (num % i == 0) - { + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0) { sum += i; } } @@ -297,12 +256,10 @@ public boolean isPerfectNumber(int num) * @param s * @return */ - public String join(int[] array, String seperator) - { + public String join(int[] array, String seperator) { String str = ""; int length = array.length; - for (int a : array) - { + for (int a : array) { str += a + seperator; } str = str.substring(0, str.length() - 1); diff --git a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java index 31d27b592c..4683947920 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java +++ b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java @@ -21,22 +21,17 @@ public class DomXmlHelper private Map kv = new HashMap(); - public DomXmlHelper () throws DocumentException - { + public DomXmlHelper () throws DocumentException { fetchAttributes(); } // 遍历当前节点下的所有节点 - private void listNodes(Element node, String loop, Map kv) - { + private void listNodes(Element node, String loop, Map kv) { // System.out.println("当前节点的名称:" + node.getName()); - if (loop.equals("")) - { + if (loop.equals("")) { loop += node.getName(); - } - else - { + } else { kv.put(loop, node.getName()); loop += "-" + node.getName(); } @@ -44,8 +39,7 @@ private void listNodes(Element node, String loop, Map kv) // 首先获取当前节点的所有属性节点 List list = node.attributes(); // 遍历属性节点 - for (Attribute attribute : list) - { + for (Attribute attribute : list) { // System.out.println("属性 "+attribute.getName() +":" + // attribute.getValue()); kv.put(loop, attribute.getValue()); @@ -54,8 +48,7 @@ private void listNodes(Element node, String loop, Map kv) } // 如果当前节点内容不为空,则输出 - if (!(node.getTextTrim().equals(""))) - { + if (!(node.getTextTrim().equals(""))) { // System.out.println("内容 " + node.getName() + ":" + // node.getText()); kv.put(loop, node.getText()); @@ -66,15 +59,13 @@ private void listNodes(Element node, String loop, Map kv) // 同时迭代当前节点下面的所有子节点 // 使用递归 Iterator iterator = node.elementIterator(); - while (iterator.hasNext()) - { + while (iterator.hasNext()) { Element e = iterator.next(); listNodes(e, loop, kv); } } - private void fetchAttributes() throws DocumentException - { + private void fetchAttributes() throws DocumentException { // 创建SAXReader对象 SAXReader reader = new SAXReader(); // 读取文件 转换成Document @@ -86,23 +77,20 @@ private void fetchAttributes() throws DocumentException listNodes(root, "", kv); - for (Map.Entry entity : kv.entrySet()) - { + for (Map.Entry entity : kv.entrySet()) { // System.out.println("key: " + entity.getKey() + " , value: " + // entity.getValue()); } } - public String getActionView(String action, String method) - { + public String getActionView(String action, String method) { String key = "struts-action-" + action; String className = kv.get(key); key += "-" + className + "-result-" + method; return kv.get(key); } - public String getActionClassByName(String action) - { + public String getActionClassByName(String action) { String key = "struts-action-" + action; return kv.get(key); } diff --git a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java index 6d1d5a2ca6..91527ca78e 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java +++ b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java @@ -12,21 +12,17 @@ public class LoginAction private String password; private String message; - public String getName() - { + public String getName() { return name; } - public String getPassword() - { + public String getPassword() { return password; } - public String execute() - { + public String execute() { System.out.println("name: " + name + " , password: " + password); - if ("test".equals(name) && "1234".equals(password)) - { + if ("test".equals(name) && "1234".equals(password)) { this.message = "login successful"; return "success"; } @@ -34,18 +30,15 @@ public String execute() return "fail"; } - public void setName(String name) - { + public void setName(String name) { this.name = name; } - public void setPassword(String password) - { + public void setPassword(String password) { this.password = password; } - public String getMessage() - { + public String getMessage() { return this.message; } } diff --git a/group24/1525619747/homework_20170319/src/com/struts/Struts.java b/group24/1525619747/homework_20170319/src/com/struts/Struts.java index f0ff145909..af2a5ce5c0 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/Struts.java +++ b/group24/1525619747/homework_20170319/src/com/struts/Struts.java @@ -16,8 +16,7 @@ public static View runAction(String actionName, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchFieldException - { + NoSuchFieldException { /* * @@ -45,8 +44,7 @@ public static View runAction(String actionName, class1 = Class.forName(className); // System.out.println("类名称 " + class1.getName()); - if (class1 != null) - { + if (class1 != null) { // 调用class1的setName方法, 待参数 // 根据.class反射出来的类实例 Object instance = class1.newInstance(); @@ -58,8 +56,7 @@ public static View runAction(String actionName, Object res2 = method.invoke(instance, "1234"); // set attr - for (Map.Entry entity : parameters.entrySet()) - { + for (Map.Entry entity : parameters.entrySet()) { String attrName = entity.getKey(); String attrValue = entity.getValue(); diff --git a/group24/1525619747/homework_20170319/src/com/struts/View.java b/group24/1525619747/homework_20170319/src/com/struts/View.java index b5b9e2802b..15125a7f04 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/View.java +++ b/group24/1525619747/homework_20170319/src/com/struts/View.java @@ -7,24 +7,20 @@ public class View private String jsp; private Map parameters; - public String getJsp() - { + public String getJsp() { return jsp; } - public View setJsp(String jsp) - { + public View setJsp(String jsp) { this.jsp = jsp; return this; } - public Map getParameters() - { + public Map getParameters() { return parameters; } - public View setParameters(Map parameters) - { + public View setParameters(Map parameters) { this.parameters = parameters; return this; } diff --git a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java index 61ef07748f..0f9f66b87b 100644 --- a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java +++ b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java @@ -18,8 +18,7 @@ public class StrutsTest public void testLoginActionSuccess() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, - InvocationTargetException, DocumentException, NoSuchFieldException - { + InvocationTargetException, DocumentException, NoSuchFieldException { String actionName = "login"; @@ -38,8 +37,7 @@ public void testLoginActionSuccess() throws ClassNotFoundException, public void testLoginActionFailed() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, - InvocationTargetException, DocumentException, NoSuchFieldException - { + InvocationTargetException, DocumentException, NoSuchFieldException { String actionName = "login"; Map params = new HashMap(); params.put("name", "test"); diff --git a/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java index fffa56eac1..7b7f69c570 100644 --- a/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java +++ b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java @@ -8,10 +8,8 @@ public class TestArrayUtil { - private void print_r(int[] a) - { - for (int i = 0; i < a.length; i++) - { + private void print_r(int[] a) { + for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); @@ -25,8 +23,7 @@ private void print_r(int[] a) } @Test - public void testReverseArray() - { + public void testReverseArray() { ArrayUtil arrayUtil = new ArrayUtil(); int[] a = { 7, 9, 30, 3 }; @@ -50,8 +47,7 @@ public void testReverseArray() } @Test - public void testRemoveZero() - { + public void testRemoveZero() { ArrayUtil arrayUtil = new ArrayUtil(); int[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; @@ -63,8 +59,7 @@ public void testRemoveZero() } @Test - public void testMerge() - { + public void testMerge() { ArrayUtil arrayUtil = new ArrayUtil(); int[] a1 = { 3, 5, 7, 8 }; @@ -79,8 +74,7 @@ public void testMerge() } @Test - public void testGrow() - { + public void testGrow() { ArrayUtil arrayUtil = new ArrayUtil(); int[] a1 = { 3, 5, 7, 8 }; @@ -96,8 +90,7 @@ public void testGrow() } @Test - public void testFibonacci() - { + public void testFibonacci() { ArrayUtil arrayUtil = new ArrayUtil(); int max = 100; int[] arr = arrayUtil.fibonacci(max); @@ -112,8 +105,7 @@ public void testFibonacci() } @Test - public void testGetPrimes() - { + public void testGetPrimes() { ArrayUtil arrayUtil = new ArrayUtil(); int max = 23; int[] arr = arrayUtil.getPrimes(max); @@ -126,8 +118,7 @@ public void testGetPrimes() } @Test - public void testGetPerfectNumbers() - { + public void testGetPerfectNumbers() { ArrayUtil arrayUtil = new ArrayUtil(); int max = 300; int[] arr = arrayUtil.getPerfectNumbers(max); @@ -140,8 +131,7 @@ public void testGetPerfectNumbers() } @Test - public void testJoin() - { + public void testJoin() { ArrayUtil arrayUtil = new ArrayUtil(); int[] a = { 3, 8, 9 }; String str = arrayUtil.join(a, "-"); diff --git a/group24/1525619747/homework_20170328/src/com/coderising/download/DownloadThread.java b/group24/1525619747/homework_20170328/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..4c4ba75534 --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,107 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread +{ + + Connection conn; + int startPos; + int endPos; + int length = 0; + String savePathDir; + String fileName; + + public DownloadThread (Connection conn, int startPos, int endPos) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.length = endPos - startPos + 1; + } + + public DownloadThread (Connection conn, int startPos, int endPos, + String savePathDir, String fileName) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.length = endPos - startPos + 1; + this.savePathDir = savePathDir; + this.fileName = fileName; + } + + public void run() { + System.out.println("thread " + this.getId() + " running..."); + + try { + byte[] data = null; + data = conn.read(startPos, endPos); + // 检验下载长度是否一致(即是否传输出错) + for (int i = 0; i < 5; i++) { + if (length != data.length) { + System.out.print("thread " + this.getId() + + " not equal ! Loop " + (i + 1)); + System.out.print(", length: " + length); + System.out.println(", downloaded: " + data.length); + data = conn.read(startPos, endPos); + } else { + break; + } + } + + saveFile(savePathDir, fileName, data, startPos); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private synchronized boolean saveFile(String savePathDir, String fileName, + byte[] data, int offset) { + System.out.println("thread " + this.getId() + " saveFile..."); + File saveDir = new File(savePathDir); + if (!saveDir.exists()) { + saveDir.mkdir(); + } + + synchronized (this) { + File file = new File(saveDir + File.separator + fileName); + RandomAccessFile raf = null; + try { + raf = new RandomAccessFile(file, "rw"); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + raf.seek(offset); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + raf.write(data); + return true; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + try { + if (raf != null) { + raf.close(); + } + } catch (IOException e) { + // + } + } + } + + return false; + } +} diff --git a/group24/1525619747/homework_20170328/src/com/coderising/download/FileDownloader.java b/group24/1525619747/homework_20170328/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..fad06f3e57 --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,124 @@ +package com.coderising.download; + +import java.awt.List; +import java.util.ArrayList; + +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; + + String savePathDir; + + String fileName; + + int threadNum = 5; + private ArrayList dtList = new ArrayList(); + + public FileDownloader (String _url) { + this.url = _url; + } + + public FileDownloader (String _url, String savePathDir, String fileName) { + this.url = _url; + this.savePathDir = savePathDir; + this.fileName = fileName; + } + + public FileDownloader (String _url, String savePathDir, String fileName, + int threadNum) { + this.url = _url; + this.savePathDir = savePathDir; + this.fileName = fileName; + this.threadNum = threadNum; + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (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; + try { + + conn = cm.open(this.url); + int length = conn.getContentLength(); + + int sectionLength = length / threadNum; + System.out.println("content length: " + length + + ", sectionLength: " + sectionLength); + + for (int i = 0; i < threadNum; i++) { + if (i == threadNum - 1) { + dtList.add(new DownloadThread(conn, sectionLength * i, + length - 1, this.savePathDir, this.fileName)); + } else { + dtList.add(new DownloadThread(conn, sectionLength * i, + sectionLength * (i + 1) - 1, this.savePathDir, + this.fileName)); + } + } + for (int i = 0; i < dtList.size(); i++) { + dtList.get(i).start(); + } + + // while (!isFinished()) { + // // block wait ? + // } + // if (isFinished()) { + // listener.notifyFinished(); + // } + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + } + + public boolean isFinished() { + for (int i = 0; i < dtList.size(); i++) { + if (!dtList.get(i).getState().equals(Thread.State.TERMINATED)) { + return false; + } + } + return true; + } + + 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/1525619747/homework_20170328/src/com/coderising/download/api/Connection.java b/group24/1525619747/homework_20170328/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..714efcf0df --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/api/Connection.java @@ -0,0 +1,29 @@ +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/group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionException.java b/group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..5627d198b8 --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,6 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception +{ + +} diff --git a/group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionManager.java b/group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..c7e9ab91cb --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,12 @@ +package com.coderising.download.api; + +public interface ConnectionManager +{ + /** + * 给定一个url , 打开一个连接 + * + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group24/1525619747/homework_20170328/src/com/coderising/download/api/DownloadListener.java b/group24/1525619747/homework_20170328/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..5e64145084 --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,6 @@ +package com.coderising.download.api; + +public interface DownloadListener +{ + public void notifyFinished(); +} diff --git a/group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionImpl.java b/group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..65367e80d2 --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,93 @@ +package com.coderising.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.HashMap; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection +{ + + private String urlStr = null; + + public ConnectionImpl () { + } + + public ConnectionImpl (String url) { + this.urlStr = url; + } + + public byte[] read(int startPos, int endPos) throws IOException { + // 设置起始与终止位置 + URL url = null; + try { + url = new URL(this.urlStr); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + conn.setConnectTimeout(3 * 1000); // 3s + conn.setReadTimeout(60 * 1000); + conn.setRequestProperty("User-Agent", + "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream inputStream = conn.getInputStream(); + + byte[] buffer = new byte[1024]; + int len = 0; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + while ((len = inputStream.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + + inputStream.close(); + bos.close(); + conn.disconnect(); + + return bos.toByteArray(); + } + + public int getContentLength() { + URL url = null; + try { + url = new URL(this.urlStr); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + conn.setConnectTimeout(3 * 1000); // 3s + conn.setRequestProperty("User-Agent", + "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + + return conn.getContentLength(); + } + + public void close() { + + } + +} diff --git a/group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..f3e819fe4b --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,19 @@ +package com.coderising.download.impl; + +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 +{ + + public Connection open(String urlStr) throws ConnectionException { + return new ConnectionImpl(urlStr); + } + +} diff --git a/group24/1525619747/homework_20170328/src/com/coderising/helper/Tool.java b/group24/1525619747/homework_20170328/src/com/coderising/helper/Tool.java new file mode 100644 index 0000000000..b4e56334e4 --- /dev/null +++ b/group24/1525619747/homework_20170328/src/com/coderising/helper/Tool.java @@ -0,0 +1,59 @@ +package com.coderising.helper; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Tool +{ + public static String getMd5(String fileName) { + try { + File file = new File(fileName); + FileInputStream fis = new FileInputStream(file); + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] buffer = new byte[1024]; + int length = -1; + while ((length = fis.read(buffer, 0, 1024)) != -1) { + md.update(buffer, 0, length); + } + BigInteger bigInt = new BigInteger(1, md.digest()); + // System.out.println("文件md5值:" + bigInt.toString(16)); + return bigInt.toString(16); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public static String getSHA256(String fileName) { + try { + File file = new File(fileName); + FileInputStream fis = new FileInputStream(file); + MessageDigest md = MessageDigest.getInstance("SHA-256"); + byte[] buffer = new byte[1024]; + int length = -1; + while ((length = fis.read(buffer, 0, 1024)) != -1) { + md.update(buffer, 0, length); + } + BigInteger bigInt = new BigInteger(1, md.digest()); + // System.out.println("文件SHA256值:" + bigInt.toString(16)); + return bigInt.toString(16); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group24/1525619747/homework_20170328/src/testcase/FileDownloaderTest.java b/group24/1525619747/homework_20170328/src/testcase/FileDownloaderTest.java new file mode 100644 index 0000000000..44d392729e --- /dev/null +++ b/group24/1525619747/homework_20170328/src/testcase/FileDownloaderTest.java @@ -0,0 +1,78 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.FileDownloader; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; +import com.coderising.helper.Tool; + +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://windows.php.net/downloads/releases/php-7.0.17-nts-Win32-VC14-x86.zip"; + String uriSHA256 = "d5f559fe2143b408ccb743dbd7af4406ebe4cdd7c2bec5f417a48dd89aa331b0"; + + String savePathDir = "D:/App_data/java/"; + String fileName = "php-7.0.17-nts-Win32-VC14-x86.zip"; + String downloadFileName = "D:/App_data/java/php-7.0.17-nts-Win32-VC14-x86.zip"; + + FileDownloader downloader = new FileDownloader(url, savePathDir, + fileName); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + while (!downloader.isFinished()) { + try { + System.out.println("还没有下载完成,休眠五秒"); + // 休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + // // 等待多线程下载程序执行完毕 + // while (!downloadFinished) { + // try { + // System.out.println("还没有下载完成,休眠五秒"); + // //休眠5秒 + // Thread.sleep(5000); + // } catch (InterruptedException e) { + // e.printStackTrace(); + // } + // } + System.out.println("下载完成!"); + + System.out.println(uriSHA256); + System.out.println(Tool.getSHA256(downloadFileName)); + assertEquals(uriSHA256, Tool.getSHA256(downloadFileName)); + + } + +}