Skip to content

Commit

Permalink
Merge pull request #3 from guodongym/master
Browse files Browse the repository at this point in the history
download the news
  • Loading branch information
maishihang authored Apr 18, 2017
2 parents e17c53f + 218a213 commit 311d8bf
Show file tree
Hide file tree
Showing 510 changed files with 26,351 additions and 2,501 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

public class ConnectionException extends Exception {

/**
* 自定义异常错误
* @param string
*/
public ConnectionException(String string) {

}

}
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();
}
2 changes: 1 addition & 1 deletion group04/1020483199/1020483199Learning/.classpath
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>
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();
}





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

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

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

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

}
Loading

0 comments on commit 311d8bf

Please sign in to comment.