diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/Iterator.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/Iterator.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java similarity index 54% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java index 6040cb5a47..55d8839b02 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java @@ -40,6 +40,7 @@ public void add(int index , Object o){ newNode.setPre(pre); newNode.setNext(indexNode); indexNode.setPre(newNode); + size++; } private void checkIndex(int index){ if(index >= size || index <0){ @@ -56,8 +57,10 @@ private Node getNode(int index){ if(index==size-1){ return tail; } + Node current = head; for (int i = 0; i < index; i++) { - result = head.getNext(); + result = current.getNext(); + current = result; } return result; } @@ -79,6 +82,7 @@ public Object remove(int index){ }else{ tail = pre; } + size--; return indexNode.getData(); } @@ -88,28 +92,54 @@ public int size(){ public void addFirst(Object o){ Node newNode = new Node(o); + if (size==0){ + add(o); + return; + } head.setPre(newNode); newNode.setNext(head); head = newNode; + size++; } public void addLast(Object o){ + if (size == 0) { + add(o); + return; + } Node newNode = new Node(o); tail.setNext(newNode); newNode.setPre(tail); tail = newNode; + size++; } public Object removeFirst(){ + if(size<1){ + throw new IllegalArgumentException(); + } + if(size==1){ + tail=null; + } Node next = head.getNext(); Node oldHead = head; - head = next; head.setPre(null); + head = next; + oldHead.setNext(null); + size--; return oldHead; } public Object removeLast(){ + if(size<1){ + throw new IllegalArgumentException(); + } + if(size==1){ + head=null; + } Node oldTail = tail; Node pre = tail.getPre(); tail = pre; tail.setNext(null); + oldTail.setPre(null); + size--; return oldTail; } public Iterator iterator(){ @@ -161,6 +191,28 @@ public void setPre(Node pre) { * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ + Node current = head; + Node next = current.getNext(); + Node pre = current.getPre(); + while(next!=null){ + Node nNext = next.getNext(); + if(pre!=null){ + pre.setPre(current); + } + if(current!=null){ + current.setPre(next); + current.setNext(pre); + } + if(next!=null){ + next.setNext(current); + } + pre = current; + current = next; + next = nNext; + } + Node oldHead = head; + head = tail; + tail = oldHead; } /** @@ -170,16 +222,54 @@ public void reverse(){ */ public void removeFirstHalf(){ - + int removeSize = size/2; + for (int i = 0; i < removeSize; i++) { + removeFirst(); + } } /** * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length + * @param start 开始位置 + * @param length 长度 */ - public void remove(int i, int length){ + public void remove(int start, int length){ + checkIndex(start); + if(length<1){ + return; + } + if(start==0 && length>=size){ + int removeSum = size; + for (int j = 0; j < removeSum; j++) { + removeFirst(); + } + size = size-length; + return; + } + int customMaxIndex = start+length; + int endIndex = customMaxIndex < size ? customMaxIndex : size; + if(start==0){ + for (int j = 0; j < length; j++) { + removeFirst(); + } + size = size-length; + return; + } + if(endIndex==size){ + int removeSum = size-start; + for (int j = 0; j < removeSum; j++) { + removeLast(); + } + return; + } + Node startNode = getNode(start-1); + Node endNode = getNode(endIndex); + startNode.getNext().setPre(null); + startNode.setNext(endNode); + endNode.getPre().setNext(null); + endNode.setPre(startNode); + size = size-length; } /** * 假定当前链表和list均包含已升序排列的整数 @@ -189,8 +279,24 @@ public void remove(int i, int length){ * 返回的结果应该是[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ - return null; + public int[] getElements(LinkedList list){ + for (int i = 0; i < list.size; i++) { + if(Integer.parseInt(list.get(i).toString())>=this.size){ + throw new IndexOutOfBoundsException(); + } + } + int[] result = new int[list.size]; + int index = 0; + for (int i = 0; i < this.size; i++) { + if (index==list.size()){ + break; + } + if(Integer.parseInt(list.get(index).toString())==i){ + result[index] = Integer.parseInt(this.get(i).toString()); + index++; + } + } + return result; } /** @@ -201,7 +307,22 @@ public static int[] getElements(LinkedList list){ */ public void subtract(LinkedList list){ - + int index = 0; + Object compare = list.get(index); + Node current = head; + while(current!=null && compare!=null){ + Node preCurrent = current; + current = current.getNext(); + // TODO 这里不能删除重复元素只能删除一次 + if(preCurrent.getData().equals(compare)){ + preCurrent.getPre().setNext(preCurrent.getNext()); + preCurrent.getNext().setPre(preCurrent.getPre()); + preCurrent.setPre(null); + preCurrent.setNext(null); + size--; + compare = ++index < list.size ? list.get(index) : null; + } + } } /** @@ -209,7 +330,24 @@ public void subtract(LinkedList list){ * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + Node current = head; + Node next = current.getNext(); + while (next!=null){ + if(next.getData().equals(current.getData())){ + Node preNext = next; + next = preNext.getNext(); + current.setNext(preNext.getNext()); + if (next != null) { + next.setPre(current); + } + preNext.setPre(null); + preNext.setNext(null); + size--; + }else{ + current = next; + next = next.getNext(); + } + } } /** @@ -219,7 +357,23 @@ public void removeDuplicateValues(){ * @param max */ public void removeRange(int min, int max){ - + int minIndex = -1; + int maxIndex = -1; + int index = 0; + Node current = head; + while (current!=null){ + if(current.getData().equals(min)){ + minIndex = index; + } + if(current.getData().equals(max)){ + maxIndex = index; + } + index++; + current = current.getNext(); + } + if (minIndex > -1 && maxIndex > -1 && min <= max && minIndex + 1 < size) { + remove(minIndex + 1, maxIndex - minIndex - 1); + } } /** @@ -230,4 +384,17 @@ public void removeRange(int min, int max){ public LinkedList intersection( LinkedList list){ return null; } + @Override + public String toString(){ + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("["); + for (int i = 0; i < size; i++) { + stringBuffer.append(get(i)); + if(i!=size-1){ + stringBuffer.append(","); + } + } + stringBuffer.append("]"); + return stringBuffer.toString(); + } } diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/List.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/List.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/Queue.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/Queue.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/Stack.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/Stack.java diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/lru/LRUPageFrame.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/lru/LRUPageFrame.java new file mode 100644 index 0000000000..7e3c07737d --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/lru/LRUPageFrame.java @@ -0,0 +1,49 @@ +package com.github.wdn.coding2017.basic.lru; + +import com.github.wdn.coding2017.basic.LinkedList; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + private int capacity; + private LinkedList cache = new LinkedList(); + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if(capacity==cache.size()){ + int exist = exist(pageNum); + if(exist>-1){ + cache.addFirst(cache.remove(exist)); + }else{ + cache.removeLast(); + cache.addFirst(pageNum); + } + }else { + cache.addFirst(pageNum); + } + + } + private int exist(int pageNum){ + for (int i = 0; i < cache.size(); i++) { + if(cache.get(i).equals(pageNum)){ + return i; + } + } + return -1; + } + public String toString(){ + return cache.toString().replace("[","").replace("]",""); + } + +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/lru/LRUPageFrameTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..62edda7ec8 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/lru/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.github.wdn.coding2017.basic.lru; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..7238acf44d --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java @@ -0,0 +1,54 @@ +package com.github.wdn.coding2017.coderising.download; + + +import com.github.wdn.coding2017.coderising.download.api.Connection; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + private static final Object lock = new Object(); + private static final File file = new File("E:\\down.jpg"); + private CountDownLatch latch; + public DownloadThread(Connection conn, int startPos, int endPos,CountDownLatch latch){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.latch = latch; + } + public boolean writeFile(){ + if (file.length()==startPos) { + synchronized (lock){ + byte[] image = new byte[0]; + try { + image = conn.read(startPos, endPos); + FileOutputStream fos = new FileOutputStream(file,true); + fos.write(image); + fos.close(); + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + }else{ + return false; + } + } + public void run(){ + while (!writeFile()){ + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + latch.countDown(); + } +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java similarity index 62% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java index c3c8a3f27d..ca3709a549 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java @@ -1,10 +1,14 @@ -package com.coderising.download; +package com.github.wdn.coding2017.coderising.download; -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; +import com.github.wdn.coding2017.coderising.download.api.Connection; +import com.github.wdn.coding2017.coderising.download.api.ConnectionException; +import com.github.wdn.coding2017.coderising.download.api.ConnectionManager; +import com.github.wdn.coding2017.coderising.download.api.DownloadListener; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.concurrent.CountDownLatch; public class FileDownloader { @@ -35,25 +39,34 @@ public void execute(){ // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 Connection conn = null; + try { - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - - } catch (ConnectionException e) { + int threadSum = 4; + final CountDownLatch latch = new CountDownLatch(threadSum); + int length = conn.getContentLength(); + if(length<=1024){ + new DownloadThread(cm.open(this.url),0,length,latch).start(); + }else{ + int partLength = length/threadSum; + for (int i = 0; i < threadSum; i++) { + if(i==threadSum-1){ + new DownloadThread(cm.open(this.url),i*partLength,length,latch).start(); + }else{ + new DownloadThread(cm.open(this.url),i*partLength,i*partLength+partLength,latch).start(); + } + + } + } + latch.await(); + listener.notifyFinished(); + } catch (Exception e) { e.printStackTrace(); }finally{ if(conn != null){ conn.close(); } } - - - - } public void setListener(DownloadListener listener) { diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java similarity index 72% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java index 4ff7f46ae0..8ca48ad471 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java @@ -1,12 +1,12 @@ -package com.coderising.download; +package com.github.wdn.coding2017.coderising.download; +import com.github.wdn.coding2017.coderising.download.api.ConnectionManager; +import com.github.wdn.coding2017.coderising.download.api.DownloadListener; +import com.github.wdn.coding2017.coderising.download.impl.ConnectionManagerImpl; import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; public class FileDownloaderTest { boolean downloadFinished = false; @@ -21,7 +21,7 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "http://localhost:8080/test.jpg"; + String url = "http://pic1.win4000.com/wallpaper/9/58dcbb7ee7de0.jpg"; FileDownloader downloader = new FileDownloader(url); diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java similarity index 73% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java index 0957eaf7f4..8b0e057a32 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java @@ -1,6 +1,7 @@ -package com.coderising.download.api; +package com.github.wdn.coding2017.coderising.download.api; import java.io.IOException; +import java.net.HttpURLConnection; public interface Connection { /** @@ -20,4 +21,7 @@ public interface Connection { * 关闭连接 */ public void close(); + + + void setHttpURLConnection(HttpURLConnection httpURLConnection); } diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..5c90080401 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.github.wdn.coding2017.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java similarity index 75% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java index ce045393b1..f814016839 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.github.wdn.coding2017.coderising.download.api; public interface ConnectionManager { /** diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java similarity index 54% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java index bf9807b307..078b14365d 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.github.wdn.coding2017.coderising.download.api; public interface DownloadListener { public void notifyFinished(); diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..7f9e090a1e --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,55 @@ +package com.github.wdn.coding2017.coderising.download.impl; + +import com.github.wdn.coding2017.coderising.download.api.Connection; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.Arrays; + + +public class ConnectionImpl implements Connection { + HttpURLConnection connection; + @Override + public byte[] read(int startPos, int endPos) throws IOException { + if (connection == null) { + throw new IllegalArgumentException("connection is null"); + } + int code = connection.getResponseCode(); + ByteArrayOutputStream baos=null; + InputStream is=null; + if(code==200){ + //5读取服务器资源的流 + is= connection.getInputStream(); + //准备内存输出流 临时存储的 + baos = new ByteArrayOutputStream(); + byte buff[] = new byte[1024]; + int len=0; + int sum = 0; + while((len=is.read(buff))!=-1){ + baos.write(buff,0,len); + baos.flush(); + sum+=len; + if(sum>=endPos){ + break; + } + } + } + byte[] readResult = baos.toByteArray(); + return Arrays.copyOfRange(readResult,startPos,endPos); + } + + @Override + public int getContentLength() { + return connection.getContentLength(); + } + + @Override + public void close() { + + } + public void setHttpURLConnection(HttpURLConnection httpURLConnection){ + this.connection = httpURLConnection; + } +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..bcdad865aa --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,26 @@ +package com.github.wdn.coding2017.coderising.download.impl; + + +import com.github.wdn.coding2017.coderising.download.api.Connection; +import com.github.wdn.coding2017.coderising.download.api.ConnectionException; +import com.github.wdn.coding2017.coderising.download.api.ConnectionManager; + +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + try { + URL targetUrl = new URL(url); + Connection connection = new ConnectionImpl(); + connection.setHttpURLConnection((HttpURLConnection) targetUrl.openConnection()); + return connection; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java similarity index 100% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java rename to group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java diff --git a/group24/626451284/data-structure/src/main/test/com/github/wdn/coding2017/basic/LinkedListTest.java b/group24/626451284/data-structure/src/main/test/com/github/wdn/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..7a967cea64 --- /dev/null +++ b/group24/626451284/data-structure/src/main/test/com/github/wdn/coding2017/basic/LinkedListTest.java @@ -0,0 +1,91 @@ +package com.github.wdn.coding2017.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Administrator on 2017/4/3 0003. + */ +public class LinkedListTest { + LinkedList linkedList = new LinkedList(); + @Before + public void initLinkedList(){ + for (int i = 0; i < 12; i++) { + linkedList.add(i); + } + } + @Test + public void testReverse(){ + + System.out.println(linkedList.size()); + System.out.println(linkedList); + linkedList.reverse(); + System.out.println(""); + System.out.println(linkedList); + } + @Test + public void testRemoveFirstHalf(){ + System.out.println(linkedList); + linkedList.removeFirstHalf(); + System.out.println(linkedList); + } + @Test + public void testRemove(){ + System.out.println(linkedList); + //linkedList.remove(0,30); + //System.out.println(linkedList); + //linkedList.remove(2,30); + //System.out.println(linkedList); + linkedList.remove(2,0); + System.out.println(linkedList); + } + @Test + public void testGetElements(){ + LinkedList indexs = new LinkedList(); + indexs.add(3); + indexs.add(5); + indexs.add(7); + indexs.add(9); + int[] result = linkedList.getElements(indexs); + System.out.println(Arrays.toString(result)); + Assert.assertArrayEquals(new int[]{3, 5, 7, 9},result); + } + @Test + public void testSubtract(){ + LinkedList indexs = new LinkedList(); + indexs.add(3); + indexs.add(5); + indexs.add(7); + indexs.add(9); + linkedList.subtract(indexs); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + @Test + public void testRemoveDuplicateValues(){ + LinkedList list = new LinkedList(); + list.add(3); + //list.add(3); + list.add(5); + //list.add(5); + list.add(7); + list.add(7); + list.add(9); + list.add(9); + list.removeDuplicateValues(); + System.out.println(list); + } + @Test + public void testRemoveRange(){ + LinkedList indexs = new LinkedList(); + indexs.add(3); + indexs.add(5); + indexs.add(7); + indexs.add(9); + indexs.removeRange(9, 9); + System.out.println(indexs); + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..20c8183f3a --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java @@ -0,0 +1,80 @@ +package com.github.wdn.coding2017.jvm.loader; + +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + File file = null; + for (int i = 0; i < clzPaths.size(); i++) { + String path = clzPaths.get(i); + String fullPath = path +File.separator+ className.replace(".", File.separator)+".class"; + file = new File(fullPath); + } + + try { + if(file.exists()){ + // 使用FileUtils最简单 + // return FileUtils.readFileToByteArray(file); + FileInputStream inputStream = new FileInputStream(file); + long fileLength = file.length(); + if (fileLength>Integer.MAX_VALUE) { + throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + fileLength); + } + byte[] fileBytes = new byte[(int)fileLength]; + byte[] bytes = new byte[1024]; + int len; + int offset=0; + // for循环使用inputStream api读取 一次读完。。 + for(offset = 0; offset < fileLength && (len = inputStream.read(fileBytes, offset, (int)fileLength - offset)) != -1; offset += len) { + System.out.println("dd"); + } + // while循环使用System.arraycopy读取 + /*while ((len = inputStream.read(bytes))>-1){ + System.arraycopy(bytes, 0, fileBytes, offset, len); + offset += len; + }*/ + return fileBytes; + } + throw new FileNotFoundException(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < clzPaths.size(); i++) { + if (i==clzPaths.size()-1) { + stringBuffer.append(clzPaths.get(i)); + }else{ + stringBuffer.append(clzPaths.get(i)).append(";"); + } + } + return stringBuffer.toString(); + } + + + + + +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..346159abb5 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,91 @@ +package com.github.wdn.coding2017.jvm.test; + +import com.github.wdn.coding2017.jvm.loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + + + + +public class ClassFileloaderTest { + + + static String path1 = "E:\\softdata\\ideaworkspace\\self\\coding2017\\group24\\626451284\\mini-jvm\\target\\classes"; + static String path2 = "E:\\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.github.wdn.coding2017.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1078, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.github.wdn.coding2017.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i - - 4.0.0 - - com.github.wdn - coding2017 - 1.0-SNAPSHOT - \ No newline at end of file diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java deleted file mode 100644 index 900a3ad358..0000000000 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - public void run(){ - - } -} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java deleted file mode 100644 index 1551a80b3d..0000000000 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public class ConnectionException extends Exception { - -} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java deleted file mode 100644 index 36a9d2ce15..0000000000 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index 172371dd55..0000000000 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml deleted file mode 100644 index fb12bdc239..0000000000 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - -