1 package com.sun.org.apache.bcel.internal.util; 2 3 56 57 import java.util.*; 58 import java.util.zip.*; 59 import java.io.*; 60 61 68 public class ClassPath { 69 private PathEntry[] paths; 70 71 74 public ClassPath(String class_path) { 75 ArrayList vec = new ArrayList(); 76 77 for(StringTokenizer tok=new StringTokenizer(class_path, 78 java.io.File.pathSeparator); 79 tok.hasMoreTokens();) 80 { 81 String path = tok.nextToken(); 82 83 if(!path.equals("")) { 84 File file = new File(path); 85 86 try { 87 if(file.exists()) { 88 if(file.isDirectory()) 89 vec.add(new Dir(path)); 90 else 91 vec.add(new Zip(new ZipFile(file))); 92 } 93 } catch(IOException e) { 94 System.err.println("CLASSPATH component " + file + ": " + e); 95 } 96 } 97 } 98 99 paths = new PathEntry[vec.size()]; 100 vec.toArray(paths); 101 } 102 103 106 public ClassPath() { 107 this(getClassPath()); 108 } 109 110 private static final void getPathComponents(String path, ArrayList list) { 111 if(path != null) { 112 StringTokenizer tok = new StringTokenizer(path, File.pathSeparator); 113 114 while(tok.hasMoreTokens()) { 115 String name = tok.nextToken(); 116 File file = new File(name); 117 118 if(file.exists()) 119 list.add(name); 120 } 121 } 122 } 123 124 private static final String getClassPath() { 125 String class_path = System.getProperty("java.class.path"); 126 String boot_path = System.getProperty("sun.boot.class.path"); 127 String ext_path = System.getProperty("java.ext.dirs"); 128 129 ArrayList list = new ArrayList(); 130 131 getPathComponents(class_path, list); 132 getPathComponents(boot_path, list); 133 134 ArrayList dirs = new ArrayList(); 135 getPathComponents(ext_path, dirs); 136 137 for(Iterator e = dirs.iterator(); e.hasNext(); ) { 138 File ext_dir = new File((String )e.next()); 139 String [] extensions = ext_dir.list(new FilenameFilter() { 140 public boolean accept(File dir, String name) { 141 name = name.toLowerCase(); 142 return name.endsWith(".zip") || name.endsWith(".jar"); 143 } 144 }); 145 146 if(extensions != null) 147 for(int i=0; i < extensions.length; i++) 148 list.add(ext_path + File.separatorChar + extensions[i]); 149 } 150 151 StringBuffer buf = new StringBuffer (); 152 153 for(Iterator e = list.iterator(); e.hasNext(); ) { 154 buf.append((String )e.next()); 155 156 if(e.hasNext()) 157 buf.append(File.pathSeparatorChar); 158 } 159 160 return buf.toString(); 161 } 162 163 167 public InputStream getInputStream(String name) throws IOException { 168 return getInputStream(name, ".class"); 169 } 170 171 176 public InputStream getInputStream(String name, String suffix) throws IOException { 177 return getClassFile(name, suffix).getInputStream(); 178 } 179 180 185 public ClassFile getClassFile(String name, String suffix) throws IOException { 186 for(int i=0; i < paths.length; i++) { 187 ClassFile cf; 188 189 if((cf = paths[i].getClassFile(name, suffix)) != null) 190 return cf; 191 } 192 193 throw new IOException("Couldn't find: " + name + suffix); 194 } 195 196 200 public ClassFile getClassFile(String name) throws IOException { 201 return getClassFile(name, ".class"); 202 } 203 204 209 public byte[] getBytes(String name, String suffix) throws IOException { 210 InputStream is = getInputStream(name, suffix); 211 212 if(is == null) 213 throw new IOException("Couldn't find: " + name + suffix); 214 215 DataInputStream dis = new DataInputStream(is); 216 byte[] bytes = new byte[is.available()]; 217 dis.readFully(bytes); 218 dis.close(); is.close(); 219 220 return bytes; 221 } 222 223 226 public byte[] getBytes(String name) throws IOException { 227 return getBytes(name, ".class"); 228 } 229 230 234 public String getPath(String name) throws IOException { 235 int index = name.lastIndexOf('.'); 236 String suffix = ""; 237 238 if(index > 0) { 239 suffix = name.substring(index); 240 name = name.substring(0, index); 241 } 242 243 return getPath(name, suffix); 244 } 245 246 251 public String getPath(String name, String suffix) throws IOException { 252 return getClassFile(name, suffix).getPath(); 253 } 254 255 private static abstract class PathEntry { 256 abstract ClassFile getClassFile(String name, String suffix) throws IOException; 257 } 258 259 261 public abstract static class ClassFile { 262 264 public abstract InputStream getInputStream() throws IOException; 265 266 268 public abstract String getPath(); 269 270 272 public abstract long getTime(); 273 274 276 public abstract long getSize(); 277 } 278 279 private static class Dir extends PathEntry { 280 private String dir; 281 282 Dir(String d) { dir = d; } 283 284 ClassFile getClassFile(String name, String suffix) throws IOException { 285 final File file = new File(dir + File.separatorChar + 286 name.replace('.', File.separatorChar) + suffix); 287 288 return file.exists()? new ClassFile() { 289 public InputStream getInputStream() throws IOException { return new FileInputStream(file); } 290 291 public String getPath() { try { 292 return file.getCanonicalPath(); 293 } catch(IOException e) { return null; } 294 295 } 296 public long getTime() { return file.lastModified(); } 297 public long getSize() { return file.length(); } 298 } : null; 299 } 300 301 public String toString() { return dir; } 302 } 303 304 private static class Zip extends PathEntry { 305 private ZipFile zip; 306 307 Zip(ZipFile z) { zip = z; } 308 309 ClassFile getClassFile(String name, String suffix) throws IOException { 310 final ZipEntry entry = zip.getEntry(name.replace('.', '/') + suffix); 311 312 return (entry != null)? new ClassFile() { 313 public InputStream getInputStream() throws IOException { return zip.getInputStream(entry); } 314 public String getPath() { return entry.toString(); } 315 public long getTime() { return entry.getTime(); } 316 public long getSize() { return entry.getSize(); } 317 } : null; 318 } 319 } 320 } 321 322 323 | Popular Tags |