From b839a3ed0f3a0ad050579c33f2ede3e8b8542fea Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Wed, 18 Jul 2001 02:29:34 +0000 Subject: [PATCH] Improve the performance of the classloader - The classloader will, however, now have the jars open. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269352 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/AntClassLoader.java | 51 ++++++++++--------- .../tools/ant/taskdefs/ExecuteJava.java | 1 - 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java index 3aa8e9e04..5aace35b9 100644 --- a/src/main/org/apache/tools/ant/AntClassLoader.java +++ b/src/main/org/apache/tools/ant/AntClassLoader.java @@ -163,7 +163,7 @@ public class AntClassLoader extends ClassLoader implements BuildListener { /** * The size of buffers to be used in this classloader. */ - static private final int BUFFER_SIZE = 1024; + static private final int BUFFER_SIZE = 8192; /** * The components of the classpath that the classloader searches for classes @@ -204,6 +204,11 @@ public class AntClassLoader extends ClassLoader implements BuildListener { */ private ClassLoader parent = null; + /** + * A hashtable of zip files opened by the classloader + */ + private Hashtable zipFiles = new Hashtable(); + /** * The context loader saved when setting the thread's current context loader. */ @@ -579,28 +584,15 @@ public class AntClassLoader extends ClassLoader implements BuildListener { } } else { - ZipFile zipFile = null; - try { + // is the zip file in the cache + ZipFile zipFile = (ZipFile)zipFiles.get(file); + if (zipFile == null) { zipFile = new ZipFile(file); - - ZipEntry entry = zipFile.getEntry(resourceName); - if (entry != null) { - // we need to read the entry out of the zip file into - // a baos and then - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead; - InputStream stream = zipFile.getInputStream(entry); - while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) { - baos.write(buffer, 0, bytesRead); - } - return new ByteArrayInputStream(baos.toByteArray()); - } + zipFiles.put(file, zipFile); } - finally { - if (zipFile != null) { - zipFile.close(); - } + ZipEntry entry = zipFile.getEntry(resourceName); + if (entry != null) { + return zipFile.getInputStream(entry); } } } @@ -788,7 +780,7 @@ public class AntClassLoader extends ClassLoader implements BuildListener { if (theClass != null) { return theClass; } - + if (isParentFirst(classname)) { try { theClass = findBaseClass(classname); @@ -816,7 +808,7 @@ public class AntClassLoader extends ClassLoader implements BuildListener { if (resolve) { resolveClass(theClass); } - + return theClass; } @@ -847,9 +839,9 @@ public class AntClassLoader extends ClassLoader implements BuildListener { throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead = -1; - byte[] buffer = new byte[1024]; + byte[] buffer = new byte[BUFFER_SIZE]; - while ((bytesRead = stream.read(buffer, 0, 1024)) != -1) { + while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) { baos.write(buffer, 0, bytesRead); } @@ -951,6 +943,15 @@ public class AntClassLoader extends ClassLoader implements BuildListener { public void buildFinished(BuildEvent event) { pathComponents = null; project = null; + for (Enumeration e = zipFiles.elements(); e.hasMoreElements(); ) { + ZipFile zipFile = (ZipFile)e.nextElement(); + try { + zipFile.close(); + } + catch (IOException ioe) { + // ignore + } + } } public void targetStarted(BuildEvent event) { diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java index 39854316a..629f64134 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java @@ -129,7 +129,6 @@ public class ExecuteJava { } final Method main = target.getMethod("main", param); main.invoke(null, argument); - } catch (NullPointerException e) { throw new BuildException("Could not find main() method in " + classname); } catch (ClassNotFoundException e) {