-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Jamesmahaitao/master
FourthHomework
- Loading branch information
Showing
6 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
51 changes: 51 additions & 0 deletions
51
group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> clzPaths = new ArrayList<String>(); | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.coderising.jvm.loader; | ||
|
||
public class TestJVM { | ||
public static void main(String[] args) { | ||
System.out.println("Hello"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |