您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ClassPoolTail.java 12KB

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