Skip to content

Commit

Permalink
第四周作业
Browse files Browse the repository at this point in the history
  • Loading branch information
PigTS committed Apr 1, 2017
1 parent 171df2a commit 1cf57b6
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

package week04.jvm.loader;

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 {
URL base = this.getClass().getResource("/");
String baseToString = ""+base;
String filePath = baseToString.replaceAll("file:/", "")+className.replace(".", "\\")+".class";
//String filePath = clzPaths.get(0)+"\\"+className.replace(".", "\\")+".class"; //符合Junit测试调用addClassPath方法
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) {
clzPaths.add(path);
}

public String getClassPath(){
StringBuffer strBuffer = new StringBuffer();
for(int i=0;i<clzPaths.size();i++){
if(i == (clzPaths.size() - 1)){
strBuffer.append(clzPaths.get(i));
}else{
strBuffer.append(clzPaths.get(i)+";");
}
}
return strBuffer.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package week04.jvm.test;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import week04.jvm.loader.ClassFileLoader;


public class ClassFileloaderTest {


static String path1 = "D:\\GitHub\\coding2017\\group01\\1814014897\\zhouhui\\bin";
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 = "week04.jvm.test.EmployeeV1";

byte[] byteCodes = loader.readBinaryCode(className);

// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
Assert.assertEquals(1040, byteCodes.length);

}


@Test
public void testMagicNumber() throws Exception{
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "week04.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 group01/1814014897/zhouhui/src/week04/jvm/test/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package week04.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();

}
}
110 changes: 110 additions & 0 deletions group01/1814014897/zhouhui/src/week04/lru/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package week04.lru;

/**
* 用双向链表实现LRU算法
* @author Hui Zhou
*
*/
public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

Node(int pageNum) {
this.pageNum = pageNum;
}
}

private int capacity;
private int size;

private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {
Node node = new Node(pageNum);

if(first == null){
init(node);
}
else if(size == capacity){
if(node.pageNum == first.pageNum){
return;
}
else if(node.pageNum == first.next.pageNum){
insertOfMidEqual(node);
}
else if(node.pageNum == last.pageNum){
insert(node);
}
else{
insert(node);
}
}
else{
addNode(node);
}
}

private void init(Node node) {
first = node;
last = first;
size++;
}

private void addNode(Node node) {
node.next = first;
first.prev = node;
first = node;
size++;
}

private void insert(Node node) {
last = first.next;
last.next = null;

node.next = first;
first.prev = node;
first = node;
}

private void insertOfMidEqual(Node node) {
first.next = last;
last.prev = first;

node.next = first;
first.prev = node;
first = node;
}

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();
}

}
31 changes: 31 additions & 0 deletions group01/1814014897/zhouhui/src/week04/lru/LRUPageFrameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package week04.lru;

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());
}

}

0 comments on commit 1cf57b6

Please sign in to comment.