diff --git a/src/main/org/apache/tools/ant/taskdefs/GUnzip.java b/src/main/org/apache/tools/ant/taskdefs/GUnzip.java index 7a0f26ce0..9d35744af 100644 --- a/src/main/org/apache/tools/ant/taskdefs/GUnzip.java +++ b/src/main/org/apache/tools/ant/taskdefs/GUnzip.java @@ -82,15 +82,15 @@ public class GUnzip extends Task { public void execute() throws BuildException { if (source == null) { - throw new BuildException("No source specified"); + throw new BuildException("No source for gunzip specified", location); } if (!source.exists()) { - throw new BuildException("source doesn't exist"); + throw new BuildException("source doesn't exist", location); } if (source.isDirectory()) { - throw new BuildException("Cannot expand a directory"); + throw new BuildException("Cannot expand a directory", location); } if (dest == null) { @@ -112,20 +112,31 @@ public class GUnzip extends Task { log("Expanding "+ source.getAbsolutePath() + " to " + dest.getAbsolutePath()); + FileOutputStream out = null; + GZIPInputStream zIn = null; try { - FileOutputStream out = new FileOutputStream(dest); - GZIPInputStream zIn = new GZIPInputStream(new FileInputStream(source)); + out = new FileOutputStream(dest); + zIn = new GZIPInputStream(new FileInputStream(source)); byte[] buffer = new byte[8 * 1024]; int count = 0; do { out.write(buffer, 0, count); count = zIn.read(buffer, 0, buffer.length); } while (count != -1); - zIn.close(); - out.close(); } catch (IOException ioe) { String msg = "Problem expanding gzip " + ioe.getMessage(); - throw new BuildException(msg, ioe); + throw new BuildException(msg, ioe, location); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException ioex) {} + } + if (zIn != null) { + try { + zIn.close(); + } catch (IOException ioex) {} + } } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/GZip.java b/src/main/org/apache/tools/ant/taskdefs/GZip.java index deaed6438..aa7cc14fc 100644 --- a/src/main/org/apache/tools/ant/taskdefs/GZip.java +++ b/src/main/org/apache/tools/ant/taskdefs/GZip.java @@ -83,19 +83,26 @@ public class GZip extends Task { public void execute() throws BuildException { log("Building gzip: " + zipFile.getAbsolutePath()); + GZIPOutputStream zOut = null; try { - GZIPOutputStream zOut = new GZIPOutputStream(new FileOutputStream(zipFile)); + zOut = new GZIPOutputStream(new FileOutputStream(zipFile)); if (source.isDirectory()) { - log ("Cannot Gzip a directory!"); + log ("Cannot Gzip a directory!", Project.MSG_ERR); } else { zipFile(source, zOut); } - // close up - zOut.close(); } catch (IOException ioe) { String msg = "Problem creating gzip " + ioe.getMessage(); - throw new BuildException(msg); + throw new BuildException(msg, ioe, location); + } finally { + if (zOut != null) { + try { + // close up + zOut.close(); + } + catch (IOException e) {} + } } } @@ -114,7 +121,10 @@ public class GZip extends Task { throws IOException { FileInputStream fIn = new FileInputStream(file); - zipFile(fIn, zOut); - fIn.close(); + try { + zipFile(fIn, zOut); + } finally { + fIn.close(); + } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java index 46b82e0ca..7fb213bb1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java @@ -87,18 +87,20 @@ public class Tar extends MatchingTask { log("Building tar: "+ tarFile.getAbsolutePath()); if (baseDir == null) { - throw new BuildException("basedir attribute must be set!"); + throw new BuildException("basedir attribute must be set!", + location); } if (!baseDir.exists()) { - throw new BuildException("basedir does not exist!"); + throw new BuildException("basedir does not exist!", location); } DirectoryScanner ds = super.getDirectoryScanner(baseDir); String[] files = ds.getIncludedFiles(); + TarOutputStream tOut = null; try { - TarOutputStream tOut = new TarOutputStream(new FileOutputStream(tarFile)); + tOut = new TarOutputStream(new FileOutputStream(tarFile)); tOut.setDebug(true); for (int i = 0; i < files.length; i++) { @@ -106,12 +108,17 @@ public class Tar extends MatchingTask { String name = files[i].replace(File.separatorChar,'/'); tarFile(f, tOut, name); } - - // close up - tOut.close(); } catch (IOException ioe) { String msg = "Problem creating TAR: " + ioe.getMessage(); - throw new BuildException(msg); + throw new BuildException(msg, ioe, location); + } finally { + if (tOut != null) { + try { + // close up + tOut.close(); + } + catch (IOException e) {} + } } } @@ -120,20 +127,22 @@ public class Tar extends MatchingTask { { FileInputStream fIn = new FileInputStream(file); - TarEntry te = new TarEntry(vPath); - te.setSize(file.length()); - te.setModTime(file.lastModified()); - tOut.putNextEntry(te); - - byte[] buffer = new byte[8 * 1024]; - int count = 0; - do { - tOut.write(buffer, 0, count); - count = fIn.read(buffer, 0, buffer.length); - } while (count != -1); - - tOut.closeEntry(); - - fIn.close(); + try { + TarEntry te = new TarEntry(vPath); + te.setSize(file.length()); + te.setModTime(file.lastModified()); + tOut.putNextEntry(te); + + byte[] buffer = new byte[8 * 1024]; + int count = 0; + do { + tOut.write(buffer, 0, count); + count = fIn.read(buffer, 0, buffer.length); + } while (count != -1); + + tOut.closeEntry(); + } finally { + fIn.close(); + } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java index 29e75f0aa..6e1e88c5f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Untar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java @@ -79,11 +79,13 @@ public class Untar extends Task { Touch touch = (Touch) project.createTask("touch"); touch.setTarget(target); + File srcF=project.resolveFile(source); + + TarInputStream tis = null; try { if (source == null) { throw new BuildException("No source specified", location); } - File srcF=project.resolveFile(source); if (!srcF.exists()) { throw new BuildException("source "+srcF+" doesn't exist", location); @@ -95,8 +97,7 @@ public class Untar extends Task { File dir=project.resolveFile(dest); log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); - // code from WarExpand - TarInputStream tis = new TarInputStream(new FileInputStream(srcF)); + tis = new TarInputStream(new FileInputStream(srcF)); TarEntry te = null; while ((te = tis.getNextEntry()) != null) { @@ -133,8 +134,16 @@ public class Untar extends Task { } } } catch (IOException ioe) { - throw new BuildException(ioe); - } + throw new BuildException("Error while expanding " + srcF.getPath(), + ioe, location); + } finally { + if (tis != null) { + try { + tis.close(); + } + catch (IOException e) {} + } + } } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index c40847f00..70649c529 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -122,8 +122,9 @@ public class Zip extends MatchingTask { log("Building "+ archiveType +": "+ zipFile.getAbsolutePath()); + ZipOutputStream zOut = null; try { - ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile)); + zOut = new ZipOutputStream(new FileOutputStream(zipFile)); if (doCompress) { zOut.setMethod(ZipOutputStream.DEFLATED); } else { @@ -142,12 +143,17 @@ public class Zip extends MatchingTask { String name = files[i].replace(File.separatorChar,'/'); zipFile(f, zOut, name); } - - // close up - zOut.close(); } catch (IOException ioe) { String msg = "Problem creating " + archiveType + " " + ioe.getMessage(); - throw new BuildException(msg); + throw new BuildException(msg, ioe, location); + } finally { + if (zOut != null) { + try { + // close up + zOut.close(); + } + catch (IOException e) {} + } } } @@ -224,7 +230,10 @@ public class Zip extends MatchingTask { throws IOException { FileInputStream fIn = new FileInputStream(file); - zipFile(fIn, zOut, vPath, file.lastModified()); - fIn.close(); + try { + zipFile(fIn, zOut, vPath, file.lastModified()); + } finally { + fIn.close(); + } } }