Skip to content

Commit

Permalink
Merge pull request #27 from yanghaitao0410/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
diliuzuzhanghao authored Apr 5, 2017
2 parents 3b0213f + 7176f79 commit e62fb21
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 0 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.coding.basic.homework_04.jvm.loader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class ClassFileLoader {

private List<String> clzPaths = new ArrayList<String>();
private static final int BUFFER_SIZE = 1024;

public byte[] readBinaryCode(String className) {
byte[] result = null;
for(String path : clzPaths){
File file = new File(getPath(path, className));
if(!file.exists()){
continue;
}
result = readFile(file);
}
return result;
}

/**
* 文件数据存放在字节数组中返回
* @param file
* @return
*/
private byte[] readFile(File file){
FileInputStream fileInputStream;
byte[] buffer = new byte[BUFFER_SIZE];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

try {
fileInputStream = new FileInputStream(file);
while(byteArrayOutputStream.size() < file.length()){
int len = fileInputStream.read(buffer);
if(len < 0){
break;
}
byteArrayOutputStream.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}

if(byteArrayOutputStream.size() > file.length()){
byte[] result = byteArrayOutputStream.toByteArray();
return Arrays.copyOf(result, (int)file.length());
}
return byteArrayOutputStream.toByteArray();
}

/**
* 获取真实路径路径
* @param className
* @return
*/
private String getPath(String path ,String className){
System.out.println(className);
String [] ways = className.split("\\.");
for (String string : ways) {
System.out.println(string);
}
StringBuilder builder = new StringBuilder();
builder.append(path);
for (String string : ways) {

builder.append("\\");
builder.append(string);
}
builder.append(".class");
System.out.println(builder.toString());
return builder.toString();
}

private byte[] loadClassFile(String clzFileName) {

return null;
}



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

public String getClassPath_V1(){

return null;
}


public String getClassPath(){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < clzPaths.size(); i++){
builder.append(clzPaths.get(i));
if(i < clzPaths.size() - 1){
builder.append(";");
}
}
return builder.toString();
}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.coding.basic.homework_04.jvm.test;

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

import com.coding.basic.homework_04.jvm.loader.ClassFileLoader;

public class ClassFileloaderTest {


static String path1 = "C:\\Users\\yanght\\Documents\\mygit\\coding2017-1\\group06\\1454385822\\bin";
static String path2 = "D:\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.coding.basic.homework_04.jvm.test.EmployeeV1";
// String className = "EmployeeV1";

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

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

}


@Test
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.coding.basic.homework_04.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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.coding.basic.homework_04.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,137 @@
package com.coding.basic.homework_04.lru;

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

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
Node(int pageNum){
this.pageNum = pageNum;
}
}

private int capacity;


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


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {
if(first == null){ //缓存第一个数据
first = new Node(pageNum);
first.next = first.prev = null;
last = first;
}else if(size() < capacity && first != null){ //缓存页未满时
Node node = new Node(pageNum);
if(last.prev == null){
last.prev = node;
node.next = last;
}else{
node.next = first;
first.prev = node;
node.prev = null;
}
first = node;
}else if(size() >= capacity){ //缓存页满了
Node node = getNode(pageNum);
LRU(node, pageNum);
}
}

/**
* lru算法
* @param node
* @param pageNum
*/
private void LRU(Node node, int pageNum){
if(node != null){
if(last.pageNum == node.pageNum){ //缓存是last
last = node.prev;
last.next = null;
node.next = first;
first.prev = node;
node.prev = null;
first = node;
}else if(last.pageNum != node.pageNum && first.pageNum != node.pageNum){
//缓存在first和last的中间范围
node.prev.next = node.next;
node.next.prev = node.prev;
node.prev = null;
node.next = first;
first = node;
}
}else{
//新缓存
last = last.prev;
last.next = null;
node = new Node(pageNum);
node.next = first;
first.prev = node;
first = node;
}
}

/**
* 根据数据在缓存中获取节点
* @param pageNum
* @return
*/
private Node getNode(int pageNum){
Node node = first;
while(node != null){
if(node.pageNum == pageNum){
return node;
}
node = node.next;
}
return null;
}

public int size(){
int num = 0;
Node node = first;
while(node != null){
num++;
node = node.next;
}
return num;
}

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

}
Loading

0 comments on commit e62fb21

Please sign in to comment.