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,79 @@
package io.github.vxzh.jvm.loader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class ClassFileLoader {

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

public byte[] readBinaryCode(String className) {

className = className.replaceAll("\\.", "/");
File file = findFile(className);
if (file == null) {
return new byte[0];
}

FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte buffer[] = new byte[BUFFER_MAX_SIZE];
int len = -1;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return null;
}

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

public String getClassPath() {

StringBuilder builder = new StringBuilder();
for (String path : clzPaths) {
builder.append(path).append(";");
}

return builder.toString().substring(0, builder.toString().length() - 1);
}

private File findFile(String className) {
for (String path : clzPaths) {
String filePath = path + "/" + className + ".class";
File file = new File(filePath);
if (file.exists()) {
return file;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.github.vxzh.jvm.test;

import io.github.vxzh.jvm.loader.ClassFileLoader;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ClassFileloaderTest {


static String path1 = "/Users/xuxiaoqing/Workspace/test";
static String path2 = "/Users/xuxiaoqing/Documents/demo";


@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 = "io.github.vxzh.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 = "io.github.vxzh.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();
}

}
29 changes: 29 additions & 0 deletions group13/2729382520/L4/src/io/github/vxzh/jvm/test/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.vxzh.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();
}
}
109 changes: 109 additions & 0 deletions group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.github.vxzh.lru;

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

private static class Node {
Node prev;
Node next;
int pageNum;

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

private int capacity;
private int size;
private Node first;// 链表头
private Node last;// 链表尾

public LRUPageFrame(int capacity) {
this.capacity = capacity;
}

/**
* 获取缓存中对象
*
* @param pageNum
* @return
*/
public void access(int pageNum) {
if (first != null && first.pageNum == pageNum) {
return;
}

removeNode(pageNum);
if (size() + 1 > capacity) {
removeLast();
}
addFirst(pageNum);

}

private int size() {
return size;
}

private void addFirst(int pageNum) {
Node f = first;
Node newNode = new Node(null, pageNum, f);
if (f == null)
last = newNode;
else
f.prev = newNode;
first = newNode;
size++;
}

private void removeLast() {

Node l = last;
Node prev = l.prev;
prev.next = null;
l.prev = null;
last = prev;
size--;
}

private void removeNode(int pageNum) {
Node node = first;
while (node != null) {
if (node.pageNum == pageNum) {
if (node == last) {
removeLast();
} else {
final Node prev = node.prev;
final Node next = node.next;
prev.next = next;
next.prev = prev;
node.prev = null;
node.next = null;
size--;
}
break;
} else {
node = node.next;
}
}
}

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

}
29 changes: 29 additions & 0 deletions group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.vxzh.lru;

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

}
27 changes: 27 additions & 0 deletions group13/2931408816/lesson4/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
group 'cn.net.pikachu'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.1.1'

repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Loading