Skip to content

Commit

Permalink
JVM第一周作业
Browse files Browse the repository at this point in the history
  • Loading branch information
lingchen1dian21fen committed Mar 30, 2017
1 parent 34472e3 commit c9152aa
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 6 deletions.
135 changes: 135 additions & 0 deletions group10/904627477/src/com/coding/basic/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.coding.basic;

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

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
}

private int capacity;


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


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {
Node node = findPageNumNode(pageNum);
if(node==null){
if(size()>=capacity){
removeLast();
}
push(pageNum);
}else{
moveToFirst(node);
}
}

public Node findPageNumNode(int pageNum) {
Node node = first;
while(node!=null){
if(node.pageNum==pageNum){
break;
}else{
node = node.next;
}
}
return node;
}

public void moveToFirst(Node node) {
if(first==null||node==null||first.pageNum==node.pageNum){
return;
}
if(node.pageNum == last.pageNum){
node.prev.next = null;
last = node.prev;
}else{
Node tPrev = node.prev;
Node tNext = node.next;
tPrev.next = tNext;
tNext.prev = tPrev;
}
node.prev = null;
node.next = first;
first = node;
}

public void push(int pageNum) {
if(size()==0){
first = new Node();
first.pageNum = pageNum;
last = first;
return ;
}
Node node;
node = new Node();
node.pageNum = pageNum;
node.next = first;
first.prev = node;
first = node;
}

public void removeLast() {
if(last==null){
return ;
}
if(size()==1){
first = null;
last = null;
return ;
}
Node temp = last.prev;
last.prev = null;
last = temp;
last.next = null;
}

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

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

}
6 changes: 0 additions & 6 deletions group10/904627477/src/com/coding/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.coding.download;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;

import com.coding.download.api.Connection;
import com.coding.download.api.ConnectionException;
import com.coding.download.api.ConnectionManager;
import com.coding.download.api.DownloadListener;
import com.coding.download.api.Resource;
import com.coding.util.IOUtils;


Expand Down
88 changes: 88 additions & 0 deletions group10/904627477/src/com/coding/test/ClassFileloaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.coding.test;

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

import com.conding.jvm.loader.ClassFileLoader;

public class ClassFileloaderTest {


static String path1 = "D:\\workspace\\MyGithub\\coding2017\\group10\\904627477\\target\\classes";
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.coding.test.EmployeeV1";

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

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

}


@Test
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.coding.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 group10/904627477/src/com/coding/test/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.coding.test;
@SuppressWarnings("unused")
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();

}
}
32 changes: 32 additions & 0 deletions group10/904627477/src/com/coding/test/LRUPageFrameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.coding.test;

import org.junit.Assert;
import org.junit.Test;

import com.coding.basic.LRUPageFrame;


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

}
30 changes: 30 additions & 0 deletions group10/904627477/src/com/coding/util/IOUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.coding.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class IOUtils {

private final static int DEFAULT_SIZE = 1024;

public static void writeFile(File file,int startPos,byte[] buff) throws IOException{
if(buff==null){
return;
Expand Down Expand Up @@ -37,4 +41,30 @@ public static void createFile(long length,String filePath){
}
}

public static byte[] readFile(String filePath) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream in = null;
try {
in = new FileInputStream(filePath);
byte[] buff = new byte[DEFAULT_SIZE];
int len = 0;
while((len=in.read(buff))!=-1){
out.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return out.toByteArray();
}

}
Loading

0 comments on commit c9152aa

Please sign in to comment.