Skip to content

Commit

Permalink
jvm
Browse files Browse the repository at this point in the history
  • Loading branch information
龚启盼 committed Apr 11, 2017
1 parent 8afbe08 commit a260cd7
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.ArrayList;
import java.util.List;

public class ClassFileLoader {
public class ClassFileLoaderTmp {
private List<String> clzPaths = new ArrayList<String>();
int countForClassPath = 0;
int countForReadBinaryCode = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.pan.jvm;

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
Expand Up @@ -18,31 +18,21 @@ public class ClassFileLoader {
private List<String> clzPaths = new ArrayList();

public byte[] readBinaryCode(String className) {

className = className.replace('.', File.separatorChar) + ".class";

for (String path : this.clzPaths) {

String clzFileName = path + File.separatorChar + className;
byte[] codes = loadClassFile(clzFileName);
if (codes != null) {
return codes;
}
}

return null;


}

private byte[] loadClassFile(String clzFileName) {

File f = new File(clzFileName);

try {

return IOUtils.toByteArray(new FileInputStream(f));

} catch (IOException e) {
e.printStackTrace();
return null;
Expand All @@ -54,9 +44,7 @@ public void addClassPath(String path) {
if (this.clzPaths.contains(path)) {
return;
}

this.clzPaths.add(path);

}


Expand All @@ -74,7 +62,6 @@ public ClassFile loadClass(String className) {

// ------------------------------backup------------------------
public String getClassPath_V1() {

StringBuffer buffer = new StringBuffer();
for (int i = 0; i < this.clzPaths.size(); i++) {
buffer.append(this.clzPaths.get(i));
Expand All @@ -88,31 +75,19 @@ public String getClassPath_V1() {
private byte[] loadClassFile_V1(String clzFileName) {

BufferedInputStream bis = null;

try {

File f = new File(clzFileName);


bis = new BufferedInputStream(new FileInputStream(f));

ByteArrayOutputStream bos = new ByteArrayOutputStream();


byte[] buffer = new byte[1024];
int length = -1;

while ((length = bis.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}

byte[] codes = bos.toByteArray();

return codes;

} catch (IOException e) {
e.printStackTrace();

} finally {
if (bis != null) {
try {
Expand All @@ -123,7 +98,6 @@ private byte[] loadClassFile_V1(String clzFileName) {
}
}
return null;

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.pan.alg;

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());
frame.access(5);
Assert.assertEquals("5,4,0", frame.toString());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.pan.jvm;

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
@@ -1,10 +1,41 @@
package com.pan.jvm;

import com.pan.jvm.loader.ClassFileLoader;
import org.junit.Test;

import java.io.IOException;

/**
* 用于测试第一次JVM作业,读取.class作业 和 魔幻数字
*/
public class TestReadCFBB {

@Test
public void testClassPath(){
ClassFileLoader classFileLoader = new ClassFileLoader();
String path = ClassFileLoader.class.getClassLoader().getResource("").getPath();
path = path.replace("test-classes", "classes");
classFileLoader.addClassPath(path);
classFileLoader.addClassPath("d://tmp");

String clzPath = classFileLoader.getClassPath();
System.out.println(clzPath);
}



@Test
public void testReadCFBB() throws IOException {

ClassFileLoader classFileLoader = new ClassFileLoader();
String path = ClassFileLoader.class.getClassLoader().getResource("").getPath();
path = path.replace("test-classes", "classes");
classFileLoader.addClassPath(path);
byte[] bytes = classFileLoader.readBinaryCode("com.pan.jvm.loader.ClassFileLoader");
for (byte b : bytes) {
String toHexString = Integer.toHexString(b & 0xFF).toUpperCase();
System.out.print(toHexString + " ");
}
}

}

0 comments on commit a260cd7

Please sign in to comment.