Skip to content

Commit 218a213

Browse files
authored
Merge pull request #57 from onlyliuxin/master
update
2 parents d814b76 + e6fe324 commit 218a213

File tree

401 files changed

+22619
-1977
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

401 files changed

+22619
-1977
lines changed

group03/1196051822/3月13日作业的代码/src/download/api/ConnectionException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22

33
public class ConnectionException extends Exception {
44

5+
/**
6+
* 自定义异常错误
7+
* @param string
8+
*/
9+
public ConnectionException(String string) {
10+
11+
}
12+
513
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coderising.download.api;
22

33
public interface DownloadListener {
4+
45
public void notifyFinished();
56
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
54
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
66
<classpathentry kind="output" path="bin"/>
77
</classpath>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.coderising.jvm.loader;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
14+
15+
public class ClassFileLoader {
16+
17+
private List<String> clzPaths = new ArrayList<String>();
18+
19+
public byte[] readBinaryCode(String className) {
20+
/**
21+
* class文件存储位置
22+
*/
23+
String location = clzPaths.get(0);
24+
File file = new File(location);
25+
File[] files = file.listFiles();
26+
InputStream in = null;
27+
byte[] bt = null;
28+
29+
int size = 0;
30+
for(File fileSon:files){
31+
/**
32+
* 判断出为class文件时
33+
*/
34+
if(fileSon.isFile() && fileSon.getName().endsWith("EmployeeV1.class")){
35+
try {
36+
long length = fileSon.length();
37+
bt = new byte[(int) length];
38+
byte[] context = new byte[1024];
39+
in = new FileInputStream(fileSon);
40+
int tempbyte;
41+
while((tempbyte = in.read(context)) != -1){
42+
for(int i = 0;i < context.length;i++){
43+
System.arraycopy(context, 0, bt, size, tempbyte);
44+
}
45+
size = tempbyte;
46+
}
47+
48+
} catch (FileNotFoundException e) {
49+
e.printStackTrace();
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}finally{
53+
if(in != null){
54+
try {
55+
in.close();
56+
} catch (IOException e) {
57+
}
58+
}
59+
}
60+
61+
}
62+
}
63+
return bt;
64+
}
65+
66+
67+
public void addClassPath(String path) {
68+
69+
clzPaths.add(path);
70+
71+
}
72+
73+
74+
75+
public String getClassPath(){
76+
StringBuilder sb = new StringBuilder();
77+
for(int i = 0;i < clzPaths.size();i++){
78+
if(i == clzPaths.size() - 1){
79+
sb.append(clzPaths.get(i));
80+
break;
81+
}
82+
sb.append(clzPaths.get(i)).append(";");
83+
84+
}
85+
return sb.toString();
86+
}
87+
88+
89+
90+
91+
92+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.coderising.jvm.test;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import com.coderising.jvm.loader.ClassFileLoader;
9+
10+
11+
12+
13+
14+
public class ClassFileloaderTest {
15+
16+
17+
static String path1 = "F:/myGithub/coding2017/group04/1020483199/FourthHomeWork/bin/com/coderising/jvm/test";
18+
static String path2 = "C:/temp";
19+
20+
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
}
25+
26+
@After
27+
public void tearDown() throws Exception {
28+
}
29+
30+
@Test
31+
public void testClassPath(){
32+
33+
ClassFileLoader loader = new ClassFileLoader();
34+
loader.addClassPath(path1);
35+
loader.addClassPath(path2);
36+
37+
String clzPath = loader.getClassPath();
38+
Assert.assertEquals(path1+";"+path2,clzPath);
39+
40+
}
41+
42+
@Test
43+
public void testClassFileLength() {
44+
45+
ClassFileLoader loader = new ClassFileLoader();
46+
47+
loader.addClassPath(path1);
48+
49+
String className = "com.coderising.jvm.test.EmployeeV1";
50+
51+
byte[] byteCodes = loader.readBinaryCode(className);
52+
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
53+
Assert.assertEquals(1056, byteCodes.length);
54+
55+
}
56+
57+
58+
@Test
59+
public void testMagicNumber(){
60+
ClassFileLoader loader = new ClassFileLoader();
61+
loader.addClassPath(path1);
62+
String className = "com.coderising.jvm.test.EmployeeV1";
63+
byte[] byteCodes = loader.readBinaryCode(className);
64+
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};
65+
66+
67+
String acctualValue = this.byteToHexString(codes);
68+
69+
Assert.assertEquals("cafebabe", acctualValue);
70+
}
71+
72+
73+
74+
75+
76+
77+
private String byteToHexString(byte[] codes ){
78+
StringBuffer buffer = new StringBuffer();
79+
for(int i=0;i<codes.length;i++){
80+
byte b = codes[i];
81+
int value = b & 0xFF;
82+
String strHex = Integer.toHexString(value);
83+
if(strHex.length()< 2){
84+
strHex = "0" + strHex;
85+
}
86+
buffer.append(strHex);
87+
}
88+
return buffer.toString();
89+
}
90+
91+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderising.jvm.test;
2+
3+
public class EmployeeV1 {
4+
5+
6+
private String name;
7+
private int age;
8+
9+
public EmployeeV1(String name, int age) {
10+
this.name = name;
11+
this.age = age;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
public void setAge(int age){
18+
this.age = age;
19+
}
20+
public void sayHello() {
21+
System.out.println("Hello , this is class Employee ");
22+
}
23+
public static void main(String[] args){
24+
EmployeeV1 p = new EmployeeV1("Andy",29);
25+
p.sayHello();
26+
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic.linkedList;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.coding.basic.linkedList;
2+
3+
/**
4+
* 用双向链表实现LRU算法
5+
* @author liuxin
6+
*
7+
*/
8+
public class LRUPageFrame {
9+
10+
private static int stateAtHead = 0;
11+
private static int stateAtLast = 1;
12+
private static int stateAtMid = 2;
13+
14+
private static class Node {
15+
16+
Node prev;
17+
Node next;
18+
int pageNum;
19+
20+
Node(){
21+
22+
}
23+
}
24+
25+
private int capacity;
26+
27+
private int size;
28+
private Node first;// 链表头
29+
private Node last;// 链表尾
30+
31+
32+
public LRUPageFrame(int capacity) {
33+
34+
this.capacity = capacity;
35+
36+
}
37+
38+
/**
39+
* 获取缓存中对象
40+
*
41+
* @param key
42+
* @return
43+
*/
44+
public void access(int pageNum) {
45+
/**
46+
* 空链表
47+
*/
48+
size ++;
49+
Node newNode = new Node();
50+
newNode.pageNum = pageNum;
51+
if(size <= capacity){
52+
if(last == null){
53+
last= newNode;
54+
first = newNode;
55+
}else{
56+
Node preHead = first;//记录原来的头节点
57+
first = newNode;
58+
first.next = preHead;
59+
preHead.prev = first;
60+
}
61+
}else{
62+
Node currentNode = first;
63+
int judgeNum = currentNode.pageNum;
64+
Node preHead = first;//记录原来的头节点
65+
Node preLast = last;//记录原来的尾节点
66+
int state = -1;//记录当前状态
67+
/**
68+
* 当前插入的值为等于链表头的位置
69+
*/
70+
71+
int k = 0;
72+
if(judgeNum == pageNum){
73+
state = stateAtHead;
74+
}else if(preLast.pageNum == pageNum){
75+
state = stateAtLast;
76+
}
77+
while(currentNode != null && k < capacity - 1){
78+
k++;
79+
currentNode = currentNode.next;
80+
judgeNum = currentNode.pageNum;
81+
if(judgeNum == pageNum && judgeNum != preLast.pageNum){
82+
state = stateAtMid;
83+
break;
84+
}
85+
}
86+
87+
switch (state) {
88+
case -1:
89+
first = newNode;
90+
last = preLast.prev;
91+
first.next = preHead;
92+
first.prev = null;
93+
preHead.next = last;
94+
preHead.prev = first;
95+
last.next = null;
96+
break;
97+
case 0:
98+
99+
break;
100+
101+
case 1:
102+
last = preLast.prev;
103+
last.next = null;//原来尾节点的上一个节点变成了尾节点
104+
first = newNode;
105+
first.prev = null;
106+
first.next = preHead;
107+
break;
108+
case 2:
109+
first = newNode;
110+
first.next = preHead;
111+
first.prev = null;
112+
preHead.next = last;
113+
preHead.prev = first;
114+
last.prev = preHead;
115+
break;
116+
}
117+
118+
}
119+
120+
121+
122+
123+
}
124+
125+
126+
127+
public String toString(){
128+
StringBuilder buffer = new StringBuilder();
129+
Node node = first;
130+
while(node != null){
131+
buffer.append(node.pageNum);
132+
133+
node = node.next;
134+
if(node != null){
135+
buffer.append(",");
136+
}
137+
}
138+
return buffer.toString();
139+
}
140+
141+
}

0 commit comments

Comments
 (0)