You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassPoolTail.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FilenameFilter;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.net.MalformedURLException;
  25. import java.net.URL;
  26. import java.util.jar.JarEntry;
  27. import java.util.jar.JarFile;
  28. final class ClassPathList {
  29. ClassPathList next;
  30. ClassPath path;
  31. ClassPathList(ClassPath p, ClassPathList n) {
  32. next = n;
  33. path = p;
  34. }
  35. }
  36. final class DirClassPath implements ClassPath {
  37. String directory;
  38. DirClassPath(String dirName) {
  39. directory = dirName;
  40. }
  41. public InputStream openClassfile(String classname) {
  42. try {
  43. char sep = File.separatorChar;
  44. String filename = directory + sep
  45. + classname.replace('.', sep) + ".class";
  46. return new FileInputStream(filename.toString());
  47. }
  48. catch (FileNotFoundException e) {}
  49. catch (SecurityException e) {}
  50. return null;
  51. }
  52. public URL find(String classname) {
  53. char sep = File.separatorChar;
  54. String filename = directory + sep
  55. + classname.replace('.', sep) + ".class";
  56. File f = new File(filename);
  57. if (f.exists())
  58. try {
  59. return f.getCanonicalFile().toURI().toURL();
  60. }
  61. catch (MalformedURLException e) {}
  62. catch (IOException e) {}
  63. return null;
  64. }
  65. public void close() {}
  66. public String toString() {
  67. return directory;
  68. }
  69. }
  70. final class JarDirClassPath implements ClassPath {
  71. JarClassPath[] jars;
  72. JarDirClassPath(String dirName) throws NotFoundException {
  73. File[] files = new File(dirName).listFiles(new FilenameFilter() {
  74. public boolean accept(File dir, String name) {
  75. name = name.toLowerCase();
  76. return name.endsWith(".jar") || name.endsWith(".zip");
  77. }
  78. });
  79. if (files != null) {
  80. jars = new JarClassPath[files.length];
  81. for (int i = 0; i < files.length; i++)
  82. jars[i] = new JarClassPath(files[i].getPath());
  83. }
  84. }
  85. public InputStream openClassfile(String classname) throws NotFoundException {
  86. if (jars != null)
  87. for (int i = 0; i < jars.length; i++) {
  88. InputStream is = jars[i].openClassfile(classname);
  89. if (is != null)
  90. return is;
  91. }
  92. return null; // not found
  93. }
  94. public URL find(String classname) {
  95. if (jars != null)
  96. for (int i = 0; i < jars.length; i++) {
  97. URL url = jars[i].find(classname);
  98. if (url != null)
  99. return url;
  100. }
  101. return null; // not found
  102. }
  103. public void close() {
  104. if (jars != null)
  105. for (int i = 0; i < jars.length; i++)
  106. jars[i].close();
  107. }
  108. }
  109. final class JarClassPath implements ClassPath {
  110. JarFile jarfile;
  111. String jarfileURL;
  112. JarClassPath(String pathname) throws NotFoundException {
  113. try {
  114. jarfile = new JarFile(pathname);
  115. jarfileURL = new File(pathname).getCanonicalFile()
  116. .toURI().toURL().toString();
  117. return;
  118. }
  119. catch (IOException e) {}
  120. throw new NotFoundException(pathname);
  121. }
  122. public InputStream openClassfile(String classname)
  123. throws NotFoundException
  124. {
  125. try {
  126. String jarname = classname.replace('.', '/') + ".class";
  127. JarEntry je = jarfile.getJarEntry(jarname);
  128. if (je != null)
  129. return jarfile.getInputStream(je);
  130. else
  131. return null; // not found
  132. }
  133. catch (IOException e) {}
  134. throw new NotFoundException("broken jar file?: "
  135. + jarfile.getName());
  136. }
  137. public URL find(String classname) {
  138. String jarname = classname.replace('.', '/') + ".class";
  139. JarEntry je = jarfile.getJarEntry(jarname);
  140. if (je != null)
  141. try {
  142. return new URL("jar:" + jarfileURL + "!/" + jarname);
  143. }
  144. catch (MalformedURLException e) {}
  145. return null; // not found
  146. }
  147. public void close() {
  148. try {
  149. jarfile.close();
  150. jarfile = null;
  151. }
  152. catch (IOException e) {}
  153. }
  154. public String toString() {
  155. return jarfile == null ? "<null>" : jarfile.toString();
  156. }
  157. }
  158. final class ClassPoolTail {
  159. protected ClassPathList pathList;
  160. public ClassPoolTail() {
  161. pathList = null;
  162. }
  163. public String toString() {
  164. StringBuffer buf = new StringBuffer();
  165. buf.append("[class path: ");
  166. ClassPathList list = pathList;
  167. while (list != null) {
  168. buf.append(list.path.toString());
  169. buf.append(File.pathSeparatorChar);
  170. list = list.next;
  171. }
  172. buf.append(']');
  173. return buf.toString();
  174. }
  175. public synchronized ClassPath insertClassPath(ClassPath cp) {
  176. pathList = new ClassPathList(cp, pathList);
  177. return cp;
  178. }
  179. public synchronized ClassPath appendClassPath(ClassPath cp) {
  180. ClassPathList tail = new ClassPathList(cp, null);
  181. ClassPathList list = pathList;
  182. if (list == null)
  183. pathList = tail;
  184. else {
  185. while (list.next != null)
  186. list = list.next;
  187. list.next = tail;
  188. }
  189. return cp;
  190. }
  191. public synchronized void removeClassPath(ClassPath cp) {
  192. ClassPathList list = pathList;
  193. if (list != null)
  194. if (list.path == cp)
  195. pathList = list.next;
  196. else {
  197. while (list.next != null)
  198. if (list.next.path == cp)
  199. list.next = list.next.next;
  200. else
  201. list = list.next;
  202. }
  203. cp.close();
  204. }
  205. public ClassPath appendSystemPath() {
  206. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  207. return appendClassPath(new LoaderClassPath(cl));
  208. }
  209. public ClassPath insertClassPath(String pathname)
  210. throws NotFoundException
  211. {
  212. return insertClassPath(makePathObject(pathname));
  213. }
  214. public ClassPath appendClassPath(String pathname)
  215. throws NotFoundException
  216. {
  217. return appendClassPath(makePathObject(pathname));
  218. }
  219. private static ClassPath makePathObject(String pathname)
  220. throws NotFoundException
  221. {
  222. String lower = pathname.toLowerCase();
  223. if (lower.endsWith(".jar") || lower.endsWith(".zip"))
  224. return new JarClassPath(pathname);
  225. int len = pathname.length();
  226. if (len > 2 && pathname.charAt(len - 1) == '*'
  227. && (pathname.charAt(len - 2) == '/'
  228. || pathname.charAt(len - 2) == File.separatorChar)) {
  229. String dir = pathname.substring(0, len - 2);
  230. return new JarDirClassPath(dir);
  231. }
  232. return new DirClassPath(pathname);
  233. }
  234. /**
  235. * This method does not close the output stream.
  236. */
  237. void writeClassfile(String classname, OutputStream out)
  238. throws NotFoundException, IOException, CannotCompileException
  239. {
  240. InputStream fin = openClassfile(classname);
  241. if (fin == null)
  242. throw new NotFoundException(classname);
  243. try {
  244. copyStream(fin, out);
  245. }
  246. finally {
  247. fin.close();
  248. }
  249. }
  250. /*
  251. -- faster version --
  252. void checkClassName(String classname) throws NotFoundException {
  253. if (find(classname) == null)
  254. throw new NotFoundException(classname);
  255. }
  256. -- slower version --
  257. void checkClassName(String classname) throws NotFoundException {
  258. InputStream fin = openClassfile(classname);
  259. try {
  260. fin.close();
  261. }
  262. catch (IOException e) {}
  263. }
  264. */
  265. /**
  266. * Opens the class file for the class specified by
  267. * <code>classname</code>.
  268. *
  269. * @param classname a fully-qualified class name
  270. * @return null if the file has not been found.
  271. * @throws NotFoundException if any error is reported by ClassPath.
  272. */
  273. InputStream openClassfile(String classname)
  274. throws NotFoundException
  275. {
  276. ClassPathList list = pathList;
  277. InputStream ins = null;
  278. NotFoundException error = null;
  279. while (list != null) {
  280. try {
  281. ins = list.path.openClassfile(classname);
  282. }
  283. catch (NotFoundException e) {
  284. if (error == null)
  285. error = e;
  286. }
  287. if (ins == null)
  288. list = list.next;
  289. else
  290. return ins;
  291. }
  292. if (error != null)
  293. throw error;
  294. else
  295. return null; // not found
  296. }
  297. /**
  298. * Searches the class path to obtain the URL of the class file
  299. * specified by classname. It is also used to determine whether
  300. * the class file exists.
  301. *
  302. * @param classname a fully-qualified class name.
  303. * @return null if the class file could not be found.
  304. */
  305. public URL find(String classname) {
  306. ClassPathList list = pathList;
  307. URL url = null;
  308. while (list != null) {
  309. url = list.path.find(classname);
  310. if (url == null)
  311. list = list.next;
  312. else
  313. return url;
  314. }
  315. return null;
  316. }
  317. /**
  318. * Reads from an input stream until it reaches the end.
  319. *
  320. * @return the contents of that input stream
  321. */
  322. public static byte[] readStream(InputStream fin) throws IOException {
  323. byte[][] bufs = new byte[8][];
  324. int bufsize = 4096;
  325. for (int i = 0; i < 8; ++i) {
  326. bufs[i] = new byte[bufsize];
  327. int size = 0;
  328. int len = 0;
  329. do {
  330. len = fin.read(bufs[i], size, bufsize - size);
  331. if (len >= 0)
  332. size += len;
  333. else {
  334. byte[] result = new byte[bufsize - 4096 + size];
  335. int s = 0;
  336. for (int j = 0; j < i; ++j) {
  337. System.arraycopy(bufs[j], 0, result, s, s + 4096);
  338. s = s + s + 4096;
  339. }
  340. System.arraycopy(bufs[i], 0, result, s, size);
  341. return result;
  342. }
  343. } while (size < bufsize);
  344. bufsize *= 2;
  345. }
  346. throw new IOException("too much data");
  347. }
  348. /**
  349. * Reads from an input stream and write to an output stream
  350. * until it reaches the end. This method does not close the
  351. * streams.
  352. */
  353. public static void copyStream(InputStream fin, OutputStream fout)
  354. throws IOException
  355. {
  356. int bufsize = 4096;
  357. byte[] buf = null;
  358. for (int i = 0; i < 64; ++i) {
  359. if (i < 8) {
  360. bufsize *= 2;
  361. buf = new byte[bufsize];
  362. }
  363. int size = 0;
  364. int len = 0;
  365. do {
  366. len = fin.read(buf, size, bufsize - size);
  367. if (len >= 0)
  368. size += len;
  369. else {
  370. fout.write(buf, 0, size);
  371. return;
  372. }
  373. } while (size < bufsize);
  374. fout.write(buf);
  375. }
  376. throw new IOException("too much data");
  377. }
  378. }