8182710: File.listRoots() always returns the root of CD drive
Summary: Include only logical drives with an extant filesystem location
Reviewed-by: clanger, rriggs, plevart, tvaleev
--- a/jdk/src/java.base/windows/classes/java/io/WinNTFileSystem.java Thu Jun 29 12:59:55 2017 +0200
+++ b/jdk/src/java.base/windows/classes/java/io/WinNTFileSystem.java Thu Jun 29 13:40:38 2017 -0700
@@ -27,6 +27,7 @@
import java.io.File;
import java.nio.file.Path;
+import java.util.BitSet;
import java.util.Locale;
import java.util.Properties;
import sun.security.action.GetPropertyAction;
@@ -586,24 +587,12 @@
@Override
public File[] listRoots() {
- int ds = listRoots0();
- int n = 0;
- for (int i = 0; i < 26; i++) {
- if (((ds >> i) & 1) != 0) {
- if (!access((char)('A' + i) + ":" + slash))
- ds &= ~(1 << i);
- else
- n++;
- }
- }
- File[] fs = new File[n];
- int j = 0;
- char slash = this.slash;
- for (int i = 0; i < 26; i++) {
- if (((ds >> i) & 1) != 0)
- fs[j++] = new File((char)('A' + i) + ":" + slash);
- }
- return fs;
+ return BitSet
+ .valueOf(new long[] {listRoots0()})
+ .stream()
+ .mapToObj(i -> new File((char)('A' + i) + ":" + slash))
+ .filter(f -> access(f.getPath()) && f.exists())
+ .toArray(File[]::new);
}
private static native int listRoots0();