From d754a20c3fb29379494a2411bbd69ed876fb9e7a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 6 Nov 2008 10:34:11 +0000 Subject: [PATCH] Take advantage of Java 1.4 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@711835 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/types/resources/Union.java | 20 ++----- .../org/apache/tools/ant/util/FileUtils.java | 59 ++++++------------- 2 files changed, 25 insertions(+), 54 deletions(-) diff --git a/src/main/org/apache/tools/ant/types/resources/Union.java b/src/main/org/apache/tools/ant/types/resources/Union.java index c0c72b259..c9c3b59c2 100644 --- a/src/main/org/apache/tools/ant/types/resources/Union.java +++ b/src/main/org/apache/tools/ant/types/resources/Union.java @@ -17,13 +17,11 @@ */ package org.apache.tools.ant.types.resources; -import java.util.List; -import java.util.Iterator; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.ResourceCollection; @@ -101,23 +99,17 @@ public class Union extends BaseResourceCollectionContainer { if (rc.isEmpty()) { return Collections.EMPTY_LIST; } - //preserve order-encountered using a list; enforce set logic manually: - // (LinkedHashSet better, but JDK 1.4+) - ArrayList union = new ArrayList(rc.size() * 2); - // Use a set as list.contains() can be expensive for lots of resources - Set set = new HashSet(rc.size() * 2); + LinkedHashSet set = new LinkedHashSet(rc.size() * 2); for (Iterator rcIter = rc.iterator(); rcIter.hasNext();) { for (Iterator r = nextRC(rcIter).iterator(); r.hasNext();) { Object o = r.next(); if (asString) { o = o.toString(); } - if (set.add(o)) { - union.add(o); - } + set.add(o); } } - return union; + return set; } private static ResourceCollection nextRC(Iterator i) { diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 373129cd2..df6c4e740 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -28,6 +28,7 @@ import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.MalformedURLException; import java.net.URL; +import java.nio.channels.Channel; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; @@ -1116,46 +1117,7 @@ public class FileUtils { * @since Ant 1.6 */ public String toURI(String path) { - // #8031: first try Java 1.4. - Class uriClazz = null; - try { - uriClazz = Class.forName("java.net.URI"); - } catch (ClassNotFoundException e) { - // OK, Java 1.3. - } - if (uriClazz != null) { - try { - File f = new File(path).getAbsoluteFile(); - java.lang.reflect.Method toURIMethod = File.class.getMethod("toURI", new Class[0]); - Object uriObj = toURIMethod.invoke(f, new Object[] {}); - java.lang.reflect.Method toASCIIStringMethod - = uriClazz.getMethod("toASCIIString", new Class[0]); - return (String) toASCIIStringMethod.invoke(uriObj, new Object[] {}); - } catch (Exception e) { - // Reflection problems? Should not happen, debug. - e.printStackTrace(); - } - } - boolean isDir = new File(path).isDirectory(); - - StringBuffer sb = new StringBuffer("file:"); - - path = resolveFile(null, path).getPath(); - sb.append("//"); - // add an extra slash for filesystems with drive-specifiers - if (!path.startsWith(File.separator)) { - sb.append("/"); - } - path = path.replace('\\', '/'); - try { - sb.append(Locator.encodeURI(path)); - } catch (UnsupportedEncodingException exc) { - throw new BuildException(exc); - } - if (isDir && !path.endsWith("/")) { - sb.append('/'); - } - return sb.toString(); + return new File(path).getAbsoluteFile().toURI().toASCIIString(); } /** @@ -1428,6 +1390,23 @@ public class FileUtils { } } + /** + * Close a Channel without throwing any exception if something went wrong. + * Do not attempt to close it if the argument is null. + * + * @param device channel, can be null. + * @since Ant 1.8.0 + */ + public static void close(Channel device) { + if (null != device) { + try { + device.close(); + } catch (IOException e) { + //ignore + } + } + } + /** * Delete the file with {@link File#delete()} if the argument is not null. * Do nothing on a null argument.