forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #51 from wa122as/master
补交第四次作业
- Loading branch information
Showing
24 changed files
with
1,194 additions
and
8 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
group15/1521_653895972/src/task4/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,44 @@ | ||
package task4.jvm.loader; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
|
||
public class ClassFileLoader { | ||
|
||
private List<String> clzPaths = new ArrayList<String>(); | ||
|
||
public byte[] readBinaryCode(String className) throws Exception { | ||
String filePath = clzPaths.get(0)+File.separatorChar+className.replace('.',File.separatorChar)+".class"; | ||
File file = new File(filePath); | ||
|
||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
|
||
byte[] buffer = new byte[1024]; | ||
int len = 0; | ||
while((len = bis.read(buffer)) != -1){ | ||
baos.write(buffer,0,len); | ||
} | ||
return baos.toByteArray(); | ||
} | ||
|
||
public void addClassPath(String path) { | ||
if (clzPaths.contains(path)) | ||
return; | ||
clzPaths.add(path); | ||
} | ||
|
||
public String getClassPath(){ | ||
return StringUtils.join(clzPaths,";"); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
group15/1521_653895972/src/task4/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,83 @@ | ||
package task4.jvm.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import task4.jvm.loader.ClassFileLoader; | ||
|
||
|
||
public class ClassFileloaderTest { | ||
|
||
|
||
static String path1 = "D:\\oneces\\GitHub\\coding2017\\group15\\1521_653895972\\out\\production\\1521_653895972"; | ||
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() throws Exception { | ||
|
||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
|
||
String className = "task4.jvm.test.EmployeeV1"; | ||
|
||
byte[] byteCodes = loader.readBinaryCode(className); | ||
|
||
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 | ||
Assert.assertEquals(1038, byteCodes.length); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testMagicNumber() throws Exception{ | ||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
String className = "task4.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(); | ||
} | ||
|
||
} |
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 task4.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(); | ||
|
||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
group15/1521_653895972/src/task4/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,157 @@ | ||
package task4.linklist; | ||
|
||
|
||
public class LRUPageFrame { | ||
|
||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
} | ||
} | ||
|
||
private int capacity; | ||
|
||
private int currentSize; | ||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
this.currentSize = 0; | ||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param pageNum | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
|
||
Node node = find(pageNum); | ||
//在该队列中存在, 则提到队列头 | ||
if (node != null) { | ||
|
||
moveExistingNodeToHead(node); | ||
|
||
} else { | ||
|
||
node = new Node(); | ||
node.pageNum = pageNum; | ||
|
||
// 缓存容器是否已经超过大小. | ||
if (currentSize >= capacity) { | ||
removeLast(); | ||
|
||
} | ||
|
||
addNewNodetoHead(node); | ||
|
||
|
||
} | ||
} | ||
|
||
private void addNewNodetoHead(Node node) { | ||
|
||
if (isEmpty()) { | ||
|
||
node.prev = null; | ||
node.next = null; | ||
first = node; | ||
last = node; | ||
|
||
} else { | ||
node.prev = null; | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
} | ||
this.currentSize++; | ||
} | ||
|
||
private Node find(int data) { | ||
|
||
Node node = first; | ||
while (node != null) { | ||
if (node.pageNum == data) { | ||
return node; | ||
} | ||
node = node.next; | ||
} | ||
return null; | ||
|
||
} | ||
|
||
|
||
/** | ||
* 删除链表尾部节点 表示 删除最少使用的缓存对象 | ||
*/ | ||
private void removeLast() { | ||
Node prev = last.prev; | ||
prev.next = null; | ||
last.prev = null; | ||
last = prev; | ||
this.currentSize--; | ||
} | ||
|
||
/** | ||
* 移动到链表头,表示这个节点是最新使用过的 | ||
* | ||
* @param node | ||
*/ | ||
private void moveExistingNodeToHead(Node node) { | ||
|
||
if (node == first) { | ||
|
||
return; | ||
} else if (node == last) { | ||
//当前节点是链表尾, 需要放到链表头 | ||
Node prevNode = node.prev; | ||
prevNode.next = null; | ||
last.prev = null; | ||
last = prevNode; | ||
|
||
} else { | ||
//node 在链表的中间, 把node 的前后节点连接起来 | ||
Node prevNode = node.prev; | ||
prevNode.next = node.next; | ||
|
||
Node nextNode = node.next; | ||
nextNode.prev = prevNode; | ||
|
||
|
||
} | ||
|
||
node.prev = null; | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
|
||
} | ||
|
||
private boolean isEmpty() { | ||
return (first == null) && (last == null); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
group15/1521_653895972/src/task4/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,34 @@ | ||
package task4.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(5); | ||
Assert.assertEquals("5,4,0", frame.toString()); | ||
System.out.println("3232"); | ||
} | ||
|
||
} |
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,25 @@ | ||
package task5.jvm.clz; | ||
|
||
public class AccessFlag { | ||
private int flagValue; | ||
|
||
public AccessFlag(int value) { | ||
this.flagValue = value; | ||
} | ||
|
||
public int getFlagValue() { | ||
return flagValue; | ||
} | ||
|
||
public void setFlagValue(int flag) { | ||
this.flagValue = flag; | ||
} | ||
|
||
public boolean isPublicClass(){ | ||
return (this.flagValue & 0x0001) != 0; | ||
} | ||
public boolean isFinalClass(){ | ||
return (this.flagValue & 0x0010) != 0; | ||
} | ||
|
||
} |
Oops, something went wrong.