From 49d2ca4ee7cd1273d66a97461d82e836559dae01 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 3 Apr 2017 12:50:10 +0800 Subject: [PATCH] add jvm_1 of week 6 --- .../jvm/loader/ClassFileLoader.java | 74 +++++++++++ .../jvm/test/ClassFileloaderTest.java | 88 +++++++++++++ .../coding2017/jvm/test/EmployeeV1.java | 28 ++++ .../coding2017/linklist/LRUPageFrame.java | 124 ++++++++++++++++++ .../coding2017/linklist/LRUPageFrameTest.java | 35 +++++ .../com/coding/basic/linklist/LinkedList.java | 12 +- 6 files changed, 356 insertions(+), 5 deletions(-) create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/jvm/test/ClassFileloaderTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/jvm/test/EmployeeV1.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrame.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java b/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..5c52c492a3 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java @@ -0,0 +1,74 @@ +package com.github.HarryHook.coding2017.jvm.loader; + + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + + String fileName = clzPaths.get(0) + File.separatorChar + + className.replace('.', File.separatorChar) + ".class"; + + InputStream in = null; + try { + in = new FileInputStream(fileName); + ByteArrayOutputStream out = new ByteArrayOutputStream(1024); + + byte[] buffer = new byte[1024]; + int length = 0; + while((length = in.read(buffer)) != -1) { + out.write(buffer, 0, length); + } + return out.toByteArray(); + } catch(IOException e) { + e.printStackTrace(); + } finally { + if(in != null) { + try{ + in.close(); + }catch(IOException e) { + e.printStackTrace(); + } + } + } + return null; + + } + + + public void addClassPath(String path) { + + this.clzPaths.add(path); + } + + + + public String getClassPath(){ + + StringBuilder buffer = new StringBuilder(); + + for(int i=0; i capacity) { + last = last.prev; + last.next = null; + } + }else { + moveToFirst(iteratorNode); + } + } + } + } + // 将节点/缓存页添加到fitrst + public void addToFirst(int pageNum) { + Node node = new Node(); + node.pageNum = pageNum; + node.prev = null; + node.next = first; + first.prev = node; + first = node; + currentSize++; + } + //将last节点移动到first + public void moveLastToFirst() { + last.next = first; + first.prev = last; + first = last; + last = last.prev; + last.next = null; + first.prev = null; + } + //将最近使用的已有缓存页移动到first + public void moveToFirst(Node iteratorNode) { + iteratorNode.prev.next = iteratorNode.next; + iteratorNode.next.prev = iteratorNode.prev; + iteratorNode.prev = null; + iteratorNode.next = first; + first.prev = iteratorNode; + first = iteratorNode; + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null) { + buffer.append(node.pageNum); + node = node.next; + if(node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..004b99eac4 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java @@ -0,0 +1,35 @@ +package com.github.HarryHook.coding2017.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + Assert.assertEquals("7", frame.toString()); + frame.access(0); + Assert.assertEquals("0,7", frame.toString()); + frame.access(7); + Assert.assertEquals("7,0", frame.toString()); + frame.access(1); + Assert.assertEquals("1,7,0", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,7", 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/liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java b/liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java index f4c7556a2e..9a408dc46d 100644 --- a/liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java +++ b/liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java @@ -6,6 +6,12 @@ public class LinkedList implements List { private Node head; + + private static class Node{ + Object data; + Node next; + + } public void add(Object o){ @@ -41,11 +47,7 @@ public Iterator iterator(){ } - private static class Node{ - Object data; - Node next; - - } + /** * 把该链表逆置