Skip to content

Commit

Permalink
Merge pull request #29 from Jiandan1357/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
Mr-ChangK authored Apr 9, 2017
2 parents eb88881 + db1ef05 commit fea41ca
Show file tree
Hide file tree
Showing 15 changed files with 650 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.loader;

import java.io.*;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;



public class ClassFileLoader {

private List<String> clzPaths = new ArrayList<>();

public byte[] readBinaryCode(String className) throws ClassNotFoundException {

String fileName = className.replace('.',File.separatorChar)+".class";
InputStream is = null;
try{
is = new FileInputStream(fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while((length = is.read(buffer)) != -1){
baos.write(buffer,0,length);
}
return baos.toByteArray();
} catch (IOException e){
e.printStackTrace();
} finally {
if(is!=null){
try{
is.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
return null;
}




public void addClassPath(String path) {

StringBuilder str = new StringBuilder(path);

}



public String getClassPath(){

String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();

return path;
}





}
88 changes: 88 additions & 0 deletions group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.test;

import com.loader.ClassFileLoader;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;


public class ClassFileloaderTest {


public static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin";
public 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 ClassNotFoundException {

ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);

String className = "EmployeeV1";

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

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

}


@Test
public void testMagicNumber() throws ClassNotFoundException {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "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 group23/601689050/4LRU&JVM/JVM/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.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();

}
}
5 changes: 5 additions & 0 deletions group23/601689050/4LRU&JVM/LRU/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public interface Iterator {
public boolean hasNext();
public Object next();

}
117 changes: 117 additions & 0 deletions group23/601689050/4LRU&JVM/LRU/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 用双向链表实现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;

}

/**
* 获取缓存中对象
*
* @param
* @return
*/
int size = 0;
public void access(int pageNum) {

if(size == 0){
Node node = new Node();
node.pageNum = pageNum;
node.prev = node.next = null;
first = last = node;
size++;
}else{
if(first.pageNum == pageNum){
return;
}
if(size == 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 (size > capacity) {
last = last.prev;
last.next = null;
}
} else {
moveToFirst(iteratorNode);
}
}
}

}

private void moveLastToFirst() {
last.next = first;
first.prev = last;
first = last;
last = last.prev;
last.next = null;
first.prev = null;
}
private 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;
}

private void addToFirst(int pageNum) {
Node node = new Node();
node.pageNum = pageNum;
node.prev = null;
node.next = first;
first.prev = node;
first = node;
size++;
}


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

}
29 changes: 29 additions & 0 deletions group23/601689050/4LRU&JVM/LRU/LRUPageFrameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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());
}

}
Loading

0 comments on commit fea41ca

Please sign in to comment.