You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@locus.apache.org on 2000/07/10 09:29:27 UTC
cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs GUnzip.java GZip.java Tar.java Untar.java Zip.java
bodewig 00/07/10 00:29:27
Modified: src/main/org/apache/tools/ant/taskdefs GUnzip.java GZip.java
Tar.java Untar.java Zip.java
Log:
Take more care with regard to open files/streams.
Revision Changes Path
1.3 +19 -8 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/GUnzip.java
Index: GUnzip.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/GUnzip.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GUnzip.java 2000/07/06 16:48:15 1.2
+++ GUnzip.java 2000/07/10 07:29:26 1.3
@@ -82,15 +82,15 @@
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 @@
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) {}
+ }
}
}
}
1.4 +17 -7 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/GZip.java
Index: GZip.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/GZip.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- GZip.java 2000/07/06 16:48:15 1.3
+++ GZip.java 2000/07/10 07:29:26 1.4
@@ -83,19 +83,26 @@
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 @@
throws IOException
{
FileInputStream fIn = new FileInputStream(file);
- zipFile(fIn, zOut);
- fIn.close();
+ try {
+ zipFile(fIn, zOut);
+ } finally {
+ fIn.close();
+ }
}
}
1.4 +31 -22 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tar.java
Index: Tar.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tar.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Tar.java 2000/07/06 16:48:19 1.3
+++ Tar.java 2000/07/10 07:29:26 1.4
@@ -87,18 +87,20 @@
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 @@
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 @@
{
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();
+ }
}
}
1.6 +14 -5 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java
Index: Untar.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Untar.java 2000/07/06 16:48:19 1.5
+++ Untar.java 2000/07/10 07:29:26 1.6
@@ -79,11 +79,13 @@
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 @@
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 @@
}
}
} 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) {}
+ }
+ }
}
/**
1.9 +16 -7 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java
Index: Zip.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Zip.java 2000/07/06 16:48:19 1.8
+++ Zip.java 2000/07/10 07:29:26 1.9
@@ -122,8 +122,9 @@
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 @@
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 @@
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();
+ }
}
}