implement Entries
This commit is contained in:
parent
6be17bb41d
commit
06831e48d6
@ -32,6 +32,7 @@ public class Cmd {
|
||||
boolean versionFlag = false;
|
||||
String cpOption = "";
|
||||
String className = "";
|
||||
String Xjre = "";
|
||||
String[] args;
|
||||
|
||||
/**
|
||||
@ -59,6 +60,10 @@ public class Cmd {
|
||||
return args;
|
||||
}
|
||||
|
||||
public String getXjre() {
|
||||
return Xjre;
|
||||
}
|
||||
|
||||
/**
|
||||
* reset properties
|
||||
*/
|
||||
@ -68,6 +73,7 @@ public class Cmd {
|
||||
this.cpOption = "";
|
||||
this.className = "";
|
||||
this.args = null;
|
||||
this.Xjre = "";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,6 +88,7 @@ public class Cmd {
|
||||
options.addOption("version", false, "print version and exit");
|
||||
options.addOption("?", "help", false, "print help message");
|
||||
options.addOption("cp", "classpath", true, "classpath");
|
||||
options.addOption("Xjre", true, "path to jre");
|
||||
CommandLine commandLine = parser.parse(options, args);
|
||||
if (commandLine.hasOption("help") || commandLine.hasOption("?")) {
|
||||
this.helpFlag = true;
|
||||
@ -90,12 +97,14 @@ public class Cmd {
|
||||
this.versionFlag = true;
|
||||
}
|
||||
|
||||
if (commandLine.hasOption("cp") || commandLine.hasOption("classpath")) {
|
||||
if (commandLine.hasOption("cp")) {
|
||||
this.cpOption = commandLine.getOptionValue("cp");
|
||||
} else {
|
||||
this.cpOption = commandLine.getOptionValue("classpath");
|
||||
}
|
||||
if (commandLine.hasOption("cp")) {
|
||||
this.cpOption = commandLine.getOptionValue("cp");
|
||||
} else if (commandLine.hasOption("classpath")) {
|
||||
this.cpOption = commandLine.getOptionValue("classpath");
|
||||
}
|
||||
|
||||
if (commandLine.hasOption("Xjre")) {
|
||||
this.Xjre = commandLine.getOptionValue("Xjre");
|
||||
}
|
||||
String[] commandLineArgs = commandLine.getArgs();
|
||||
if (commandLineArgs.length > 0) {
|
||||
|
||||
@ -24,6 +24,35 @@ package com.gabongao.jvm.classpath;
|
||||
* ┗┻┛ ┗┻┛+ + + +
|
||||
* Created by Imgaojp on 2017/2/16.
|
||||
*/
|
||||
public interface Entry {
|
||||
// public
|
||||
public abstract class Entry {
|
||||
static String pathListSeparator = System.getProperty("path.separator");//system path separator
|
||||
|
||||
// public Entry newEntry(String path) {
|
||||
// if (path.contains(pathListSeparator)) {
|
||||
// return newCompositeEntry(path);
|
||||
// }
|
||||
// if (path.contains("*")) {
|
||||
// return newWildcardEntry(path);
|
||||
// }
|
||||
// if (path.contains(".jar") || path.contains(".zip") || path.contains(".JAR") || path.contains(".ZIP")) {
|
||||
// return newZipEntry(path);
|
||||
// }
|
||||
// return newDirEntry(path);
|
||||
// }
|
||||
|
||||
/**
|
||||
* read class from file
|
||||
*
|
||||
* @param className
|
||||
* @return
|
||||
*/
|
||||
public abstract byte[] readClass(String className);
|
||||
|
||||
/**
|
||||
* to string
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String toString();
|
||||
|
||||
}
|
||||
|
||||
29
src/com/gabongao/jvm/classpath/Entry_Composite.java
Normal file
29
src/com/gabongao/jvm/classpath/Entry_Composite.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.gabongao.jvm.classpath;
|
||||
|
||||
/**
|
||||
* ┏┓ ┏┓+ +
|
||||
* ┏┛┻━━━┛┻┓ + +
|
||||
* ┃ ┃
|
||||
* ┃ ━ ┃ ++ + + +
|
||||
* ████━████ ┃+
|
||||
* ┃ ┃ +
|
||||
* ┃ ┻ ┃
|
||||
* ┃ ┃ + +
|
||||
* ┗━┓ ┏━┛
|
||||
* ┃ ┃
|
||||
* ┃ ┃ + + + +
|
||||
* ┃ ┃ Code is far away from bug with the animal protecting
|
||||
* ┃ ┃ + 神兽保佑,代码无bug
|
||||
* ┃ ┃
|
||||
* ┃ ┃ +
|
||||
* ┃ ┗━━━┓ + +
|
||||
* ┃ ┣┓
|
||||
* ┃ ┏┛
|
||||
* ┗┓┓┏━┳┓┏┛ + + + +
|
||||
* ┃┫┫ ┃┫┫
|
||||
* ┗┻┛ ┗┻┛+ + + +
|
||||
* Created by Imgaojp on 2017/2/17.
|
||||
*/
|
||||
public class Entry_Composite extends Entry {
|
||||
|
||||
}
|
||||
80
src/com/gabongao/jvm/classpath/Entry_Dir.java
Normal file
80
src/com/gabongao/jvm/classpath/Entry_Dir.java
Normal file
@ -0,0 +1,80 @@
|
||||
package com.gabongao.jvm.classpath;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* ┏┓ ┏┓+ +
|
||||
* ┏┛┻━━━┛┻┓ + +
|
||||
* ┃ ┃
|
||||
* ┃ ━ ┃ ++ + + +
|
||||
* ████━████ ┃+
|
||||
* ┃ ┃ +
|
||||
* ┃ ┻ ┃
|
||||
* ┃ ┃ + +
|
||||
* ┗━┓ ┏━┛
|
||||
* ┃ ┃
|
||||
* ┃ ┃ + + + +
|
||||
* ┃ ┃ Code is far away from bug with the animal protecting
|
||||
* ┃ ┃ + 神兽保佑,代码无bug
|
||||
* ┃ ┃
|
||||
* ┃ ┃ +
|
||||
* ┃ ┗━━━┓ + +
|
||||
* ┃ ┣┓
|
||||
* ┃ ┏┛
|
||||
* ┗┓┓┏━┳┓┏┛ + + + +
|
||||
* ┃┫┫ ┃┫┫
|
||||
* ┗┻┛ ┗┻┛+ + + +
|
||||
* Created by Imgaojp on 2017/2/17.
|
||||
*/
|
||||
public class Entry_Dir extends Entry {
|
||||
private String absDir;
|
||||
|
||||
public Entry_Dir(String path) {
|
||||
File file = new File(path);
|
||||
absDir = file.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* read class in hex
|
||||
* @param className
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public byte[] readClass(String className) {
|
||||
Path path = Paths.get(absDir, className);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) path.toFile().length());
|
||||
BufferedInputStream inputStream=null;
|
||||
try {
|
||||
inputStream = new BufferedInputStream(new FileInputStream(path.toFile()));
|
||||
int buf_size = 1024;
|
||||
byte[] buffer = new byte[buf_size];
|
||||
int len = 0;
|
||||
while (-1 != (len=inputStream.read(buffer, 0, buf_size))) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
for (byte b:bos.toByteArray()
|
||||
) {
|
||||
System.out.printf("%x",b);
|
||||
}
|
||||
return bos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
inputStream.close();
|
||||
bos.close();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.absDir;
|
||||
}
|
||||
}
|
||||
80
src/com/gabongao/jvm/classpath/Entry_Zip.java
Normal file
80
src/com/gabongao/jvm/classpath/Entry_Zip.java
Normal file
@ -0,0 +1,80 @@
|
||||
package com.gabongao.jvm.classpath;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.JarInputStream;
|
||||
|
||||
/**
|
||||
* ┏┓ ┏┓+ +
|
||||
* ┏┛┻━━━┛┻┓ + +
|
||||
* ┃ ┃
|
||||
* ┃ ━ ┃ ++ + + +
|
||||
* ████━████ ┃+
|
||||
* ┃ ┃ +
|
||||
* ┃ ┻ ┃
|
||||
* ┃ ┃ + +
|
||||
* ┗━┓ ┏━┛
|
||||
* ┃ ┃
|
||||
* ┃ ┃ + + + +
|
||||
* ┃ ┃ Code is far away from bug with the animal protecting
|
||||
* ┃ ┃ + 神兽保佑,代码无bug
|
||||
* ┃ ┃
|
||||
* ┃ ┃ +
|
||||
* ┃ ┗━━━┓ + +
|
||||
* ┃ ┣┓
|
||||
* ┃ ┏┛
|
||||
* ┗┓┓┏━┳┓┏┛ + + + +
|
||||
* ┃┫┫ ┃┫┫
|
||||
* ┗┻┛ ┗┻┛+ + + +
|
||||
* Created by Imgaojp on 2017/2/17.
|
||||
*/
|
||||
public class Entry_Zip extends Entry {
|
||||
String absPath;
|
||||
|
||||
public Entry_Zip(String path) {
|
||||
File file = new File(path);
|
||||
absPath = file.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* use JarEntry,JarInputStream read "class" file in "zip" or "jar" file
|
||||
* @param className
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public byte[] readClass(String className) {
|
||||
try {
|
||||
InputStream in = new BufferedInputStream(new FileInputStream(new File(absPath)));
|
||||
JarInputStream jarInput = new JarInputStream(in);
|
||||
JarEntry entry = jarInput.getNextJarEntry();
|
||||
while (entry != null) {
|
||||
if (className.equals(entry.getName())) {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
int buf_size=1024;
|
||||
byte[] buffer = new byte[buf_size];
|
||||
int len=0;
|
||||
while (-1 != (len = jarInput.read(buffer, 0, buf_size))) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
for (byte b : bos.toByteArray()
|
||||
) {
|
||||
System.out.printf("%x",b);
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
entry = jarInput.getNextJarEntry();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return absPath;
|
||||
}
|
||||
}
|
||||
@ -82,6 +82,7 @@ public void testParseCmd() throws Exception {
|
||||
|
||||
|
||||
cmd.parseCmd(new String[]{"-classpath","classpath1","-version","MyApp","args1","args2"});
|
||||
assertEquals(true,"".equals(cmd.getXjre()));
|
||||
assertEquals("classpath1",cmd.getCpOption());
|
||||
assertEquals(false,cmd.isHelpFlag());
|
||||
assertEquals(true,cmd.isVersionFlag());
|
||||
@ -89,11 +90,12 @@ public void testParseCmd() throws Exception {
|
||||
assertEquals("MyApp",cmd.getClassName());
|
||||
cmd.reset();
|
||||
|
||||
cmd.parseCmd(new String[]{"-classpath","classpath1","MyApp","-version","args1","args2"});
|
||||
cmd.parseCmd(new String[]{"-classpath","classpath1","MyApp","-version","args1","args2","-Xjre","jreoption","args3"});
|
||||
assertEquals("classpath1",cmd.getCpOption());
|
||||
assertEquals(false,cmd.isHelpFlag());
|
||||
assertEquals(true,cmd.isVersionFlag());
|
||||
assertEquals(true, Arrays.asList(new String[]{"args1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
|
||||
assertEquals(true,"jreoption".equals(cmd.getXjre()));
|
||||
assertEquals(true, Arrays.asList(new String[]{"args1", "args2","args3"}).equals(Arrays.asList(cmd.getArgs())));
|
||||
assertEquals("MyApp",cmd.getClassName());
|
||||
cmd.reset();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user