From 5b0af1a80bb4d2a81cda7e26a6ad0db43e679519 Mon Sep 17 00:00:00 2001 From: Alan Bateman Date: Thu, 19 Jan 2023 06:59:38 +0000 Subject: [PATCH] 8208077: File.listRoots performance degradation Reviewed-by: lancea, bpb --- .../classes/java/io/WinNTFileSystem.java | 4 +-- test/jdk/java/io/File/ListRoots.java | 36 +++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/java.base/windows/classes/java/io/WinNTFileSystem.java b/src/java.base/windows/classes/java/io/WinNTFileSystem.java index 6369759da74..6da8dfa7b4f 100644 --- a/src/java.base/windows/classes/java/io/WinNTFileSystem.java +++ b/src/java.base/windows/classes/java/io/WinNTFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -745,7 +745,7 @@ class WinNTFileSystem extends FileSystem { .valueOf(new long[] {listRoots0()}) .stream() .mapToObj(i -> new File((char)('A' + i) + ":" + slash)) - .filter(f -> access(f.getPath()) && f.exists()) + .filter(f -> access(f.getPath())) .toArray(File[]::new); } private static native int listRoots0(); diff --git a/test/jdk/java/io/File/ListRoots.java b/test/jdk/java/io/File/ListRoots.java index 51dd1913bd1..7b14b2f3b08 100644 --- a/test/jdk/java/io/File/ListRoots.java +++ b/test/jdk/java/io/File/ListRoots.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,11 +23,17 @@ /* @test @bug 4071322 - @summary Basic test for listRoots method + @summary Basic test for File.listRoots method */ -import java.io.*; - +import java.io.File; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; public class ListRoots { @@ -37,15 +43,25 @@ public class ListRoots { System.out.println(i + ": " + rs[i]); } - File f = new File(System.getProperty("test.src", "."), - "ListRoots.java"); + File f = new File(System.getProperty("test.src", "."), "ListRoots.java"); String cp = f.getCanonicalPath(); - for (int i = 0; i < rs.length; i++) { - if (cp.startsWith(rs[i].getPath())) break; - if (i == rs.length - 1) - throw new Exception(cp + " does not have a recognized root"); + boolean found = Stream.of(rs) + .map(File::getPath) + .anyMatch(p -> cp.startsWith(p)); + if (!found) { + throw new RuntimeException(cp + " does not have a recognized root"); } + // the list of roots should match FileSystem::getRootDirectories + Set roots1 = Stream.of(rs).collect(Collectors.toSet()); + FileSystem fs = FileSystems.getDefault(); + Set roots2 = StreamSupport.stream(fs.getRootDirectories().spliterator(), false) + .map(Path::toFile) + .collect(Collectors.toSet()); + if (!roots1.equals(roots2)) { + System.out.println(roots2); + throw new RuntimeException("Does not match FileSystem::getRootDirectories"); + } } }