Skip to content

Commit 5fcb8cc

Browse files
committed
No need to allocate empty String arrays over and over.
1 parent b02b6ac commit 5fcb8cc

7 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
*/
8787
public class FilenameUtils {
8888

89+
private static final String[] EMPTY_STRING_ARRAY = new String[0];
90+
8991
private static final String EMPTY_STRING = "";
9092

9193
private static final int NOT_FOUND = -1;
@@ -1494,7 +1496,7 @@ static String[] splitOnTokens(final String text) {
14941496
list.add(buffer.toString());
14951497
}
14961498

1497-
return list.toArray(new String[0]);
1499+
return list.toArray(EMPTY_STRING_ARRAY);
14981500
}
14991501

15001502
/**
@@ -1577,7 +1579,7 @@ private static boolean isIPv6Address(final String inet6Address) {
15771579
} else if (inet6Address.startsWith("::") && !octetList.isEmpty()) {
15781580
octetList.remove(0);
15791581
}
1580-
octets = octetList.toArray(new String[0]);
1582+
octets = octetList.toArray(EMPTY_STRING_ARRAY);
15811583
}
15821584
if (octets.length > IPV6_MAX_HEX_GROUPS) {
15831585
return false;

src/main/java/org/apache/commons/io/filefilter/IOFileFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
public interface IOFileFilter extends FileFilter, FilenameFilter {
3131

32+
String[] EMPTY_STRING_ARRAY = new String[0];
33+
3234
/**
3335
* Checks to see if the File should be accepted by this filter.
3436
* <p>

src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public NameFileFilter(final List<String> names, final IOCase caseSensitivity) {
126126
if (names == null) {
127127
throw new IllegalArgumentException("The list of names must not be null");
128128
}
129-
this.names = names.toArray(new String[0]);
129+
this.names = names.toArray(EMPTY_STRING_ARRAY);
130130
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
131131
}
132132

src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public PrefixFileFilter(final List<String> prefixes, final IOCase caseSensitivit
134134
if (prefixes == null) {
135135
throw new IllegalArgumentException("The list of prefixes must not be null");
136136
}
137-
this.prefixes = prefixes.toArray(new String[0]);
137+
this.prefixes = prefixes.toArray(EMPTY_STRING_ARRAY);
138138
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
139139
}
140140

src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public SuffixFileFilter(final List<String> suffixes, final IOCase caseSensitivit
135135
if (suffixes == null) {
136136
throw new IllegalArgumentException("The list of suffixes must not be null");
137137
}
138-
this.suffixes = suffixes.toArray(new String[0]);
138+
this.suffixes = suffixes.toArray(EMPTY_STRING_ARRAY);
139139
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
140140
}
141141

src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public WildcardFileFilter(final List<String> wildcards, final IOCase caseSensiti
134134
if (wildcards == null) {
135135
throw new IllegalArgumentException("The wildcard list must not be null");
136136
}
137-
this.wildcards = wildcards.toArray(new String[0]);
137+
this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY);
138138
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
139139
}
140140

src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public WildcardFilter(final List<String> wildcards) {
9393
if (wildcards == null) {
9494
throw new IllegalArgumentException("The wildcard list must not be null");
9595
}
96-
this.wildcards = wildcards.toArray(new String[0]);
96+
this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY);
9797
}
9898

9999
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)