Skip to content
Merged
4 changes: 3 additions & 1 deletion group02/812350401/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
target/
.idea/
src/main/java/train/
src/main/resources/
!src/main/resources/**/*.class
!src/main/resources/**/*.xml
src/main/resources/**/*.png
14 changes: 14 additions & 0 deletions group02/812350401/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>coding2017</groupId>
<artifactId>812350401</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
Expand All @@ -32,7 +33,20 @@
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

</dependencies>



</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.github.miniyk2012.coding2017.basic.linklist;

/**
* 用双向链表实现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 int size;

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


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param pageNum: page值
* @return
*/
public void access(int pageNum) {
Node hitNode = get(pageNum);
if (null == hitNode) {
hitNode = new Node(pageNum);
addTop(hitNode);
if (size > capacity) {
delBottom();
}
} else {
switchTop(hitNode);
}
}

/**
* 获取值为pageNum的Node,如果没有返回null
* @param pageNum
* @return
*/
private Node get(int pageNum) {
Node currentNode = first;
while (currentNode != null) {
if (currentNode.pageNum == pageNum) return currentNode;
currentNode = currentNode.next;
}
return null;
}

/**
* 往顶部放一个Node
* @param node
*/
private void addTop(Node node) {
size++;
if (first == null) {
first = last = node;
} else {
node.next = first;
first.prev = node;
first = node;
}
}

/**
* 把node和顶部做交换
* @param node
*/
private void switchTop(Node node) {
if (node == first) return;
Node preNode = node.prev;
Node nextNode = node.next;
preNode.next = nextNode;
if (nextNode != null) {
nextNode.prev = preNode;
} else {
last = preNode;
}
node.next = node.prev = null;
addTop(node);
size--;
}

/**
* 把底部的踢掉
*/
private void delBottom() {
size--;
if (last == first) first = null;
Node temp = last;
last = last.prev;
temp.prev = null;
last.next = 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();
}

/**
* 测试双向链表逆序输出
* @return
*/
public String lastToString(){
StringBuilder buffer = new StringBuilder();
Node node = last;
while(node != null){
buffer.append(node.pageNum);

node = node.prev;
if(node != null){
buffer.append(",");
}
}
return buffer.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.miniyk2012.coding2017.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);
Assert.assertEquals("0,7", frame.toString());
Assert.assertEquals("7,0", frame.lastToString());
frame.access(1);
Assert.assertEquals("1,0,7", frame.toString());
Assert.assertEquals("7,0,1", frame.lastToString());
frame.access(2);
Assert.assertEquals("2,1,0", frame.toString());
Assert.assertEquals("0,1,2", frame.lastToString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
Assert.assertEquals("1,2,0", frame.lastToString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
Assert.assertEquals("1,2,0", frame.lastToString());
frame.access(3);
Assert.assertEquals("3,0,2", frame.toString());
Assert.assertEquals("2,0,3", frame.lastToString());
frame.access(0);
Assert.assertEquals("0,3,2", frame.toString());
Assert.assertEquals("2,3,0", frame.lastToString());
frame.access(4);
Assert.assertEquals("4,0,3", frame.toString());
Assert.assertEquals("3,0,4", frame.lastToString());
frame.access(4);
Assert.assertEquals("4,0,3", frame.toString());
Assert.assertEquals("3,0,4", frame.lastToString());
frame.access(4);
Assert.assertEquals("4,0,3", frame.toString());
Assert.assertEquals("3,0,4", frame.lastToString());
frame.access(0);
Assert.assertEquals("0,4,3", frame.toString());
Assert.assertEquals("3,4,0", frame.lastToString());
frame.access(7);
Assert.assertEquals("7,0,4", frame.toString());
Assert.assertEquals("4,0,7", frame.lastToString());
frame.access(4);
Assert.assertEquals("4,7,0", frame.toString());
Assert.assertEquals("0,7,4", frame.lastToString());

LRUPageFrame frame2 = new LRUPageFrame(1);
Assert.assertEquals("", frame2.toString());
frame2.access(7);
Assert.assertEquals("7", frame2.toString());
frame2.access(0);
Assert.assertEquals("0", frame2.toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.miniyk2012.coding2017.coderising.jvm.loader;

import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ClassFileLoader {

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

public byte[] readBinaryCode(String className) {
// InputStream is = null;
// ByteArrayOutputStream bas = null;
// byte[] ret = null;
// try {
// is = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(className));
// bas = new ByteArrayOutputStream();
// byte[] buf = new byte[1024];
// int bytesRead = 0;
// while ((bytesRead = is.read(buf)) != -1) {
// bas.write(buf, 0, bytesRead);
// }
// ret = bas.toByteArray();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// if (is != null)
// is.close();
// if (bas != null)
// bas.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// return ret;
int lastIndexDot = className.lastIndexOf(".");
String classPath = className.substring(0, lastIndexDot).replace(".", File.separator)
+ className.substring(lastIndexDot);
for (String parentPath: clzPaths) {
try {
String fullPath = parentPath + File.separator + classPath;
return FileUtils.readFileToByteArray(new File(fullPath));
} catch (IOException e) {
continue;
}
}
return null;
}

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

public String getClassPath(){
return String.join(";", clzPaths);
}

public static void main(String[] args) {
new ClassFileLoader().readBinaryCode("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.miniyk2012.coding2017.coderising.jvm.test;

import com.github.miniyk2012.coding2017.coderising.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 = ClassFileloaderTest.class.getClassLoader().getResource("struts").getPath();
static String path2 = ClassFileloaderTest.class.getClassLoader().getResource("jvm").getPath();

@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);
loader.addClassPath(path2);

String className = "com.github.miniyk2012.coding2017.jvm.test.EmployeeV1.class";

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

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

}

@Test
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
loader.addClassPath(path2);
String className = "com.github.miniyk2012.coding2017.jvm.test.EmployeeV1.class";
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);
}

@org.jetbrains.annotations.NotNull
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();
}

}
Loading