diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java index 6342314df6..5b13d3340a 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java @@ -1,19 +1,21 @@ -package com.github.ipk2015.coding2017.basic; - -public class Queue { - private LinkedList elementDatas=new LinkedList(); - public void enQueue(Object o){ - elementDatas.add(o); - } - public Object deQueue(){ - return elementDatas.removeFirst(); - } - - public boolean isEmpty(){ - return size()==0; - } - - public int size(){ - return elementDatas.size(); - } -} +package com.github.ipk2015.coding2017.basic; + +import com.github.ipk2015.coding2017.basic.linkedlist.LinkedList; + +public class Queue { + private LinkedList elementDatas=new LinkedList(); + public void enQueue(Object o){ + elementDatas.add(o); + } + public Object deQueue(){ + return elementDatas.removeFirst(); + } + + public boolean isEmpty(){ + return size()==0; + } + + public int size(){ + return elementDatas.size(); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LRUPageFrame.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LRUPageFrame.java new file mode 100644 index 0000000000..81a3cb3554 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/linkedlist/LRUPageFrame.java @@ -0,0 +1,119 @@ +package com.github.ipk2015.coding2017.basic.linkedlist; + + + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + private int size=0; + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + if(this.capacity<=1){ + throw new RuntimeException("MeaningLess"); + } + int pos=searchPageNum(pageNum); + if(pos==-1){ + addFirst(pageNum); + return; + } + if(pos==0){ + return; + } + Node tempNode=first; + for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws IOException { + className=getCompleteClassName(className); + File file=null; + for(String path:clzPaths){ + file=new File(path+"\\"+className); + if(file.exists()){ + break; + } + } + if(null==file){ + throw new FileNotFoundException(className); + } + ByteArrayOutputStream bos=new ByteArrayOutputStream((int)file.length()); + BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); + int size=1024; + byte[] buffer=new byte[size]; + int length=0; + while((length=in.read(buffer, 0, size))!=-1){ + bos.write(buffer,0,length); + } + return bos.toByteArray(); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuffer buffer=new StringBuffer(); + for(String path:clzPaths){ + buffer.append(path+";"); + } + buffer.deleteCharAt(buffer.length()-1); + return buffer.toString(); + } + + private String getCompleteClassName(String name){ + if(!name.endsWith(".class")){ + name=name+".class"; + } + int pointPos=name.lastIndexOf(".", name.length()-7); + if(pointPos>-1){ + name=name.substring(pointPos+1); + } + return name; + } + + + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..61440e39c2 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java @@ -0,0 +1,97 @@ +package com.github.ipk2015.coding2017.minijvm.test; + + + +import java.io.IOException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.minijvm.loader.ClassFileLoader; + + + + + + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + static String path3 = "E:\\javaImprove\\git\\group24\\121111914\\src\\com\\github\\ipk2015\\coding2017\\minijvm\\bin"; + + + + @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() throws IOException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path3); + + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + System.out.println( byteCodes.length+""); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(835, byteCodes.length); + + } + + + @Test + public void testMagicNumber() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path3); + 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