summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/Untar.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/org/apache/tools/ant/taskdefs/Untar.java')
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/Untar.java61
1 files changed, 25 insertions, 36 deletions
diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java
index 430aa5401..fa26c3544 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Untar.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java
@@ -37,8 +37,6 @@ import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
-
-
/**
* Untar a file.
* <p>PatternSets are used to select files to extract
@@ -86,33 +84,32 @@ public class Untar extends Expand {
*
* @since Ant 1.8.0
*/
+ @Override
public void setScanForUnicodeExtraFields(boolean b) {
- throw new BuildException("The " + getTaskName()
- + " task doesn't support the encoding"
- + " attribute", getLocation());
+ throw new BuildException(
+ "The " + getTaskName()
+ + " task doesn't support the encoding attribute",
+ getLocation());
}
/**
* @see Expand#expandFile(FileUtils, File, File)
*/
/** {@inheritDoc} */
+ @Override
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
- InputStream fis = null;
if (!srcF.exists()) {
throw new BuildException("Unable to untar "
+ srcF
+ " as the file does not exist",
getLocation());
}
- try {
- fis = Files.newInputStream(srcF.toPath());
+ try (InputStream fis = Files.newInputStream(srcF.toPath())) {
expandStream(srcF.getPath(), fis, dir);
} catch (IOException ioe) {
throw new BuildException("Error while expanding " + srcF.getPath()
+ "\n" + ioe.toString(),
ioe, getLocation());
- } finally {
- FileUtils.close(fis);
}
}
@@ -123,6 +120,7 @@ public class Untar extends Expand {
* @param dir the destination directory
* @since Ant 1.7
*/
+ @Override
protected void expandResource(Resource srcR, File dir) {
if (!srcR.isExists()) {
throw new BuildException("Unable to untar "
@@ -131,15 +129,11 @@ public class Untar extends Expand {
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);
}
}
@@ -148,16 +142,13 @@ public class Untar extends Expand {
*/
private void expandStream(String name, InputStream stream, File dir)
throws IOException {
- TarInputStream tis = null;
- try {
- tis =
- new TarInputStream(compression.decompress(name,
- new BufferedInputStream(stream)),
- getEncoding());
+ try (TarInputStream tis = new TarInputStream(
+ compression.decompress(name, new BufferedInputStream(stream)),
+ getEncoding())) {
log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
- TarEntry te = null;
boolean empty = true;
FileNameMapper mapper = getMapper();
+ TarEntry te;
while ((te = tis.getNextEntry()) != null) {
empty = false;
extractFile(FileUtils.getFileUtils(), null, dir, tis,
@@ -165,11 +156,9 @@ public class Untar extends Expand {
te.isDirectory(), mapper);
}
if (empty && getFailOnEmptyArchive()) {
- throw new BuildException("archive '" + name + "' is empty");
+ throw new BuildException("archive '%s' is empty", name);
}
log("expand complete", Project.MSG_VERBOSE);
- } finally {
- FileUtils.close(tis);
}
}
@@ -213,8 +202,9 @@ public class Untar extends Expand {
*
* @return valid values
*/
+ @Override
public String[] getValues() {
- return new String[] {NONE, GZIP, BZIP2, XZ};
+ return new String[] { NONE, GZIP, BZIP2, XZ };
}
/**
@@ -234,19 +224,18 @@ public class Untar extends Expand {
final String v = getValue();
if (GZIP.equals(v)) {
return new GZIPInputStream(istream);
- } else if (XZ.equals(v)) {
+ }
+ if (XZ.equals(v)) {
return newXZInputStream(istream);
- } else {
- if (BZIP2.equals(v)) {
- final char[] magic = new char[] {'B', 'Z'};
- for (int i = 0; i < magic.length; i++) {
- if (istream.read() != magic[i]) {
- throw new BuildException(
- "Invalid bz2 file." + name);
- }
+ }
+ if (BZIP2.equals(v)) {
+ final char[] magic = new char[] { 'B', 'Z' };
+ for (int i = 0; i < magic.length; i++) {
+ if (istream.read() != magic[i]) {
+ throw new BuildException("Invalid bz2 file." + name);
}
- return new CBZip2InputStream(istream);
}
+ return new CBZip2InputStream(istream);
}
return istream;
}