Skip to content

Commit

Permalink
Merge pull request #2 from DonaldY/master
Browse files Browse the repository at this point in the history
同步代码
  • Loading branch information
EtheriousNatsu authored Apr 19, 2017
2 parents 5c9b6dd + dd5ab28 commit 5d373ae
Show file tree
Hide file tree
Showing 4,048 changed files with 202,482 additions and 35,504 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
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());
}
}
2 changes: 2 additions & 0 deletions group01/1814014897/zhouhui/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="D:/Program Files/Java/jdk1.8.0_74/lib/commons-io-2.5.jar"/>
<classpathentry kind="lib" path="D:/Program Files/Java/jdk1.8.0_74/lib/commons-lang3-3.5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Loading

0 comments on commit 5d373ae

Please sign in to comment.