-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from onlyliuxin/master
请组长pull以下刘欣老师的repo
- Loading branch information
Showing
1,120 changed files
with
53,446 additions
and
4,703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
group03/1196051822/3月13日作业的代码/src/download/api/DownloadListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.coderising.download.api; | ||
|
||
public interface DownloadListener { | ||
|
||
public void notifyFinished(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
92 changes: 92 additions & 0 deletions
92
group04/1020483199/FourthHomeWork/src/com/coderising/jvm/loader/ClassFileLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.coderising.jvm.loader; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
|
||
|
||
public class ClassFileLoader { | ||
|
||
private List<String> clzPaths = new ArrayList<String>(); | ||
|
||
public byte[] readBinaryCode(String className) { | ||
/** | ||
* class文件存储位置 | ||
*/ | ||
String location = clzPaths.get(0); | ||
File file = new File(location); | ||
File[] files = file.listFiles(); | ||
InputStream in = null; | ||
byte[] bt = null; | ||
|
||
int size = 0; | ||
for(File fileSon:files){ | ||
/** | ||
* 判断出为class文件时 | ||
*/ | ||
if(fileSon.isFile() && fileSon.getName().endsWith("EmployeeV1.class")){ | ||
try { | ||
long length = fileSon.length(); | ||
bt = new byte[(int) length]; | ||
byte[] context = new byte[1024]; | ||
in = new FileInputStream(fileSon); | ||
int tempbyte; | ||
while((tempbyte = in.read(context)) != -1){ | ||
for(int i = 0;i < context.length;i++){ | ||
System.arraycopy(context, 0, bt, size, tempbyte); | ||
} | ||
size = tempbyte; | ||
} | ||
|
||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
}finally{ | ||
if(in != null){ | ||
try { | ||
in.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
return bt; | ||
} | ||
|
||
|
||
public void addClassPath(String path) { | ||
|
||
clzPaths.add(path); | ||
|
||
} | ||
|
||
|
||
|
||
public String getClassPath(){ | ||
StringBuilder sb = new StringBuilder(); | ||
for(int i = 0;i < clzPaths.size();i++){ | ||
if(i == clzPaths.size() - 1){ | ||
sb.append(clzPaths.get(i)); | ||
break; | ||
} | ||
sb.append(clzPaths.get(i)).append(";"); | ||
|
||
} | ||
return sb.toString(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
91 changes: 91 additions & 0 deletions
91
group04/1020483199/FourthHomeWork/src/com/coderising/jvm/test/ClassFileloaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.coderising.jvm.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.coderising.jvm.loader.ClassFileLoader; | ||
|
||
|
||
|
||
|
||
|
||
public class ClassFileloaderTest { | ||
|
||
|
||
static String path1 = "F:/myGithub/coding2017/group04/1020483199/FourthHomeWork/bin/com/coderising/jvm/test"; | ||
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.coderising.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 = "com.coderising.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(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
group04/1020483199/FourthHomeWork/src/com/coderising/jvm/test/EmployeeV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.coderising.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(); | ||
|
||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
group04/1020483199/FourthHomeWork/src/com/coding/basic/linkedList/Iterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.coding.basic.linkedList; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
141 changes: 141 additions & 0 deletions
141
group04/1020483199/FourthHomeWork/src/com/coding/basic/linkedList/LRUPageFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.coding.basic.linkedList; | ||
|
||
/** | ||
* 用双向链表实现LRU算法 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class LRUPageFrame { | ||
|
||
private static int stateAtHead = 0; | ||
private static int stateAtLast = 1; | ||
private static int stateAtMid = 2; | ||
|
||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node(){ | ||
|
||
} | ||
} | ||
|
||
private int capacity; | ||
|
||
private int size; | ||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param key | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
/** | ||
* 空链表 | ||
*/ | ||
size ++; | ||
Node newNode = new Node(); | ||
newNode.pageNum = pageNum; | ||
if(size <= capacity){ | ||
if(last == null){ | ||
last= newNode; | ||
first = newNode; | ||
}else{ | ||
Node preHead = first;//记录原来的头节点 | ||
first = newNode; | ||
first.next = preHead; | ||
preHead.prev = first; | ||
} | ||
}else{ | ||
Node currentNode = first; | ||
int judgeNum = currentNode.pageNum; | ||
Node preHead = first;//记录原来的头节点 | ||
Node preLast = last;//记录原来的尾节点 | ||
int state = -1;//记录当前状态 | ||
/** | ||
* 当前插入的值为等于链表头的位置 | ||
*/ | ||
|
||
int k = 0; | ||
if(judgeNum == pageNum){ | ||
state = stateAtHead; | ||
}else if(preLast.pageNum == pageNum){ | ||
state = stateAtLast; | ||
} | ||
while(currentNode != null && k < capacity - 1){ | ||
k++; | ||
currentNode = currentNode.next; | ||
judgeNum = currentNode.pageNum; | ||
if(judgeNum == pageNum && judgeNum != preLast.pageNum){ | ||
state = stateAtMid; | ||
break; | ||
} | ||
} | ||
|
||
switch (state) { | ||
case -1: | ||
first = newNode; | ||
last = preLast.prev; | ||
first.next = preHead; | ||
first.prev = null; | ||
preHead.next = last; | ||
preHead.prev = first; | ||
last.next = null; | ||
break; | ||
case 0: | ||
|
||
break; | ||
|
||
case 1: | ||
last = preLast.prev; | ||
last.next = null;//原来尾节点的上一个节点变成了尾节点 | ||
first = newNode; | ||
first.prev = null; | ||
first.next = preHead; | ||
break; | ||
case 2: | ||
first = newNode; | ||
first.next = preHead; | ||
first.prev = null; | ||
preHead.next = last; | ||
preHead.prev = first; | ||
last.prev = preHead; | ||
break; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.