From f5742df16b629a3ba55362bb8e40e786142a0e29 Mon Sep 17 00:00:00 2001 From: evernat Date: Tue, 27 Aug 2013 00:03:09 +0200 Subject: [PATCH] Optimize and close Zip resources in ClassicPluginStrategy and FilePath --- .../java/hudson/ClassicPluginStrategy.java | 53 ++++++++++--------- core/src/main/java/hudson/FilePath.java | 7 ++- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/hudson/ClassicPluginStrategy.java b/core/src/main/java/hudson/ClassicPluginStrategy.java index e4d158128c..ae0a948f14 100644 --- a/core/src/main/java/hudson/ClassicPluginStrategy.java +++ b/core/src/main/java/hudson/ClassicPluginStrategy.java @@ -440,7 +440,7 @@ public class ClassicPluginStrategy implements PluginStrategy { * Repackage classes directory into a jar file to make it remoting friendly. * The remoting layer can cache jar files but not class files. */ - private static void createClassJarFromWebInfClasses(File archive, File destDir, Project prj) { + private static void createClassJarFromWebInfClasses(File archive, File destDir, Project prj) throws IOException { File classesJar = new File(destDir, "WEB-INF/lib/classes.jar"); ZipFileSet zfs = new ZipFileSet(); @@ -457,31 +457,36 @@ public class ClassicPluginStrategy implements PluginStrategy { mapper.add(gm); final long dirTime = archive.lastModified(); - Zip z = new Zip() { - /** - * Forces the fixed timestamp for directories to make sure - * classes.jar always get a consistent checksum. - */ - protected void zipDir(Resource dir, ZipOutputStream zOut, String vPath, - int mode, ZipExtraField[] extra) - throws IOException { - - ZipOutputStream wrapped = new ZipOutputStream(new NullOutputStream()) { - @Override - public void putNextEntry(ZipEntry ze) throws IOException { - ze.setTime(dirTime+1999); // roundup - super.putNextEntry(ze); - } - }; - super.zipDir(dir,wrapped,vPath,mode,extra); + // this ZipOutputStream is reused and not created for each directory + final ZipOutputStream wrappedZOut = new ZipOutputStream(new NullOutputStream()) { + @Override + public void putNextEntry(ZipEntry ze) throws IOException { + ze.setTime(dirTime+1999); // roundup + super.putNextEntry(ze); } }; - z.setProject(prj); - z.setTaskType("zip"); - classesJar.getParentFile().mkdirs(); - z.setDestFile(classesJar); - z.add(mapper); - z.execute(); + try { + Zip z = new Zip() { + /** + * Forces the fixed timestamp for directories to make sure + * classes.jar always get a consistent checksum. + */ + protected void zipDir(Resource dir, ZipOutputStream zOut, String vPath, + int mode, ZipExtraField[] extra) + throws IOException { + // use wrappedZOut instead of zOut + super.zipDir(dir,wrappedZOut,vPath,mode,extra); + } + }; + z.setProject(prj); + z.setTaskType("zip"); + classesJar.getParentFile().mkdirs(); + z.setDestFile(classesJar); + z.add(mapper); + z.execute(); + } finally { + wrappedZOut.close(); + } } private static void unzipExceptClasses(File archive, File destDir, Project prj) { diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 7466d9d6af..658fb9ed06 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -527,7 +527,12 @@ public final class FilePath implements Serializable { if (p != null) { p.mkdirs(); } - IOUtils.copy(zip.getInputStream(e), f); + InputStream input = zip.getInputStream(e); + try { + IOUtils.copy(input, f); + } finally { + input.close(); + } try { FilePath target = new FilePath(f); int mode = e.getUnixMode();