Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.coderising.jvm.loader;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
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 {
String clzFileName = "";
byte[] byteCodes = null;
boolean classFound = false;

for (String path : clzPaths) {
clzFileName = path + File.separatorChar + className.replace('.', File.separatorChar) + ".class";

if ((byteCodes = loadClassFile(clzFileName)) != null){
classFound = true;
return byteCodes;
}
}

if (classFound == false) {
throw new FileNotFoundException(clzFileName);
}

return null;
}

private byte[] loadClassFile(String clzFileName) throws IOException {

File file = new File(clzFileName);
if(!file.exists()){
// throw new FileNotFoundException(clzFileName);
return null;
}

ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length());
BufferedInputStream in = null;

try {
in = new BufferedInputStream(new FileInputStream(file));

int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;

while ((len = in.read(buffer, 0, buf_size)) != -1) {
bos.write(buffer, 0, len);
}

return bos.toByteArray();

} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
}



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

public String getClassPath_V1(){

return null;
}

public String getClassPath(){
String classPath = "";
for (int i = 0; i < clzPaths.size(); i++) {
classPath += clzPaths.get(i);
if (i != clzPaths.size() - 1) {
classPath += ";";
}
}
return classPath;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.coderising.jvm.test;

import java.io.IOException;

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

import com.coderising.jvm.loader.ClassFileLoader;


public class ClassFileloaderTest {

static String path1 = "F:\\Project\\Java_Project\\Java_SE\\coding2017\\group24\\1525619747\\homework_20170402\\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 = "com.coderising.jvm.test.EmployeeV1";

byte[] byteCodes = loader.readBinaryCode(className);
// System.out.println(byteCodes.length);
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
Assert.assertEquals(1056, byteCodes.length);

}


@Test
public void testMagicNumber() throws IOException{
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.coderising.jvm.test.EmployeeV1";
byte[] byteCodes = loader.readBinaryCode(className);
System.out.println(byteCodes[0] + " " + byteCodes[1] + " " + byteCodes[2] + " " +byteCodes[3]);
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};


String acctualValue = this.byteToHexString(codes);
// System.out.println(acctualValue);
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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.coderising.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();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.coderising.lru;


public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

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

private int capacity;

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


public LRUPageFrame(int capacity) {
this.currentSize = 0;
this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {
Node node = find(pageNum);
// System.out.println(node + " " + isFull());
if (node != null) { // 队列中含有节点,移到头部
moveExistedNodeToHead(node);
} else { // 队列中没有该节点,需添加
node = new Node(pageNum);
if (!isFull()) {
// 在头部添加节点
addNodeToHead(node);
} else {
// 删掉尾部节点,再在头部添加节点
deleteLastNode();
addNodeToHead(node);
}
}
}

private void deleteLastNode() {
// TODO Auto-generated method stub
if (currentSize == 0) {
return;
}
if (currentSize == 1) {
first = last = null;
--currentSize;
return;
}
Node toDelete = last;
Node lastPrev = last.prev;
lastPrev.next = last.next;
last.next.prev = lastPrev;
last = lastPrev;
toDelete = null;
--currentSize;
}

private void addNodeToHead(Node node) {
// TODO Auto-generated method stub
if (first == null) {
first = last = node;
++currentSize;
return;
}
if (currentSize == 1) {
node.prev = first;
node.next = first;
first.prev = node;
first.next = node;
first = node;
++currentSize;
return;
}
node.prev = first.prev;
first.prev.next = node;
node.next = first;
first.prev = node;
first = node;
++currentSize;
}

private void moveExistedNodeToHead(Node node) {
// TODO Auto-generated method stub
if (node == first) {
return;
}
if (node == last) {
Node lastPrev = last.prev;
first = last;
last = lastPrev;
return;
}
node.prev.next = node.next;
node.next.prev = node.prev;

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

private Node find(int pageNum) {
Node pointer = first;
while (pointer != null && pointer != last) {
if (pointer.pageNum == pageNum) {
return pointer;
}
pointer = pointer.next;
}
// pointer == last now
if (pointer != null && pointer.pageNum == pageNum) {
return pointer;
}

return null;
}

private boolean isFull() {
return (currentSize == capacity);
}

public String toString() {
StringBuilder buffer = new StringBuilder();
Node node = first;
// 由于个人实现的双链表是环链的,所以这里做了更改
while(node != null && node != last){
buffer.append(node.pageNum);

node = node.next;
if(node != null){
buffer.append(",");
}
}
if (node != null) {
buffer.append(node.pageNum);
}
System.out.println(buffer.toString());
return buffer.toString();
}

}
Loading