diff --git a/src/main/org/apache/tools/ant/DirectoryScanner.java b/src/main/org/apache/tools/ant/DirectoryScanner.java index b7f6479992..f6c0945d73 100644 --- a/src/main/org/apache/tools/ant/DirectoryScanner.java +++ b/src/main/org/apache/tools/ant/DirectoryScanner.java @@ -1210,7 +1210,8 @@ private void scandir(final File dir, final TokenizedPath path, final boolean fas return; } if (!followSymlinks) { - final ArrayList noLinks = new ArrayList<>(); + final String[] noLinks = new String[newFiles.length]; + int noLinksCount = 0; for (final String newFile : newFiles) { final Path filePath; if (dir == null) { @@ -1228,10 +1229,10 @@ private void scandir(final File dir, final TokenizedPath path, final boolean fas } accountForNotFollowedSymlink(name, file); } else { - noLinks.add(newFile); + noLinks[noLinksCount++] = newFile; } } - newFiles = noLinks.toArray(new String[0]); + newFiles = Arrays.copyOf(noLinks, noLinksCount); } else { directoryNamesFollowed.addFirst(dir.getName()); } @@ -1788,16 +1789,17 @@ private synchronized void clearCaches() { */ private TokenizedPattern[] fillNonPatternSet(final Map map, final String[] patterns) { - final List al = new ArrayList<>(patterns.length); + final TokenizedPattern[] al = new TokenizedPattern[patterns.length]; + int alCount = 0; for (String pattern : patterns) { if (SelectorUtils.hasWildcards(pattern)) { - al.add(new TokenizedPattern(pattern)); + al[alCount++] = new TokenizedPattern(pattern); } else { final String s = isCaseSensitive() ? pattern : pattern.toUpperCase(); map.put(s, new TokenizedPath(s)); } } - return al.toArray(new TokenizedPattern[0]); + return Arrays.copyOf(al, alCount); } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/Copy.java b/src/main/org/apache/tools/ant/taskdefs/Copy.java index 4851bacd7b..93e44b9f3d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Copy.java +++ b/src/main/org/apache/tools/ant/taskdefs/Copy.java @@ -771,15 +771,16 @@ protected Map scan(final Resource[] fromResources, final Fil */ protected void buildMap(final File fromDir, final File toDir, final String[] names, final FileNameMapper mapper, final Hashtable map) { - String[] toCopy = null; + final String[] toCopy; if (forceOverwrite) { - final List v = new ArrayList<>(); + final String[] v = new String[names.length]; + int added = 0; for (String name : names) { if (mapper.mapFileName(name) != null) { - v.add(name); + v[added++] = name; } } - toCopy = v.toArray(new String[0]); + toCopy = Arrays.copyOf(v, added); } else { final SourceFileScanner ds = new SourceFileScanner(this); toCopy = ds.restrict(names, fromDir, toDir, mapper, granularity); @@ -816,15 +817,16 @@ protected void buildMap(final File fromDir, final File toDir, final String[] nam protected Map buildMap(final Resource[] fromResources, final File toDir, final FileNameMapper mapper) { final Map map = new HashMap<>(); - Resource[] toCopy; + final Resource[] toCopy; if (forceOverwrite) { - final List v = new ArrayList<>(); + final Resource[] v = new Resource[fromResources.length]; + int added = 0; for (Resource rc : fromResources) { if (mapper.mapFileName(rc.getName()) != null) { - v.add(rc); + v[added++] = rc; } } - toCopy = v.toArray(new Resource[0]); + toCopy = Arrays.copyOf(v, added); } else { toCopy = ResourceUtils.selectOutOfDateSources(this, fromResources, mapper, name -> new FileResource(toDir, name), granularity);