-
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 #42 from wanghongdi56/master
0326作业
- Loading branch information
Showing
6 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
group16/313001956/src/com/coderising/jvm/loader/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,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<String> clzPaths = new ArrayList<String>(); | ||
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; | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.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,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<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(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
group16/313001956/src/com/coderising/jvm/test/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.test; | ||
|
||
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(); | ||
|
||
} | ||
} |
Binary file not shown.
135 changes: 135 additions & 0 deletions
135
group16/313001956/src/com/coding/basic/linklist/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,135 @@ | ||
package com.coding.basic.linklist; | ||
|
||
/** | ||
* 用双向链表实现LRU算法 | ||
* | ||
* @author liuxin | ||
* | ||
*/ | ||
public class LRUPageFrame { | ||
|
||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
} | ||
} | ||
|
||
private int capacity; | ||
|
||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
first = null; | ||
last = null; | ||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param key | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
Node node = new Node(); | ||
node.prev = null; | ||
node.pageNum = pageNum; | ||
|
||
if (first == null) { | ||
node.next = null; | ||
first = node; | ||
return; | ||
} | ||
if(judgeEqual(node, pageNum)){ | ||
return; | ||
} | ||
|
||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
|
||
if (last == null) { | ||
judgeFull(); | ||
} else { | ||
Node temp = last.prev; | ||
|
||
last.prev = null; | ||
last.next = null; | ||
last.pageNum = 0; | ||
|
||
temp.next = null; | ||
last = temp; | ||
} | ||
} | ||
|
||
private boolean judgeEqual(Node node, int pageNum) { | ||
if (first.pageNum == pageNum) { | ||
return true; | ||
} | ||
Node nd = first; | ||
while (nd != null) { | ||
if (nd.pageNum == pageNum) { | ||
if (nd.next != null) { | ||
nd.prev.next = nd.next; | ||
nd.next.prev = nd.prev; | ||
nd.prev = null; | ||
nd.next = first; | ||
first = nd; | ||
} else { | ||
if (last != null) { | ||
last = nd.prev; | ||
} | ||
nd.prev.next = null; | ||
|
||
nd.prev = null; | ||
nd.next = first; | ||
first.prev=nd; | ||
first = nd; | ||
} | ||
|
||
return true; | ||
} | ||
nd = nd.next; | ||
} | ||
return false; | ||
} | ||
|
||
// 判断链表是否填充满了并设置last | ||
private void judgeFull() { | ||
int count = 0; | ||
Node node = first; | ||
while (node != null) { | ||
count++; | ||
if (count == this.capacity) { | ||
last = node; | ||
return; | ||
} | ||
node = node.next; | ||
} | ||
|
||
if (count >= 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(); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.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,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()); | ||
} | ||
|
||
} |