From 8aaa005834f2ab61dd187115ca3c922517f2b850 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 21 Oct 2008 16:04:52 +0000 Subject: [PATCH] close streams passed in to zipFile(InputStream, ...). PR 42632. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@706674 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Jar.java | 43 ++++++++++++++----- .../org/apache/tools/ant/taskdefs/Zip.java | 3 +- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index 92fa9213a..d63b24844 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -431,11 +431,21 @@ public class Jar extends Zip { serviceIterator = serviceList.iterator(); while (serviceIterator.hasNext()) { service = (Service) serviceIterator.next(); - //stolen from writeManifest - super.zipFile(service.getAsStream(), zOut, - "META-INF/services/" + service.getType(), - System.currentTimeMillis(), null, - ZipFileSet.DEFAULT_FILE_MODE); + + InputStream is = null; + try { + is = service.getAsStream(); + //stolen from writeManifest + super.zipFile(is, zOut, + "META-INF/services/" + service.getType(), + System.currentTimeMillis(), null, + ZipFileSet.DEFAULT_FILE_MODE); + } finally { + // technically this is unnecessary since + // Service.getAsStream returns a ByteArrayInputStream + // and not closing it wouldn't do any harm. + FileUtils.close(is); + } } } @@ -511,9 +521,14 @@ public class Jar extends Zip { ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - super.zipFile(bais, zOut, MANIFEST_NAME, - System.currentTimeMillis(), null, - ZipFileSet.DEFAULT_FILE_MODE); + try { + super.zipFile(bais, zOut, MANIFEST_NAME, + System.currentTimeMillis(), null, + ZipFileSet.DEFAULT_FILE_MODE); + } finally { + // not really required + FileUtils.close(bais); + } super.initZipOutputStream(zOut); } @@ -592,13 +607,19 @@ public class Jar extends Zip { writer.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - super.zipFile(bais, zOut, INDEX_NAME, System.currentTimeMillis(), null, - ZipFileSet.DEFAULT_FILE_MODE); + try { + super.zipFile(bais, zOut, INDEX_NAME, System.currentTimeMillis(), + null, ZipFileSet.DEFAULT_FILE_MODE); + } finally { + // not really required + FileUtils.close(bais); + } } /** * Overridden from Zip class to deal with manifests and index lists. - * @param is the input stream + * @param in the stream to read data for the entry from. The + * caller of the method is responsible for closing the stream. * @param zOut the zip output stream * @param vPath the name this entry shall have in the archive * @param lastModified last modification time for the entry. diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index 02e49fb9e..eb841b564 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -1465,7 +1465,8 @@ public class Zip extends MatchingTask { /** * Adds a new entry to the archive, takes care of duplicates as well. * - * @param in the stream to read data for the entry from. + * @param in the stream to read data for the entry from. The + * caller of the method is responsible for closing the stream. * @param zOut the stream to write to. * @param vPath the name this entry shall have in the archive. * @param lastModified last modification time for the entry.