Browse Source

Take more care with regard to open files/streams.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267750 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
7f4859f077
5 changed files with 97 additions and 49 deletions
  1. +19
    -8
      src/main/org/apache/tools/ant/taskdefs/GUnzip.java
  2. +17
    -7
      src/main/org/apache/tools/ant/taskdefs/GZip.java
  3. +31
    -22
      src/main/org/apache/tools/ant/taskdefs/Tar.java
  4. +14
    -5
      src/main/org/apache/tools/ant/taskdefs/Untar.java
  5. +16
    -7
      src/main/org/apache/tools/ant/taskdefs/Zip.java

+ 19
- 8
src/main/org/apache/tools/ant/taskdefs/GUnzip.java View File

@@ -82,15 +82,15 @@ public class GUnzip extends Task {


public void execute() throws BuildException { public void execute() throws BuildException {
if (source == null) { if (source == null) {
throw new BuildException("No source specified");
throw new BuildException("No source for gunzip specified", location);
} }


if (!source.exists()) { if (!source.exists()) {
throw new BuildException("source doesn't exist");
throw new BuildException("source doesn't exist", location);
} }


if (source.isDirectory()) { if (source.isDirectory()) {
throw new BuildException("Cannot expand a directory");
throw new BuildException("Cannot expand a directory", location);
} }


if (dest == null) { if (dest == null) {
@@ -112,20 +112,31 @@ public class GUnzip extends Task {
log("Expanding "+ source.getAbsolutePath() + " to " log("Expanding "+ source.getAbsolutePath() + " to "
+ dest.getAbsolutePath()); + dest.getAbsolutePath());


FileOutputStream out = null;
GZIPInputStream zIn = null;
try { 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]; byte[] buffer = new byte[8 * 1024];
int count = 0; int count = 0;
do { do {
out.write(buffer, 0, count); out.write(buffer, 0, count);
count = zIn.read(buffer, 0, buffer.length); count = zIn.read(buffer, 0, buffer.length);
} while (count != -1); } while (count != -1);
zIn.close();
out.close();
} catch (IOException ioe) { } catch (IOException ioe) {
String msg = "Problem expanding gzip " + ioe.getMessage(); 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) {}
}
} }
} }
} }


+ 17
- 7
src/main/org/apache/tools/ant/taskdefs/GZip.java View File

@@ -83,19 +83,26 @@ public class GZip extends Task {
public void execute() throws BuildException { public void execute() throws BuildException {
log("Building gzip: " + zipFile.getAbsolutePath()); log("Building gzip: " + zipFile.getAbsolutePath());
GZIPOutputStream zOut = null;
try { try {
GZIPOutputStream zOut = new GZIPOutputStream(new FileOutputStream(zipFile));
zOut = new GZIPOutputStream(new FileOutputStream(zipFile));
if (source.isDirectory()) { if (source.isDirectory()) {
log ("Cannot Gzip a directory!");
log ("Cannot Gzip a directory!", Project.MSG_ERR);
} else { } else {
zipFile(source, zOut); zipFile(source, zOut);
} }
// close up
zOut.close();
} catch (IOException ioe) { } catch (IOException ioe) {
String msg = "Problem creating gzip " + ioe.getMessage(); 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 throws IOException
{ {
FileInputStream fIn = new FileInputStream(file); FileInputStream fIn = new FileInputStream(file);
zipFile(fIn, zOut);
fIn.close();
try {
zipFile(fIn, zOut);
} finally {
fIn.close();
}
} }
} }

+ 31
- 22
src/main/org/apache/tools/ant/taskdefs/Tar.java View File

@@ -87,18 +87,20 @@ public class Tar extends MatchingTask {
log("Building tar: "+ tarFile.getAbsolutePath()); log("Building tar: "+ tarFile.getAbsolutePath());


if (baseDir == null) { if (baseDir == null) {
throw new BuildException("basedir attribute must be set!");
throw new BuildException("basedir attribute must be set!",
location);
} }
if (!baseDir.exists()) { if (!baseDir.exists()) {
throw new BuildException("basedir does not exist!");
throw new BuildException("basedir does not exist!", location);
} }


DirectoryScanner ds = super.getDirectoryScanner(baseDir); DirectoryScanner ds = super.getDirectoryScanner(baseDir);


String[] files = ds.getIncludedFiles(); String[] files = ds.getIncludedFiles();


TarOutputStream tOut = null;
try { try {
TarOutputStream tOut = new TarOutputStream(new FileOutputStream(tarFile));
tOut = new TarOutputStream(new FileOutputStream(tarFile));
tOut.setDebug(true); tOut.setDebug(true);


for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
@@ -106,12 +108,17 @@ public class Tar extends MatchingTask {
String name = files[i].replace(File.separatorChar,'/'); String name = files[i].replace(File.separatorChar,'/');
tarFile(f, tOut, name); tarFile(f, tOut, name);
} }

// close up
tOut.close();
} catch (IOException ioe) { } catch (IOException ioe) {
String msg = "Problem creating TAR: " + ioe.getMessage(); 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); 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();
}
} }
} }

+ 14
- 5
src/main/org/apache/tools/ant/taskdefs/Untar.java View File

@@ -79,11 +79,13 @@ public class Untar extends Task {
Touch touch = (Touch) project.createTask("touch"); Touch touch = (Touch) project.createTask("touch");
touch.setTarget(target); touch.setTarget(target);
File srcF=project.resolveFile(source);

TarInputStream tis = null;
try { try {
if (source == null) { if (source == null) {
throw new BuildException("No source specified", location); throw new BuildException("No source specified", location);
} }
File srcF=project.resolveFile(source);
if (!srcF.exists()) { if (!srcF.exists()) {
throw new BuildException("source "+srcF+" doesn't exist", throw new BuildException("source "+srcF+" doesn't exist",
location); location);
@@ -95,8 +97,7 @@ public class Untar extends Task {
File dir=project.resolveFile(dest); File dir=project.resolveFile(dest);


log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); 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; TarEntry te = null;


while ((te = tis.getNextEntry()) != null) { while ((te = tis.getNextEntry()) != null) {
@@ -133,8 +134,16 @@ public class Untar extends Task {
} }
} }
} catch (IOException ioe) { } 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) {}
}
}
} }


/** /**


+ 16
- 7
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -122,8 +122,9 @@ public class Zip extends MatchingTask {


log("Building "+ archiveType +": "+ zipFile.getAbsolutePath()); log("Building "+ archiveType +": "+ zipFile.getAbsolutePath());


ZipOutputStream zOut = null;
try { try {
ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile));
zOut = new ZipOutputStream(new FileOutputStream(zipFile));
if (doCompress) { if (doCompress) {
zOut.setMethod(ZipOutputStream.DEFLATED); zOut.setMethod(ZipOutputStream.DEFLATED);
} else { } else {
@@ -142,12 +143,17 @@ public class Zip extends MatchingTask {
String name = files[i].replace(File.separatorChar,'/'); String name = files[i].replace(File.separatorChar,'/');
zipFile(f, zOut, name); zipFile(f, zOut, name);
} }

// close up
zOut.close();
} catch (IOException ioe) { } catch (IOException ioe) {
String msg = "Problem creating " + archiveType + " " + ioe.getMessage(); 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 throws IOException
{ {
FileInputStream fIn = new FileInputStream(file); FileInputStream fIn = new FileInputStream(file);
zipFile(fIn, zOut, vPath, file.lastModified());
fIn.close();
try {
zipFile(fIn, zOut, vPath, file.lastModified());
} finally {
fIn.close();
}
} }
} }

Loading…
Cancel
Save