Performance Enhancement: Call toArray with Zero Array Size (closes
toArray should, at least since Java 7, be used with new T[0] instead of size, since it is "faster, safer, and contractually cleaner" (https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion)
MODIFY
src/main/java/org/apache/commons/io/FilenameUtils.java
src/main/java/org/apache/commons/io/FilenameUtils.java
@@ -1494,7 +1494,7 @@ public static boolean wildcardMatch(final String fileName, final String wildcard
list.add(buffer.toString());
}
- return list.toArray( new String[ list.size() ] );
+ return list.toArray( new String[ 0 ] );
}
/**
@@ -1577,7 +1577,7 @@ private static boolean isIPv6Address(final String inet6Address) {
} else if (inet6Address.startsWith("::") && !octetList.isEmpty()) {
octetList.remove(0);
}
- octets = octetList.toArray(new String[octetList.size()]);
+ octets = octetList.toArray(new String[0]);
}
if (octets.length > IPV6_MAX_HEX_GROUPS) {
return false;

MODIFY
src/main/java/org/apache/commons/io/comparator/CompositeFileComparator.java
src/main/java/org/apache/commons/io/comparator/CompositeFileComparator.java
@@ -76,7 +76,7 @@ public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
for (final Comparator<File> comparator : delegates) {
list.add(comparator);
}
- this.delegates = (Comparator<File>[]) list.toArray(new Comparator<?>[list.size()]); //2
+ this.delegates = (Comparator<File>[]) list.toArray(new Comparator<?>[0]); //2
}
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java
src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java
@@ -89,7 +89,7 @@ public FileFilterUtils() {
acceptedFiles.add(file);
}
}
- return acceptedFiles.toArray(new File[acceptedFiles.size()]);
+ return acceptedFiles.toArray(new File[0]);
}
/**
@@ -120,7 +120,7 @@ public FileFilterUtils() {
*/
public static File[] filter(final IOFileFilter filter, final Iterable<File> files) {
final List<File> acceptedFiles = filterList(filter, files);
- return acceptedFiles.toArray(new File[acceptedFiles.size()]);
+ return acceptedFiles.toArray(new File[0]);
}
/**

MODIFY
src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java
src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java
@@ -126,7 +126,7 @@ public NameFileFilter(final List<String> names, final IOCase caseSensitivity) {
if (names == null) {
throw new IllegalArgumentException("The list of names must not be null");
}
- this.names = names.toArray(new String[names.size()]);
+ this.names = names.toArray(new String[0]);
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
@@ -134,7 +134,7 @@ public PrefixFileFilter(final List<String> prefixes, final IOCase caseSensitivit
if (prefixes == null) {
throw new IllegalArgumentException("The list of prefixes must not be null");
}
- this.prefixes = prefixes.toArray(new String[prefixes.size()]);
+ this.prefixes = prefixes.toArray(new String[0]);
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
@@ -135,7 +135,7 @@ public SuffixFileFilter(final List<String> suffixes, final IOCase caseSensitivit
if (suffixes == null) {
throw new IllegalArgumentException("The list of suffixes must not be null");
}
- this.suffixes = suffixes.toArray(new String[suffixes.size()]);
+ this.suffixes = suffixes.toArray(new String[0]);
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java
src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java
@@ -134,7 +134,7 @@ public WildcardFileFilter(final List<String> wildcards, final IOCase caseSensiti
if (wildcards == null) {
throw new IllegalArgumentException("The wildcard list must not be null");
}
- this.wildcards = wildcards.toArray(new String[wildcards.size()]);
+ this.wildcards = wildcards.toArray(new String[0]);
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java
src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java
@@ -93,7 +93,7 @@ public WildcardFilter(final List<String> wildcards) {
if (wildcards == null) {
throw new IllegalArgumentException("The wildcard list must not be null");
}
- this.wildcards = wildcards.toArray(new String[wildcards.size()]);
+ this.wildcards = wildcards.toArray(new String[0]);
}
//-----------------------------------------------------------------------

Performance Enhancement: Call toArray with Zero Array Size (closes #65)

toArray should, at least since Java 7, be used with new T[0] instead of size, since it is "faster, safer, and contractually cleaner" (https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion)