Skip to content

Commit 1160213

Browse files
committed
FileUtils: improve listContents implementation
This change adds support for two optional flags: recurse and filesOnly. If the recurse flag is true, directory contents are computed recursively. If the filesOnly flag is true, directories themselves are excluded from the resulting collection. The previous listContents and appendContents method signatures have been retained, with recurse=true and filesOnly=true, which was the default intended behavior. This change also fixes a bug in the implementation: directories were being erroneously included in the result when listing inside a JAR file. (The unit test did not catch it because it did not test with a JAR that included any directory entries, only files in subdirectories.)
1 parent 6dbf2b8 commit 1160213

1 file changed

Lines changed: 52 additions & 3 deletions

File tree

‎src/main/java/org/scijava/util/FileUtils.java‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,27 @@ else if (file.isDirectory()) {
435435
*
436436
* @param directory The directory whose contents should be listed.
437437
* @return A collection of {@link URL}s representing the directory's contents.
438+
* @see #listContents(URL, boolean, boolean)
438439
*/
439440
public static Collection<URL> listContents(final URL directory) {
440-
return appendContents(new ArrayList<URL>(), directory);
441+
return listContents(directory, true, true);
442+
}
443+
444+
/**
445+
* Lists all contents of the referenced directory. Supported protocols include
446+
* {@code file} and {@code jar}.
447+
*
448+
* @param directory The directory whose contents should be listed.
449+
* @param recurse Whether to list contents recursively, as opposed to only the
450+
* directory's direct contents.
451+
* @param filesOnly Whether to exclude directories in the resulting collection
452+
* of contents.
453+
* @return A collection of {@link URL}s representing the directory's contents.
454+
*/
455+
public static Collection<URL> listContents(final URL directory,
456+
final boolean recurse, final boolean filesOnly)
457+
{
458+
return appendContents(new ArrayList<URL>(), directory, recurse, filesOnly);
441459
}
442460

443461
/**
@@ -448,9 +466,28 @@ public static Collection<URL> listContents(final URL directory) {
448466
* @param result The collection to which contents should be added.
449467
* @param directory The directory whose contents should be listed.
450468
* @return A collection of {@link URL}s representing the directory's contents.
469+
* @see #appendContents(Collection, URL, boolean, boolean)
451470
*/
452471
public static Collection<URL> appendContents(final Collection<URL> result,
453472
final URL directory)
473+
{
474+
return appendContents(result, directory, true, true);
475+
}
476+
477+
/**
478+
* Add contents from the referenced directory to an existing collection.
479+
* Supported protocols include {@code file} and {@code jar}.
480+
*
481+
* @param result The collection to which contents should be added.
482+
* @param directory The directory whose contents should be listed.
483+
* @param recurse Whether to append contents recursively, as opposed to only
484+
* the directory's direct contents.
485+
* @param filesOnly Whether to exclude directories in the resulting collection
486+
* of contents.
487+
* @return A collection of {@link URL}s representing the directory's contents.
488+
*/
489+
public static Collection<URL> appendContents(final Collection<URL> result,
490+
final URL directory, final boolean recurse, final boolean filesOnly)
454491
{
455492
if (directory == null) return result; // nothing to append
456493
final String protocol = directory.getProtocol();
@@ -460,10 +497,10 @@ public static Collection<URL> appendContents(final Collection<URL> result,
460497
if (list != null) {
461498
for (final File file : list) {
462499
try {
463-
if (file.isFile()) {
500+
if (!filesOnly || file.isFile()) {
464501
result.add(file.toURI().toURL());
465502
}
466-
else if (file.isDirectory()) {
503+
if (recurse && file.isDirectory()) {
467504
appendContents(result, file.toURI().toURL());
468505
}
469506
}
@@ -490,6 +527,18 @@ else if (protocol.equals("jar")) {
490527
if (urlEncoded.length() > prefix.length() && // omit directory itself
491528
urlEncoded.startsWith(prefix))
492529
{
530+
if (filesOnly && urlEncoded.endsWith("/")) {
531+
// URL is directory; exclude it
532+
continue;
533+
}
534+
if (!recurse) {
535+
// check whether this URL is a *direct* child of the directory
536+
final int slash = urlEncoded.indexOf("/", prefix.length());
537+
if (slash >= 0 && slash != urlEncoded.length() - 1) {
538+
// not a direct child
539+
continue;
540+
}
541+
}
493542
result.add(new URL(baseURL + urlEncoded));
494543
}
495544
}

0 commit comments

Comments
 (0)