summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/Jar.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/org/apache/tools/ant/taskdefs/Jar.java')
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/Jar.java257
1 files changed, 109 insertions, 148 deletions
diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java
index 7902075b6..26f844c46 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Jar.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java
@@ -28,6 +28,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
@@ -35,6 +36,8 @@ import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
@@ -74,7 +77,7 @@ public class Jar extends Zip {
/**
* List of all known SPI Services
*/
- private List<Service> serviceList = new ArrayList<Service>();
+ private List<Service> serviceList = new ArrayList<>();
/** merged manifests added through addConfiguredManifest */
private Manifest configuredManifest;
@@ -138,7 +141,7 @@ public class Jar extends Zip {
*
* @since Ant 1.6
*/
- private Vector<String> rootEntries;
+ private List<String> rootEntries;
/**
* Path containing jars that shall be indexed in addition to this archive.
@@ -182,7 +185,7 @@ public class Jar extends Zip {
emptyBehavior = "create";
setEncoding("UTF8");
setZip64Mode(Zip64ModeAttribute.NEVER);
- rootEntries = new Vector<String>();
+ rootEntries = new Vector<>();
}
/**
@@ -190,6 +193,7 @@ public class Jar extends Zip {
* @param we not used
* @ant.attribute ignore="true"
*/
+ @Override
public void setWhenempty(WhenEmpty we) {
log("JARs are never empty, they contain at least a manifest file",
Project.MSG_WARN);
@@ -225,6 +229,7 @@ public class Jar extends Zip {
* @deprecated since 1.5.x.
* Use setDestFile(File) instead.
*/
+ @Deprecated
public void setJarfile(File jarFile) {
setDestFile(jarFile);
}
@@ -301,29 +306,13 @@ public class Jar extends Zip {
}
private Manifest getManifest(File manifestFile) {
-
- Manifest newManifest = null;
- InputStream fis = null;
- InputStreamReader isr = null;
- try {
- fis = Files.newInputStream(manifestFile.toPath());
- if (manifestEncoding == null) {
- isr = new InputStreamReader(fis);
- } else {
- isr = new InputStreamReader(fis, manifestEncoding);
- }
- newManifest = getManifest(isr);
- } catch (UnsupportedEncodingException e) {
- throw new BuildException("Unsupported encoding while reading manifest: "
- + e.getMessage(), e);
+ try (InputStreamReader isr = new InputStreamReader(
+ Files.newInputStream(manifestFile.toPath()), getManifestCharset())) {
+ return getManifest(isr);
} catch (IOException e) {
throw new BuildException("Unable to read manifest file: "
- + manifestFile
- + " (" + e.getMessage() + ")", e);
- } finally {
- FileUtils.close(isr);
+ + manifestFile + " (" + e.getMessage() + ")", e);
}
- return newManifest;
}
/**
@@ -332,32 +321,27 @@ public class Jar extends Zip {
* @since Ant 1.5.2
*/
private Manifest getManifestFromJar(File jarFile) throws IOException {
- ZipFile zf = null;
- try {
- zf = new ZipFile(jarFile);
+ try (ZipFile zf = new ZipFile(jarFile)) {
// must not use getEntry as "well behaving" applications
// must accept the manifest in any capitalization
Enumeration<? extends ZipEntry> e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = e.nextElement();
- if (ze.getName().equalsIgnoreCase(MANIFEST_NAME)) {
- InputStreamReader isr =
- new InputStreamReader(zf.getInputStream(ze), "UTF-8");
- return getManifest(isr);
+ if (MANIFEST_NAME.equalsIgnoreCase(ze.getName())) {
+ try (InputStreamReader isr =
+ new InputStreamReader(zf.getInputStream(ze), "UTF-8")) {
+ return getManifest(isr);
+ }
}
}
return null;
- } finally {
- FileUtils.close(zf);
}
}
private Manifest getManifest(Reader r) {
-
- Manifest newManifest = null;
try {
- newManifest = new Manifest(r);
+ return new Manifest(r);
} catch (ManifestException e) {
log("Manifest is invalid: " + e.getMessage(), Project.MSG_ERR);
throw new BuildException("Invalid Manifest: " + manifestFile,
@@ -366,23 +350,18 @@ public class Jar extends Zip {
throw new BuildException("Unable to read manifest file"
+ " (" + e.getMessage() + ")", e);
}
- return newManifest;
}
private boolean jarHasIndex(File jarFile) throws IOException {
- ZipFile zf = null;
- try {
- zf = new ZipFile(jarFile);
+ try (ZipFile zf = new ZipFile(jarFile)) {
Enumeration<? extends ZipEntry> e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = e.nextElement();
- if (ze.getName().equalsIgnoreCase(INDEX_NAME)) {
+ if (INDEX_NAME.equalsIgnoreCase(ze.getName())) {
return true;
}
}
return false;
- } finally {
- FileUtils.close(zf);
}
}
@@ -404,7 +383,7 @@ public class Jar extends Zip {
mergeManifestsMain = config != null && "merge".equals(config.getValue());
if (filesetManifestConfig != null
- && !filesetManifestConfig.getValue().equals("skip")) {
+ && !"skip".equals(filesetManifestConfig.getValue())) {
doubleFilePass = true;
}
@@ -449,19 +428,12 @@ public class Jar extends Zip {
*/
private void writeServices(ZipOutputStream zOut) throws IOException {
for (Service service : serviceList) {
- InputStream is = null;
- try {
- is = service.getAsStream();
- //stolen from writeManifest
+ try (InputStream is = service.getAsStream()) {
+ //stolen from writeManifest
super.zipFile(is, zOut,
"META-INF/services/" + service.getType(),
System.currentTimeMillis(), null,
ZipFileSet.DEFAULT_FILE_MODE);
- } finally {
- // technically this is unnecessary since
- // Service.getAsStream returns a ByteArrayInputStream
- // and not closing it wouldn't do any harm.
- FileUtils.close(is);
}
}
}
@@ -491,6 +463,7 @@ public class Jar extends Zip {
* @throws IOException on I/O errors
* @throws BuildException on other errors
*/
+ @Override
protected void initZipOutputStream(ZipOutputStream zOut)
throws IOException, BuildException {
@@ -504,12 +477,10 @@ public class Jar extends Zip {
private Manifest createManifest()
throws BuildException {
try {
- if (manifest == null) {
- if (manifestFile != null) {
- // if we haven't got the manifest yet, attempt to
- // get it now and have manifest be the final merge
- manifest = getManifest(manifestFile);
- }
+ if (manifest == null && manifestFile != null) {
+ // if we haven't got the manifest yet, attempt to
+ // get it now and have manifest be the final merge
+ manifest = getManifest(manifestFile);
}
// fileset manifest must come even before the default
@@ -598,6 +569,7 @@ public class Jar extends Zip {
* @throws IOException on I/O errors
* @throws BuildException on other errors
*/
+ @Override
protected void finalizeZipOutputStream(ZipOutputStream zOut)
throws IOException, BuildException {
@@ -667,14 +639,10 @@ public class Jar extends Zip {
throw new IOException("Encountered an error writing jar index");
}
writer.close();
- ByteArrayInputStream bais =
- new ByteArrayInputStream(baos.toByteArray());
- try {
+ try (ByteArrayInputStream bais =
+ new ByteArrayInputStream(baos.toByteArray())) {
super.zipFile(bais, zOut, INDEX_NAME, System.currentTimeMillis(),
null, ZipFileSet.DEFAULT_FILE_MODE);
- } finally {
- // not really required
- FileUtils.close(bais);
}
}
@@ -690,6 +658,7 @@ public class Jar extends Zip {
* @param mode the Unix permissions to set.
* @throws IOException on error
*/
+ @Override
protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath,
long lastModified, File fromArchive, int mode)
throws IOException {
@@ -703,8 +672,8 @@ public class Jar extends Zip {
+ " be replaced by a newly generated one.",
Project.MSG_WARN);
} else {
- if (index && vPath.indexOf("/") == -1) {
- rootEntries.addElement(vPath);
+ if (index && vPath.indexOf('/') == -1) {
+ rootEntries.add(vPath);
}
super.zipFile(is, zOut, vPath, lastModified, fromArchive, mode);
}
@@ -715,40 +684,29 @@ public class Jar extends Zip {
// If this is the same name specified in 'manifest', this
// is the manifest to use
log("Found manifest " + file, Project.MSG_VERBOSE);
- try {
- if (is != null) {
- InputStreamReader isr;
- if (manifestEncoding == null) {
- isr = new InputStreamReader(is);
- } else {
- isr = new InputStreamReader(is, manifestEncoding);
- }
+ if (is == null) {
+ manifest = getManifest(file);
+ } else {
+ try (InputStreamReader isr =
+ new InputStreamReader(is, getManifestCharset())) {
manifest = getManifest(isr);
- } else {
- manifest = getManifest(file);
}
- } catch (UnsupportedEncodingException e) {
- throw new BuildException("Unsupported encoding while reading "
- + "manifest: " + e.getMessage(), e);
}
} else if (filesetManifestConfig != null
- && !filesetManifestConfig.getValue().equals("skip")) {
+ && !"skip".equals(filesetManifestConfig.getValue())) {
// we add this to our group of fileset manifests
logWhenWriting("Found manifest to merge in file " + file,
Project.MSG_VERBOSE);
try {
- Manifest newManifest = null;
- if (is != null) {
- InputStreamReader isr;
- if (manifestEncoding == null) {
- isr = new InputStreamReader(is);
- } else {
- isr = new InputStreamReader(is, manifestEncoding);
- }
- newManifest = getManifest(isr);
- } else {
+ Manifest newManifest;
+ if (is == null) {
newManifest = getManifest(file);
+ } else {
+ try (InputStreamReader isr =
+ new InputStreamReader(is, getManifestCharset())) {
+ newManifest = getManifest(isr);
+ }
}
if (filesetManifest == null) {
@@ -804,6 +762,7 @@ public class Jar extends Zip {
*
* @exception BuildException if it likes
*/
+ @Override
protected ArchiveState getResourcesToAdd(ResourceCollection[] rcs,
File zipFile,
boolean needsUpdate)
@@ -874,33 +833,32 @@ public class Jar extends Zip {
* @return true for historic reasons
* @throws BuildException on error
*/
+ @Override
protected boolean createEmptyZip(File zipFile) throws BuildException {
if (!createEmpty) {
return true;
}
- if (emptyBehavior.equals("skip")) {
+ if ("skip".equals(emptyBehavior)) {
if (!skipWriting) {
log("Warning: skipping " + archiveType + " archive "
+ zipFile + " because no files were included.",
Project.MSG_WARN);
}
return true;
- } else if (emptyBehavior.equals("fail")) {
+ }
+ if ("fail".equals(emptyBehavior)) {
throw new BuildException("Cannot create " + archiveType
+ " archive " + zipFile
+ ": no files were included.",
getLocation());
}
- ZipOutputStream zOut = null;
- try {
- if (!skipWriting) {
- log("Building MANIFEST-only jar: "
+ if (!skipWriting) {
+ log("Building MANIFEST-only jar: "
+ getDestFile().getAbsolutePath());
- }
- zOut = new ZipOutputStream(getDestFile());
-
+ }
+ try (ZipOutputStream zOut = new ZipOutputStream(getDestFile())) {
zOut.setEncoding(getEncoding());
zOut.setUseZip64(getZip64Mode().getMode());
if (isCompress()) {
@@ -915,8 +873,6 @@ public class Jar extends Zip {
+ " (" + ioe.getMessage() + ")", ioe,
getLocation());
} finally {
- // Close the output stream.
- FileUtils.close(zOut);
createEmpty = false;
}
return true;
@@ -928,6 +884,7 @@ public class Jar extends Zip {
*
* @see Zip#cleanUp
*/
+ @Override
protected void cleanUp() {
super.cleanUp();
checkJarSpec();
@@ -939,7 +896,7 @@ public class Jar extends Zip {
filesetManifest = null;
originalManifest = null;
}
- rootEntries.removeAllElements();
+ rootEntries.clear();
}
// CheckStyle:LineLength OFF - Link is too long.
@@ -950,7 +907,7 @@ public class Jar extends Zip {
// CheckStyle:LineLength ON
private void checkJarSpec() {
String br = System.getProperty("line.separator");
- StringBuffer message = new StringBuffer();
+ StringBuilder message = new StringBuilder();
Section mainSection = (configuredManifest == null)
? null
: configuredManifest.getMainSection();
@@ -975,11 +932,10 @@ public class Jar extends Zip {
message.append(br);
message.append("Location: ").append(getLocation());
message.append(br);
- if (strict.getValue().equalsIgnoreCase("fail")) {
+ if ("fail".equalsIgnoreCase(strict.getValue())) {
throw new BuildException(message.toString(), getLocation());
- } else {
- logWhenWriting(message.toString(), strict.getLogLevel());
}
+ logWhenWriting(message.toString(), strict.getLogLevel());
}
}
@@ -990,6 +946,7 @@ public class Jar extends Zip {
*
* @since 1.44, Ant 1.5
*/
+ @Override
public void reset() {
super.reset();
emptyBehavior = "create";
@@ -1008,8 +965,9 @@ public class Jar extends Zip {
* Get the list of valid strings.
* @return the list of values - "skip", "merge" and "mergewithoutmain"
*/
+ @Override
public String[] getValues() {
- return new String[] {"skip", "merge", "mergewithoutmain"};
+ return new String[] { "skip", "merge", "mergewithoutmain" };
}
}
@@ -1056,9 +1014,7 @@ public class Jar extends Zip {
writer.println(dir);
}
- for (String file : files) {
- writer.println(file);
- }
+ files.forEach(writer::println);
}
/**
@@ -1084,39 +1040,27 @@ public class Jar extends Zip {
protected static String findJarName(String fileName,
String[] classpath) {
if (classpath == null) {
- return (new File(fileName)).getName();
+ return new File(fileName).getName();
}
fileName = fileName.replace(File.separatorChar, '/');
- TreeMap<String, String> matches = new TreeMap<String, String>(new Comparator<Object>() {
- // longest match comes first
- public int compare(Object o1, Object o2) {
- if (o1 instanceof String && o2 instanceof String) {
- return ((String) o2).length()
- - ((String) o1).length();
- }
- return 0;
+ SortedMap<String, String> matches = new TreeMap<>(Comparator
+ .<String> comparingInt(s -> s == null ? 0 : s.length()).reversed());
+
+ for (String element : classpath) {
+ String candidate = element;
+ while (true) {
+ if (fileName.endsWith(candidate)) {
+ matches.put(candidate, element);
+ break;
}
- });
-
- for (int i = 0; i < classpath.length; i++) {
- if (fileName.endsWith(classpath[i])) {
- matches.put(classpath[i], classpath[i]);
- } else {
- int slash = classpath[i].indexOf("/");
- String candidate = classpath[i];
- while (slash > -1) {
- candidate = candidate.substring(slash + 1);
- if (fileName.endsWith(candidate)) {
- matches.put(candidate, classpath[i]);
- break;
- }
- slash = candidate.indexOf("/");
+ int slash = candidate.indexOf('/');
+ if (slash < 0) {
+ break;
}
+ candidate = candidate.substring(slash + 1);
}
}
-
- return matches.size() == 0
- ? null : (String) matches.get(matches.firstKey());
+ return matches.isEmpty() ? null : matches.get(matches.firstKey());
}
/**
@@ -1133,21 +1077,21 @@ public class Jar extends Zip {
throws IOException {
try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(file, "utf-8")) {
Enumeration<org.apache.tools.zip.ZipEntry> entries = zf.getEntries();
- HashSet<String> dirSet = new HashSet<String>();
+ Set<String> dirSet = new HashSet<>();
while (entries.hasMoreElements()) {
org.apache.tools.zip.ZipEntry ze =
entries.nextElement();
String name = ze.getName();
if (ze.isDirectory()) {
dirSet.add(name);
- } else if (name.indexOf("/") == -1) {
+ } else if (name.indexOf('/') == -1) {
files.add(name);
} else {
// a file, not in the root
// since the jar may be one without directory
// entries, add the parent dir of this file as
// well.
- dirSet.add(name.substring(0, name.lastIndexOf("/") + 1));
+ dirSet.add(name.substring(0, name.lastIndexOf('/') + 1));
}
}
dirs.addAll(dirSet);
@@ -1157,13 +1101,12 @@ public class Jar extends Zip {
private Resource[][] grabManifests(ResourceCollection[] rcs) {
Resource[][] manifests = new Resource[rcs.length][];
for (int i = 0; i < rcs.length; i++) {
- Resource[][] resources = null;
+ Resource[][] resources;
if (rcs[i] instanceof FileSet) {
- resources = grabResources(new FileSet[] {(FileSet) rcs[i]});
+ resources = grabResources(new FileSet[] { (FileSet) rcs[i] });
} else {
- resources = grabNonFileSetResources(new ResourceCollection[] {
- rcs[i]
- });
+ resources = grabNonFileSetResources(
+ new ResourceCollection[] { rcs[i] });
}
for (int j = 0; j < resources[0].length; j++) {
String name = resources[0][j].getName().replace('\\', '/');
@@ -1179,7 +1122,7 @@ public class Jar extends Zip {
name = prefix + name;
}
}
- if (name.equalsIgnoreCase(MANIFEST_NAME)) {
+ if (MANIFEST_NAME.equalsIgnoreCase(name)) {
manifests[i] = new Resource[] {resources[0][j]};
break;
}
@@ -1191,11 +1134,26 @@ public class Jar extends Zip {
return manifests;
}
+ private Charset getManifestCharset() {
+ if (manifestEncoding == null) {
+ return Charset.defaultCharset();
+ }
+ try {
+ return Charset.forName(manifestEncoding);
+ } catch (IllegalArgumentException e) {
+ throw new BuildException(
+ "Unsupported encoding while reading manifest: "
+ + e.getMessage(),
+ e);
+ }
+ }
+
/** The strict enumerated type. */
public static class StrictMode extends EnumeratedAttribute {
/** Public no arg constructor. */
public StrictMode() {
}
+
/**
* Constructor with an arg.
* @param value the enumerated value as a string.
@@ -1203,18 +1161,21 @@ public class Jar extends Zip {
public StrictMode(String value) {
setValue(value);
}
+
/**
* Get List of valid strings.
* @return the list of values.
*/
+ @Override
public String[] getValues() {
- return new String[]{"fail", "warn", "ignore"};
+ return new String[] { "fail", "warn", "ignore" };
}
+
/**
* @return The log level according to the strict mode.
*/
public int getLogLevel() {
- return (getValue().equals("ignore")) ? Project.MSG_VERBOSE : Project.MSG_WARN;
+ return "ignore".equals(getValue()) ? Project.MSG_VERBOSE : Project.MSG_WARN;
}
}
}