1
3 package jminusminus;
4
5 import java.io.*;
6 import java.util.ArrayList;
7 import java.util.StringTokenizer;
8 import java.util.zip.ZipEntry;
9 import java.util.zip.ZipFile;
10
11
15 class CLPath {
16 private ArrayList<String> dirs;
18
19
25 private ArrayList<String> loadClassPath(String classPath) {
26 ArrayList<String> container = new ArrayList<String>();
27
28 StringTokenizer entries = new StringTokenizer(classPath, File.pathSeparator);
30 while (entries.hasMoreTokens()) {
31 container.add(entries.nextToken());
32 }
33
34 if (System.getProperty("sun.boot.class.path") != null) {
36 entries = new StringTokenizer(System.getProperty("sun.boot.class.path"),
37 File.pathSeparator);
38 while (entries.hasMoreTokens()) {
39 container.add(entries.nextToken());
40 }
41 } else {
42 String dir = System.getProperty("java.home") + File.separatorChar + "lib" +
43 File.separatorChar + "rt.jar";
44 container.add(dir);
45 }
46 return container;
47 }
48
49
52 public CLPath() {
53 this(null, null);
54 }
55
56
63 public CLPath(String path, String extdir) {
64 if (path == null) {
65 path = System.getProperty("java.class.path");
67 }
68 if (path == null) {
69 path = ".";
71 }
72 dirs = loadClassPath(path);
73 if (extdir == null) {
74 extdir = System.getProperty("java.ext.dirs");
76 }
77 if (extdir != null) {
78 File extDirectory = new File(extdir);
79 if (extDirectory.isDirectory()) {
80 File[] extFiles = extDirectory.listFiles();
81 for (File file : extFiles) {
82 if (file.isFile() &&
83 (file.getName().endsWith(".zip") || file.getName().endsWith(".jar"))) {
84 dirs.add(file.getName());
85 } else {
86 }
88 }
89 }
90 }
91 }
92
93
101 public CLInputStream loadClass(String name) {
102 CLInputStream reader = null;
103 for (int i = 0; i < dirs.size(); i++) {
104 String dir = dirs.get(i);
105 File file = new File(dir);
106 if (file.isDirectory()) {
107 File theClass = new File(dir, name.replace('/', File.separatorChar) + ".class");
108 if (theClass.canRead()) {
109 try {
110 reader = new CLInputStream(new BufferedInputStream(new
111 FileInputStream(theClass)));
112 } catch (FileNotFoundException e) {
113 }
115 }
116 } else if (file.isFile()) {
117 try {
118 ZipFile zip = new ZipFile(dir);
119 ZipEntry entry = zip.getEntry(name + ".class");
120 if (entry != null) {
121 reader = new CLInputStream(zip.getInputStream(entry));
122 }
123 } catch (IOException e) {
124 }
126 } else {
127 }
129 }
130 return reader;
131 }
132 }
133
134
138 class CLInputStream extends DataInputStream {
139
144 public CLInputStream(InputStream in) {
145 super(in);
146 }
147
148
163 public long readUnsignedInt() throws IOException {
164 byte[] b = new byte[4];
165 long mask = 0xFF, l;
166 in.read(b);
167 l = ((b[0] & mask) << 24) | ((b[1] & mask) << 16) | ((b[2] & mask) << 8) | (b[3] & mask);
168 return l;
169 }
170 }
171