From 06831e48d6f8b210baa3ffdfeabf2be173d7188c Mon Sep 17 00:00:00 2001 From: Imgaojp Date: Fri, 17 Feb 2017 14:36:12 +0800 Subject: [PATCH] implement Entries --- src/com/gabongao/jvm/Cmd.java | 21 +++-- src/com/gabongao/jvm/classpath/Entry.java | 33 +++++++- .../jvm/classpath/Entry_Composite.java | 29 +++++++ src/com/gabongao/jvm/classpath/Entry_Dir.java | 80 +++++++++++++++++++ src/com/gabongao/jvm/classpath/Entry_Zip.java | 80 +++++++++++++++++++ src/test/com/gabongao/jvm/CmdTest.java | 6 +- 6 files changed, 239 insertions(+), 10 deletions(-) create mode 100644 src/com/gabongao/jvm/classpath/Entry_Composite.java create mode 100644 src/com/gabongao/jvm/classpath/Entry_Dir.java create mode 100644 src/com/gabongao/jvm/classpath/Entry_Zip.java diff --git a/src/com/gabongao/jvm/Cmd.java b/src/com/gabongao/jvm/Cmd.java index 7d5e485..24c9179 100644 --- a/src/com/gabongao/jvm/Cmd.java +++ b/src/com/gabongao/jvm/Cmd.java @@ -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) { diff --git a/src/com/gabongao/jvm/classpath/Entry.java b/src/com/gabongao/jvm/classpath/Entry.java index 3284406..e39d24c 100644 --- a/src/com/gabongao/jvm/classpath/Entry.java +++ b/src/com/gabongao/jvm/classpath/Entry.java @@ -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(); + } diff --git a/src/com/gabongao/jvm/classpath/Entry_Composite.java b/src/com/gabongao/jvm/classpath/Entry_Composite.java new file mode 100644 index 0000000..5357cbb --- /dev/null +++ b/src/com/gabongao/jvm/classpath/Entry_Composite.java @@ -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 { + +} diff --git a/src/com/gabongao/jvm/classpath/Entry_Dir.java b/src/com/gabongao/jvm/classpath/Entry_Dir.java new file mode 100644 index 0000000..3ccb43a --- /dev/null +++ b/src/com/gabongao/jvm/classpath/Entry_Dir.java @@ -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; + } +} diff --git a/src/com/gabongao/jvm/classpath/Entry_Zip.java b/src/com/gabongao/jvm/classpath/Entry_Zip.java new file mode 100644 index 0000000..487d854 --- /dev/null +++ b/src/com/gabongao/jvm/classpath/Entry_Zip.java @@ -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; + } +} diff --git a/src/test/com/gabongao/jvm/CmdTest.java b/src/test/com/gabongao/jvm/CmdTest.java index a56398e..37c4f11 100644 --- a/src/test/com/gabongao/jvm/CmdTest.java +++ b/src/test/com/gabongao/jvm/CmdTest.java @@ -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();