Skip to content

Commit

Permalink
Merge pull request #47 from GZ-RXP/master
Browse files Browse the repository at this point in the history
Jvm first week - Magical Number
  • Loading branch information
gqipan authored Apr 12, 2017
2 parents 03513e4 + 8aafd6a commit a47d5a1
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.coderising.jvm.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.Iterator;
import java.util.List;



public class ClassFileLoader {

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

public byte[] readBinaryCode(String className) {

File classFile = getClassFileFromPath(className);

byte[] buffer = new byte[1024];
try {
FileInputStream fis = new FileInputStream(classFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int readLen;
while((readLen = fis.read(buffer))>-1){
baos.write(buffer, 0, readLen);
}

return baos.toByteArray();

} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}


public void addClassPath(String path) {
File clzPath = new File(path);
if(clzPath.exists() && clzPath.isDirectory()){
this.clzPaths.add(path);
}else{
System.out.println("Invalid path:"+ path);
}
}



public String getClassPath(){
StringBuilder sb = new StringBuilder();
Iterator it = this.clzPaths.iterator();
while(it.hasNext()){
if(sb.length()>0){
sb.append(";");
}
sb.append(it.next());
}
return sb.toString();
}

public File getClassFileFromPath(String className) {
Iterator it = this.clzPaths.iterator();

//replace "." with "\\" in windows
String fullclassPath = className.replaceAll("\\.", (File.separatorChar=='\\')?"\\\\":"/")+".class";

while(it.hasNext()){
File clzFile;
String path = (String)it.next();
if(path.endsWith(String.valueOf(File.separatorChar))){
clzFile = new File(path+fullclassPath);
}else{
clzFile = new File(path+File.separatorChar+fullclassPath);
}

//Check file before further proceed
if(clzFile.exists()&&clzFile.isFile()){
return clzFile;
}
}

throw new RuntimeException("Class not found:"+className);
}



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

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 = "C:\\Users\\Administrator\\mygit\\coding2017\\liuxin\\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() {

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

String className = "com.coderising.jvm.test.EmployeeV1";

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

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

}


@Test
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.coderising.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 group11/283091182/mini-jvm/com/coderising/jvm/test/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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();

}
}
141 changes: 141 additions & 0 deletions group11/283091182/src/com/coding/basic/linklist/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.coding.basic.linklist;

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

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}

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

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) {
System.out.println("CurrentList= "+this.toString()+"; accessing - "+pageNum);
Node target = find(pageNum);
if(target==null){
createNewNodeAsHead(pageNum);
}else{
moveExistingNodeToHead(target);
}

}

private void removeLast(){
Node secToLast = last.prev;
last = null;
secToLast.next = null;
last = secToLast;
size--;
}

private void moveExistingNodeToHead(Node node){

Node prev = node.prev;
Node next = node.next;

if(prev==null){
//already in the head,do nothing;
return;
}

if(next==null){
//currently in the tail
last = prev;
}

//in the middle
prev.next = next;
if(next!=null){
next.prev = prev;
}
node.prev = null;
node.next = first;
first = node;
}

private void createNewNodeAsHead(int value){
Node node = new Node(value,null,null);
//first node
if(size==0){
this.first = node;
this.last = node;
this.size ++;
}else{
//linklist already exists
this.first.prev = node;
node.next = this.first;
this.first = node;
this.size++;

if(size>capacity){
removeLast();
}
}

}

private Node find(int value){
if(size==0){
return null;
}
Node temp = first;
while(temp!=null){
if(temp.pageNum==value){
return temp;
}else{
temp = temp.next;
}
}
return null;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.coding.basic.linklist;

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 a47d5a1

Please sign in to comment.