diff --git a/group16/313001956/src/com/coderising/jvm/loader/ClassFileLoader.java b/group16/313001956/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..2b7607a09e --- /dev/null +++ b/group16/313001956/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,56 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +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(); + static final int BUFF_SIZE=1024; + + public byte[] readBinaryCode(String className) { + byte[] barray = new byte[BUFF_SIZE]; + try { + + String pathname = clzPaths.get(0) + "\\" + className.replace('.', '\\')+".class"; + File file = new File(pathname); + InputStream in = new FileInputStream(file); + int byteread = 0; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while ((byteread = in.read(barray)) != -1) { + baos.write(barray, 0, byteread); + } + return baos.toByteArray(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + int clzsize = clzPaths.size(); + String str = ""; + if (clzsize > 0) { + for (int i = 0; i < clzsize; i++) { + str += clzPaths.get(i); + if (i < clzsize - 1) { + str += ";"; + } + } + } + return str; + } + +} diff --git a/group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..6edfd64a18 --- /dev/null +++ b/group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,91 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + //static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path1 = "D:\\Java2017\\GitHub\\coding2017\\group16\\313001956\\build\\classes"; + static String path2 = "C:\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.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // ע�⣺����ֽ������ܺ����JVM�汾�й�ϵ�� ����Կ�������õ��ൽ���ж�� + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.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= this.capacity) { + + } + } + + 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/group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..d1e58e2405 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,35 @@ +package com.coding.basic.linklist; + +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()); + frame.access(0); + Assert.assertEquals("0,4,3", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,4", frame.toString()); + } + +}