Skip to content

Commit

Permalink
Merge pull request #2 from zavier/master
Browse files Browse the repository at this point in the history
第五周更新仓库
  • Loading branch information
PigTS authored Apr 9, 2017
2 parents 1cf57b6 + 622cbbf commit da8d046
Show file tree
Hide file tree
Showing 1,167 changed files with 56,470 additions and 4,599 deletions.
116 changes: 116 additions & 0 deletions group01/1298552064/src/week04/lru/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package week04.lru;

public class LRUPageFrame {
private static class Node {

Node prev;
Node next;
int pageNum;

Node(Node prev, Node next, int pageNum) {
this.prev = prev;
this.next = next;
this.pageNum = pageNum;
}
}

private int capacity;

private Node first;// 链表头
private Node last;// 链表尾
private int size; // 链表长度

public LRUPageFrame(int capacity) {
this.capacity = capacity;
}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {
int index = find(pageNum);
if (size != 0) {
if(index >= 0){
remove(index);
}else if(size == capacity){
remove(size - 1);
}
}
addToHead(pageNum);
}

public void remove(int index) {
if (index == 0) {
if(size == 1){
first = last = null;
}else{
first = first.next;
first.prev = null;
}
} else if (index == (size - 1)) {
if(size == 1){
first = last = null;
}else{
last = last.prev;
last.next = null;
}
} else {
Node node = first;
for (int i = 1; i < index; i++) {
node = node.next;
}

Node nxt = node.next;

node.next = nxt.next;
(nxt.next).prev = node;
nxt = null;
}
size--;
}

public int find(int pageNum) {
int index = 0;
Node cur = first;
while (cur != null) {
if (pageNum == cur.pageNum) {
return index;
}
cur = cur.next;
index++;
}
return -1;
}

public void addToHead(int pageNum) {
// 链表为空
if (first == null) {
Node node = new Node(null, null, pageNum);
first = node;
last = node;
} else {
Node node = new Node(null,first,pageNum);
first.prev = node;
first = node;
}
size ++;
}

@Override
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();
}
}
67 changes: 67 additions & 0 deletions group01/1298552064/src/week04/minijvm/loader/ClassFileLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package week04.minijvm.loader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ClassFileLoader {
private List<String> clzPaths = new ArrayList<String>();

public byte[] readBinaryCode(String className) throws IOException {
if(className == null){
return null;
}

boolean isFileExist = false;
File file = null;
String classPath = className.replace(".", "\\");
for(int i = 0 ; i < clzPaths.size(); i++){
String basePath = clzPaths.get(i);
file = new File(basePath + File.separator + classPath + ".class");

if(file.exists()){
isFileExist = true;
break;
}
}

//找不到类
if(!isFileExist){
throw new FileNotFoundException();
}

//读取字节码文件到数组
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte [] rs = new byte[1024];
int len = 0;
while((len = in.read(rs)) != -1){
bos.write(rs, 0, len);
}
bos.close();
in.close();
System.out.println("readBinaryCode:" + " file size = " + file.length());
return bos.toByteArray();
}

public void addClassPath(String path) {
if(! clzPaths.contains(path)){
clzPaths.add(path);
}
}

public String getClassPath() {
StringBuffer buffer = new StringBuffer();
for(int i = 0;i < clzPaths.size();i++){
buffer.append(clzPaths.get(i));
if(i != clzPaths.size() - 1){
buffer.append(";");
}
}
return buffer.toString();
}
}
78 changes: 78 additions & 0 deletions group01/1298552064/src/week04/test/ClassFileloaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package week04.test;

import java.io.IOException;

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

import week04.minijvm.loader.ClassFileLoader;


public class ClassFileloaderTest {
static String path1 = "D:\\Git_2017\\coding2017\\group01\\1298552064\\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 IOException {

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

String className = "week04.test.EmployeeV1";

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

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

}

@Test
public void testMagicNumber() throws IOException {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "week04.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();
}
}
30 changes: 30 additions & 0 deletions group01/1298552064/src/week04/test/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package week04.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();

}
}
29 changes: 29 additions & 0 deletions group01/1298552064/src/week04/test/LRUPageFrameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package week04.test;

import org.junit.Assert;
import org.junit.Test;

import week04.lru.LRUPageFrame;

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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.coding2017.week4.jvm.loader;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ClassFileLoader {
private static Joiner SEMICOLON_JOINER = Joiner.on(";").skipNulls();
private static Splitter DOT_SPLITTER = Splitter.on(".").trimResults();
private static Joiner SLASH_JOINER = Joiner.on("/").skipNulls();

private static String CLASS_SUFFIX = ".class";

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

public byte[] readBinaryCode(String className) {
List<String> list = DOT_SPLITTER.splitToList(className);
String childDirectory = SLASH_JOINER.join(list);
for (String clzPath : clzPaths) {
String fullPath = makeFullPath(clzPath, childDirectory);
if (fileExist(fullPath)) {
return readFileBytes(fullPath);
}
}
System.out.println("no this class file: " + className);
return null;
}

private byte[] readFileBytes(String filePath) {
try {
File file = new File(filePath);
long length = file.length();
byte[] fileBytes = new byte[(int) length];
int readLength = new FileInputStream(filePath).read(fileBytes);
if (readLength != length) {
System.out.println("read file error. read length: " + readLength + ", full length : " + length);
return null;
}
return fileBytes;
} catch (IOException e) {
System.out.println("read file error. " + filePath);
return null;
}
}

private boolean fileExist(String fullPath) {
File classFile = new File(fullPath);
return classFile.exists() && classFile.isFile();
}

private String makeFullPath(String clzPath, String childDirectory) {
if (clzPath.endsWith("/") || clzPath.endsWith("\\")) {
return clzPath + childDirectory + CLASS_SUFFIX;
} else {
return clzPath + "/" + childDirectory + CLASS_SUFFIX;
}
}

public void addClassPath(String path) {
if (!clzPaths.contains(path)) {
clzPaths.add(path);
}
}

public String getClassPath() {
return SEMICOLON_JOINER.join(clzPaths);
}

}
Loading

0 comments on commit da8d046

Please sign in to comment.