Skip to content

Commit bb6ab5d

Browse files
committed
OS-independent alphabetical sorting of directory content
Issue: SPR-16838
1 parent 51091f2 commit bb6ab5d

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.net.URLConnection;
2929
import java.util.Arrays;
3030
import java.util.Collections;
31+
import java.util.Comparator;
3132
import java.util.Enumeration;
3233
import java.util.LinkedHashSet;
3334
import java.util.Set;
@@ -783,15 +784,7 @@ protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> r
783784
logger.debug("Searching directory [" + dir.getAbsolutePath() +
784785
"] for files matching pattern [" + fullPattern + "]");
785786
}
786-
File[] dirContents = dir.listFiles();
787-
if (dirContents == null) {
788-
if (logger.isWarnEnabled()) {
789-
logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
790-
}
791-
return;
792-
}
793-
Arrays.sort(dirContents);
794-
for (File content : dirContents) {
787+
for (File content : listDirectory(dir)) {
795788
String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
796789
if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
797790
if (!content.canRead()) {
@@ -810,6 +803,25 @@ protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> r
810803
}
811804
}
812805

806+
/**
807+
* Determine a sorted list of files in the given directory.
808+
* @param dir the directory to introspect
809+
* @return the sorted list of files (by default in alphabetical order)
810+
* @since 5.1
811+
* @see File#listFiles()
812+
*/
813+
protected File[] listDirectory(File dir) {
814+
File[] files = dir.listFiles();
815+
if (files == null) {
816+
if (logger.isWarnEnabled()) {
817+
logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
818+
}
819+
return new File[0];
820+
}
821+
Arrays.sort(files, Comparator.comparing(File::getName));
822+
return files;
823+
}
824+
813825

814826
/**
815827
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.

0 commit comments

Comments
 (0)