Skip to content

Commit

Permalink
Merge pull request onlyliuxin#61 from Ren650119726/master
Browse files Browse the repository at this point in the history
 102228177 第五次作业
  • Loading branch information
luoziyihao authored Apr 20, 2017
2 parents 0b85735 + 5066bfd commit 2a01f09
Show file tree
Hide file tree
Showing 76 changed files with 3,888 additions and 54 deletions.
16 changes: 15 additions & 1 deletion group01/275150374/275150374Learning/.idea/description.html

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion group15/1512_656512403/.idea/description.html

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions group17/102228177/work2_26/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/dom4j-1.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
19 changes: 10 additions & 9 deletions group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {
// System.out.println(Arrays.toString(util.merge(a1, a2)));
System.out.println(Arrays.toString(util.fibonacci(15)));

System.out.println(Arrays.toString(util.getPrimes(23)));
// System.out.println(Arrays.toString(util.getPrimes(23)));
}

/**
Expand Down Expand Up @@ -115,14 +115,15 @@ public int[] grow(int [] oldArray, int size){
*/
public int[] fibonacci(int max){
List<Integer> list = new ArrayList<Integer>();
int f1 = 1, f2 = 1, f = 0;
list.add(f1);
list.add(f2);
while(f < max){
f = f1+f2;
f1 = f2;
f2 = f;
list.add(f);
if (max <= 1) {
return new int[]{};
}
int lo = 0;
int hi = 1;
while(hi<max){
list.add(hi);
hi = lo + hi;
lo = hi - lo;
}
int[] arr = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.coderising.jvm.loader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;



public class ClassFileLoader {

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

public byte[] readBinaryCode(String className) {
String name = "";
for (int i = 0; i < className.length(); i++) {
if(className.charAt(i)=='.'){
name += File.separatorChar;
}else{
name += className.charAt(i);
}
}
File file = new File(getClassPath()+ File.separatorChar +name+".class");
InputStream in = null;
ByteArrayOutputStream out = null;
try {
in = new FileInputStream(file);
out = new ByteArrayOutputStream();
byte[] buff = new byte[1024*2];
int len = 0;
while((len=in.read(buff))!=-1){
out.write(buff, 0, len);
}
return out.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return null;


}


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



public String getClassPath(){
StringBuilder sb = new StringBuilder();
for (String string : clzPaths) {
sb.append(string).append(";");
}
sb = sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin";
static String path1 = "D:\\git\\coding2017\\group17\\102228177\\work3_26\\bin";
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();

}
}
Loading

0 comments on commit 2a01f09

Please sign in to comment.