forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
356 additions
and
5 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
group02/727171008/src/com/github/HarryHook/coding2017/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,74 @@ | ||
package com.github.HarryHook.coding2017.jvm.loader; | ||
|
||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
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>(); | ||
|
||
public byte[] readBinaryCode(String className) { | ||
|
||
|
||
String fileName = clzPaths.get(0) + File.separatorChar + | ||
className.replace('.', File.separatorChar) + ".class"; | ||
|
||
InputStream in = null; | ||
try { | ||
in = new FileInputStream(fileName); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(1024); | ||
|
||
byte[] buffer = new byte[1024]; | ||
int length = 0; | ||
while((length = in.read(buffer)) != -1) { | ||
out.write(buffer, 0, length); | ||
} | ||
return out.toByteArray(); | ||
} catch(IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if(in != null) { | ||
try{ | ||
in.close(); | ||
}catch(IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return null; | ||
|
||
} | ||
|
||
|
||
public void addClassPath(String path) { | ||
|
||
this.clzPaths.add(path); | ||
} | ||
|
||
|
||
|
||
public String getClassPath(){ | ||
|
||
StringBuilder buffer = new StringBuilder(); | ||
|
||
for(int i=0; i<clzPaths.size(); i++) { | ||
|
||
buffer.append(clzPaths.get(i)); | ||
if(i < clzPaths.size() - 1) { | ||
buffer.append(";"); | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
group02/727171008/src/com/github/HarryHook/coding2017/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,88 @@ | ||
package com.github.HarryHook.coding2017.jvm.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.github.HarryHook.coding2017.jvm.loader.ClassFileLoader; | ||
|
||
|
||
public class ClassFileloaderTest { | ||
|
||
static String path1 = "F:\\Coding2017\\group02\\727171008\\bin"; | ||
//F:\Coding2017\group02\727171008\bin\com\github\HarryHook\coding2017\jvm\test | ||
static String path2 = "F:\\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.github.HarryHook.coding2017.jvm.test.EmployeeV1"; | ||
|
||
byte[] byteCodes = loader.readBinaryCode(className); | ||
// jdk_1.8.0_111 | ||
Assert.assertEquals(1090, byteCodes.length); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testMagicNumber() { | ||
|
||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
String className = "com.github.HarryHook.coding2017.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
group02/727171008/src/com/github/HarryHook/coding2017/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.github.HarryHook.coding2017.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(); | ||
|
||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
group02/727171008/src/com/github/HarryHook/coding2017/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,124 @@ | ||
package com.github.HarryHook.coding2017.linklist; | ||
|
||
/** | ||
* 用双向链表实现LRU算法 | ||
* @author HarryHook | ||
* | ||
*/ | ||
public class LRUPageFrame { | ||
|
||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
|
||
} | ||
} | ||
|
||
private int capacity; | ||
private int currentSize = 0; | ||
|
||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param pageNum | ||
* @return | ||
*/ | ||
|
||
|
||
public void access(int pageNum) { | ||
|
||
if(currentSize == 0) { | ||
Node node = new Node(); | ||
node.pageNum = pageNum; | ||
node.prev = node.next = null; | ||
first = last = node; | ||
currentSize++; | ||
}else { | ||
|
||
if(first.pageNum == pageNum) { | ||
return ; | ||
} | ||
|
||
if(currentSize == 1) { | ||
addToFirst(pageNum); | ||
}else { | ||
|
||
if(last.pageNum == pageNum) { | ||
moveLastToFirst(); | ||
return ; | ||
} | ||
Node iteratorNode = first; | ||
while(iteratorNode.next != null && iteratorNode.pageNum != pageNum) { | ||
iteratorNode = iteratorNode.next; | ||
} | ||
if(iteratorNode == last) { | ||
addToFirst(pageNum); | ||
//若缓存已满,移除最后一个节点 | ||
if(currentSize > capacity) { | ||
last = last.prev; | ||
last.next = null; | ||
} | ||
}else { | ||
moveToFirst(iteratorNode); | ||
} | ||
} | ||
} | ||
} | ||
// 将节点/缓存页添加到fitrst | ||
public void addToFirst(int pageNum) { | ||
Node node = new Node(); | ||
node.pageNum = pageNum; | ||
node.prev = null; | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
currentSize++; | ||
} | ||
//将last节点移动到first | ||
public void moveLastToFirst() { | ||
last.next = first; | ||
first.prev = last; | ||
first = last; | ||
last = last.prev; | ||
last.next = null; | ||
first.prev = null; | ||
} | ||
//将最近使用的已有缓存页移动到first | ||
public void moveToFirst(Node iteratorNode) { | ||
iteratorNode.prev.next = iteratorNode.next; | ||
iteratorNode.next.prev = iteratorNode.prev; | ||
iteratorNode.prev = null; | ||
iteratorNode.next = first; | ||
first.prev = iteratorNode; | ||
first = iteratorNode; | ||
} | ||
|
||
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
group02/727171008/src/com/github/HarryHook/coding2017/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.github.HarryHook.coding2017.linklist; | ||
|
||
import org.junit.Assert; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
public class LRUPageFrameTest { | ||
|
||
@Test | ||
public void testAccess() { | ||
LRUPageFrame frame = new LRUPageFrame(3); | ||
frame.access(7); | ||
Assert.assertEquals("7", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,7", frame.toString()); | ||
frame.access(7); | ||
Assert.assertEquals("7,0", frame.toString()); | ||
frame.access(1); | ||
Assert.assertEquals("1,7,0", frame.toString()); | ||
frame.access(2); | ||
Assert.assertEquals("2,1,7", 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()); | ||
} | ||
|
||
} |
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