try-with-resources
diff --git a/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java b/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
index c71f3f3..41a4b71 100644
--- a/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
+++ b/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
@@ -185,9 +185,7 @@
*/
@Override
protected void fetchEntry() {
- ArchiveInputStream i = null;
- try {
- i = getStream();
+ try (ArchiveInputStream i = getStream()) {
ArchiveEntry ae = null;
while ((ae = i.getNextEntry()) != null) {
if (ae.getName().equals(getName())) {
@@ -198,10 +196,6 @@
} catch (IOException e) {
log(e.getMessage(), Project.MSG_DEBUG);
throw new BuildException(e);
- } finally {
- if (i != null) {
- FileUtils.close(i);
- }
}
setEntry(null);
}
diff --git a/src/main/org/apache/ant/compress/resources/SevenZResource.java b/src/main/org/apache/ant/compress/resources/SevenZResource.java
index db491a9..d6410c7 100644
--- a/src/main/org/apache/ant/compress/resources/SevenZResource.java
+++ b/src/main/org/apache/ant/compress/resources/SevenZResource.java
@@ -164,9 +164,7 @@
return;
}
- SevenZFile z = null;
- try {
- z = new SevenZFile(f);
+ try (SevenZFile z = new SevenZFile(f)) {
SevenZArchiveEntry ze = z.getNextEntry();
while (ze != null) {
if (ze.getName().equals(getName())) {
@@ -179,14 +177,6 @@
} catch (IOException e) {
log(e.getMessage(), Project.MSG_DEBUG);
throw new BuildException(e);
- } finally {
- if (z != null) {
- try {
- z.close();
- } catch (IOException ex) {
- // swallow
- }
- }
}
}
diff --git a/src/main/org/apache/ant/compress/resources/SevenZScanner.java b/src/main/org/apache/ant/compress/resources/SevenZScanner.java
index 1f73f20..3ecdd3f 100644
--- a/src/main/org/apache/ant/compress/resources/SevenZScanner.java
+++ b/src/main/org/apache/ant/compress/resources/SevenZScanner.java
@@ -88,11 +88,8 @@
File srcFile = fp.getFile();
SevenZArchiveEntry entry = null;
- SevenZFile zf = null;
- try {
- try {
- zf = new SevenZFile(srcFile);
+ try (SevenZFile zf = new SevenZFile(srcFile)) {
entry = zf.getNextEntry();
while (entry != null) {
/* TODO implement canReadEntryData in CC
@@ -120,14 +117,5 @@
} catch (IOException ex) {
throw new BuildException("Problem opening " + srcFile, ex);
}
- } finally {
- if (zf != null) {
- try {
- zf.close();
- } catch (IOException ex) {
- // swallow
- }
- }
- }
}
}
diff --git a/src/main/org/apache/ant/compress/resources/ZipResource.java b/src/main/org/apache/ant/compress/resources/ZipResource.java
index e54169a..a8ab509 100644
--- a/src/main/org/apache/ant/compress/resources/ZipResource.java
+++ b/src/main/org/apache/ant/compress/resources/ZipResource.java
@@ -172,15 +172,11 @@
return;
}
- ZipFile z = null;
- try {
- z = new ZipFile(getZipfile(), getEncoding());
+ try (ZipFile z = new ZipFile(getZipfile(), getEncoding())) {
setEntry(z.getEntry(getName()));
} catch (IOException e) {
log(e.getMessage(), Project.MSG_DEBUG);
throw new BuildException(e);
- } finally {
- ZipFile.closeQuietly(z);
}
}
diff --git a/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java b/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
index c5bf808..7dbc44a 100644
--- a/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
@@ -543,12 +543,8 @@
ArchiveEntry ent = entryBuilder.buildEntry(r);
out.putArchiveEntry(ent);
if (!r.getResource().isDirectory()) {
- InputStream in = null;
- try {
- in = r.getResource().getInputStream();
+ try (InputStream in = r.getResource().getInputStream()) {
IOUtils.copy(in, out);
- } finally {
- FILE_UTILS.close(in);
}
} else {
addedDirectories.add(r.getName());
diff --git a/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java b/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
index 0eef517..d75de09 100644
--- a/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
@@ -134,27 +134,20 @@
getLocation());
}
- InputStream i = null;
- try {
- i = srcR.getInputStream();
+ try (InputStream i = srcR.getInputStream()) {
expandStream(srcR.getName(), i, dir);
} catch (IOException ioe) {
throw new BuildException("Error while expanding " + srcR.getName(),
ioe, getLocation());
- } finally {
- FileUtils.close(i);
}
}
private void expandStream(String name, InputStream stream, File dir)
throws IOException {
- ArchiveInputStream is = null;
- try {
- is = factory.getArchiveStream(new BufferedInputStream(stream),
- getEncoding());
+ try (ArchiveInputStream is =
+ factory.getArchiveStream(new BufferedInputStream(stream),
+ getEncoding())) {
expandArchiveStream(name, is, dir);
- } finally {
- FileUtils.close(is);
}
}
diff --git a/src/main/org/apache/ant/compress/taskdefs/Un7z.java b/src/main/org/apache/ant/compress/taskdefs/Un7z.java
index c6df0a2..8adfb06 100644
--- a/src/main/org/apache/ant/compress/taskdefs/Un7z.java
+++ b/src/main/org/apache/ant/compress/taskdefs/Un7z.java
@@ -67,21 +67,17 @@
}
*/
log("extracting " + ze.getName(), Project.MSG_DEBUG);
- InputStream is = null;
- try {
- extractFile(fileUtils, srcF, dir,
- is = new InputStream() {
- public int read() throws IOException {
- return zf.read();
- }
- public int read(byte[] b) throws IOException {
- return zf.read(b);
- }
- },
+ try (InputStream is = new InputStream() {
+ public int read() throws IOException {
+ return zf.read();
+ }
+ public int read(byte[] b) throws IOException {
+ return zf.read(b);
+ }
+ }) {
+ extractFile(fileUtils, srcF, dir, is,
ze.getName(), ze.getLastModifiedDate(),
ze.isDirectory(), mapper);
- } finally {
- FileUtils.close(is);
}
ze = zf.getNextEntry();
}
diff --git a/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java b/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
index 6c77f43..17ba7bb 100644
--- a/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
@@ -19,9 +19,10 @@
package org.apache.ant.compress.taskdefs;
import java.io.BufferedInputStream;
-import java.io.FileOutputStream;
+import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
import org.apache.ant.compress.util.CompressorStreamFactory;
import org.apache.ant.compress.util.CompressorWithConcatenatedStreamsFactory;
@@ -96,11 +97,9 @@
log("Expanding " + source.getAbsolutePath() + " to "
+ dest.getAbsolutePath());
- FileOutputStream out = null;
CompressorInputStream zIn = null;
InputStream fis = null;
- try {
- out = new FileOutputStream(dest);
+ try (OutputStream out = Files.newOutputStream(dest.toPath())) {
zIn = StreamHelper.getInputStream(factory, srcResource);
if (zIn == null) {
fis = srcResource.getInputStream();
@@ -119,7 +118,6 @@
throw new BuildException(msg, ioe, getLocation());
} finally {
FileUtils.close(fis);
- FileUtils.close(out);
FileUtils.close(zIn);
}
}
diff --git a/src/main/org/apache/ant/compress/taskdefs/Unzip.java b/src/main/org/apache/ant/compress/taskdefs/Unzip.java
index aa47f15..075948a 100644
--- a/src/main/org/apache/ant/compress/taskdefs/Unzip.java
+++ b/src/main/org/apache/ant/compress/taskdefs/Unzip.java
@@ -52,15 +52,13 @@
// overridden in order to take advantage of ZipFile
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
- ZipFile zf = null;
FileNameMapper mapper = getMapper();
if (!srcF.exists()) {
throw new BuildException("Unable to expand " + srcF
+ " as the file does not exist",
getLocation());
}
- try {
- zf = new ZipFile(srcF, getEncoding(), true);
+ try (ZipFile zf = new ZipFile(srcF, getEncoding(), true)) {
boolean empty = true;
Enumeration e = zf.getEntries();
while (e.hasMoreElements()) {
@@ -90,8 +88,6 @@
"Error while expanding " + srcF.getPath()
+ "\n" + ioe.toString(),
ioe);
- } finally {
- ZipFile.closeQuietly(zf);
}
}