From 067d3a79f84f10bb7ae4c107eb38922ce9dc0bc9 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Sat, 1 Apr 2017 22:45:25 +0800 Subject: [PATCH] FourthHomework --- .../BaseDataStructure/LRUPageFrame.java | 143 ++++++++++++++++++ .../FourthHomework/jvm/ClassFileLoader.java | 51 +++++++ .../src/FourthHomework/jvm/TestJVM.java | 7 + .../BaseDataStructure/TestLRUPageFrame.java | 31 ++++ .../test/FourthHomework/JVM/EmployeeV1.java | 28 ++++ .../JVM/TestClassFileLoader.java | 67 ++++++++ 6 files changed, 327 insertions(+) create mode 100644 group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java create mode 100644 group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java create mode 100644 group11/1310368322/src/FourthHomework/jvm/TestJVM.java create mode 100644 group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java create mode 100644 group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java create mode 100644 group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java diff --git a/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java b/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java new file mode 100644 index 0000000000..5e860a31d7 --- /dev/null +++ b/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java @@ -0,0 +1,143 @@ +package DataStructure_4_LRU; + +import org.junit.runners.Parameterized.Parameters; + +/* + * ��˫������ʵ��LRU�㷨 + */ +public class LRUPageFrame { + private static class Node{ + Node prev; + Node next; + int pageNum = -1;// ����ҳ + + Node(){ + + } + } + + private int capacity; + + private Node first;// ����ͷ + private Node last;// ����β + boolean tag = false; + + public LRUPageFrame(int capacity){ + this.capacity = capacity; + + for(int i = 0; i < capacity; i++){ + Node curNode = new Node(); + if(null == first){ + last = first = curNode; + }else{ + last.next = curNode; + curNode.prev = last; + last = last.next; + } + last.next = null; + } + } + public void printList(){ + Node curNode = first; + while(curNode != null){ + curNode = curNode.next; + } + } + /* + * ��ȡ�����ж��� + * @param key + * @return + */ + public void access(int pageNum){ + printList(); + Node index = findLogicPage(pageNum); + modifyPhysicalPage(index,pageNum); + } + + /* + * @param pageNum ��ʾҪ��ѯ���߼�ҳ�� + * @return ��������ҳ���ҵ�Ҫ��ѯ���߼�ҳ�棬�򷵻ظ�����ҳ�ڵ�����ã����򷵻�null + */ + public Node findLogicPage(int pageNum){ + + Node index = null; + Node curNode = first; + while(curNode != null){ + if(curNode.pageNum == pageNum){ + index = curNode; + tag = true; + } + curNode = curNode.next; + } + return index; + } + /* + * @prama index ������ ���߼�ҳ������ҳ�Ľڵ������ + */ + public void modifyPhysicalPage(Node index,int pageNum){ + push(pageNum,index); + } + /* + * @param pageNum Ҫ push���߼�ҳ�棬 Ĭ��ջ���� first, bottom ջ�� ָ����ջ�Ĵ�С + */ + public void push(int pageNum,Node bottom){ + Node index = checkWhichListNodeNotUsed(); + if(index != null){ + index.pageNum = pageNum; + return; + } + + Node lastNode; + if(null == bottom){ + lastNode = last; + }else{ + lastNode = bottom; + } + Node curNode = lastNode.prev; + while(curNode != null){ + lastNode.pageNum = curNode.pageNum; + lastNode = curNode; + curNode = curNode.prev; + } + lastNode.pageNum = pageNum; + return; + } + + /* + * @return ��������ҳ�� pageNum û�б�ʹ�õĽڵ������(����ջ���������)�����ȫ������ʹ�ã��򷵻� null + */ + public Node checkWhichListNodeNotUsed(){ + Node node = first; + Node index = null; + while(node != null){ + if(node.pageNum == -1){ + index = node; + } + node = node.next; + } + return index; + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + + + + + + + +} diff --git a/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java b/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java new file mode 100644 index 0000000000..6db696e5aa --- /dev/null +++ b/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java @@ -0,0 +1,51 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.runners.Parameterized.Parameters; + +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + int countForClassPath = 0; + int countForReadBinaryCode = 0; + byte [] a = new byte[10000]; + + /* ��ָ��·����ȡ�������ļ����������䱣�浽һ���ֽ������У������� + * @Parameters ָ��·�� + * @�ֽ����� + */ + public byte[] readBinaryCode(String className) throws IOException{ + DataInputStream dis = new DataInputStream( + new BufferedInputStream(new FileInputStream(className))); + for(int i = 0; dis.available() != 0; i++){ + a[i] = dis.readByte(); + countForReadBinaryCode++; + } + byte []target = new byte[countForReadBinaryCode]; + System.arraycopy(a, 0, target, 0, countForReadBinaryCode); + dis.close(); + return target; + } + + public void addClassPath(String path){ + clzPaths.add(path); + countForClassPath++; + } + + public String getClassPath(){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < countForClassPath; i++ ){ + if(i==countForClassPath-1){ + buffer.append(clzPaths.get(i)); + }else{ + buffer.append(clzPaths.get(i)+";"); + } + } + return buffer.toString(); + } +} diff --git a/group11/1310368322/src/FourthHomework/jvm/TestJVM.java b/group11/1310368322/src/FourthHomework/jvm/TestJVM.java new file mode 100644 index 0000000000..735e4d1dc2 --- /dev/null +++ b/group11/1310368322/src/FourthHomework/jvm/TestJVM.java @@ -0,0 +1,7 @@ +package com.coderising.jvm.loader; + +public class TestJVM { + public static void main(String[] args) { + System.out.println("Hello"); + } +} diff --git a/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java b/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java new file mode 100644 index 0000000000..227599c187 --- /dev/null +++ b/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java @@ -0,0 +1,31 @@ +package DataStructure_4_LRU; + +import static org.junit.Assert.*; +import org.junit.*; + +import org.junit.Test; + +public class TestLRUPageFrame { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3);// ����ҳ��洢����Ϊ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/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java b/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java new file mode 100644 index 0000000000..acbc34c9bb --- /dev/null +++ b/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.jvm.loader; + +public class EmployeeV1 { + private String name; + private int age; + + public EmployeeV1(String name, int age){ + this.name = name; + this.age = age; + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public void sayHello(){ + System.out.println("Hello, this is class Employee"); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + } +} diff --git a/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java b/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java new file mode 100644 index 0000000000..263384e71a --- /dev/null +++ b/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java @@ -0,0 +1,67 @@ +package com.coderising.jvm.loader; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.*; + +public class TestClassFileLoader { + static String path1 = "D:/ProgramWorld"; + static String path2 = "D:/ProgramWorld/Java"; + @Test + public void test() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1 + ";" + path2, clzPath); + } + @Test + public void testClassFileLength() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "D:/ProgramWorld/Java/Practice/LangSi/2017������Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // ע�⣺ ����ֽ������ܺ����JVM�汾�й�ϵ������Կ�������õ��ൽ���ж�� + Assert.assertEquals(1058,byteCodes.length); + } + @Test + public void testMagicNumber() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "D:/ProgramWorld/Java/Practice/LangSi/2017������Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{ + byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3] + }; + String actualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe",actualValue); + + } + + private String byteToHexString(byte[] codes){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < codes.length; i++){ + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if(strHex.length() < 2){ + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + + + + + + + + +}