Skip to content

Commit 8dbde86

Browse files
author
duke
committed
Backport be0161a
1 parent 5129887 commit 8dbde86

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

src/java.base/share/classes/java/io/File.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,15 +1134,9 @@ public File[] listFiles() {
11341134
if (ss == null) return null;
11351135
int n = ss.length;
11361136
File[] fs = new File[n];
1137-
if (path.isEmpty()) {
1138-
for (int i = 0; i < n; i++) {
1139-
fs[i] = new File(ss[i]);
1140-
}
1141-
} else {
1142-
for (int i = 0; i < n; i++) {
1143-
fs[i] = new File(ss[i], this);
1144-
}
1145-
}
1137+
boolean isEmpty = path.isEmpty();
1138+
for (int i = 0; i < n; i++)
1139+
fs[i] = isEmpty ? new File(ss[i]) : new File(ss[i], this);
11461140
return fs;
11471141
}
11481142

@@ -1175,9 +1169,10 @@ public File[] listFiles(FilenameFilter filter) {
11751169
String[] ss = normalizedList();
11761170
if (ss == null) return null;
11771171
ArrayList<File> files = new ArrayList<>();
1172+
boolean isEmpty = path.isEmpty();
11781173
for (String s : ss)
11791174
if ((filter == null) || filter.accept(this, s))
1180-
files.add(new File(s, this));
1175+
files.add(isEmpty ? new File(s) : new File(s, this));
11811176
return files.toArray(new File[files.size()]);
11821177
}
11831178

@@ -1208,8 +1203,9 @@ public File[] listFiles(FileFilter filter) {
12081203
String[] ss = normalizedList();
12091204
if (ss == null) return null;
12101205
ArrayList<File> files = new ArrayList<>();
1206+
boolean isEmpty = path.isEmpty();
12111207
for (String s : ss) {
1212-
File f = new File(s, this);
1208+
File f = isEmpty ? new File(s) : new File(s, this);
12131209
if ((filter == null) || filter.accept(f))
12141210
files.add(f);
12151211
}

test/jdk/java/io/File/EmptyPath.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@
2222
*/
2323

2424
/* @test
25-
* @bug 4842706 8024695 8361587
25+
* @bug 4842706 8024695 8361587 8362429
2626
* @summary Test some file operations with empty path
2727
* @run junit EmptyPath
2828
*/
2929

3030
import java.io.File;
31+
import java.io.FileFilter;
3132
import java.io.FileInputStream;
33+
import java.io.FilenameFilter;
3234
import java.io.FileNotFoundException;
3335
import java.io.IOException;
36+
import java.net.MalformedURLException;
3437
import java.nio.file.Files;
3538
import java.nio.file.FileStore;
3639
import java.nio.file.Path;
3740
import java.util.Arrays;
3841
import java.util.HashSet;
3942
import java.util.List;
4043
import java.util.Set;
44+
import java.util.function.Function;
4145
import java.util.stream.Collectors;
4246

4347
import org.junit.jupiter.api.BeforeAll;
@@ -211,7 +215,15 @@ public void length() throws IOException {
211215

212216
@Test
213217
public void list() throws IOException {
214-
String[] files = f.list();
218+
list(f.list());
219+
}
220+
221+
@Test
222+
public void listFilenameFilter() throws IOException {
223+
list(f.list((FilenameFilter)null));
224+
}
225+
226+
private void list(String[] files) throws IOException {
215227
assertNotNull(files);
216228
Set<String> ioSet = new HashSet(Arrays.asList(files));
217229
Set<String> nioSet = new HashSet();
@@ -221,11 +233,42 @@ public void list() throws IOException {
221233

222234
@Test
223235
public void listFiles() throws IOException {
224-
File child = new File(f.getAbsoluteFile(), "child");
236+
listFiles(x -> x.listFiles());
237+
}
238+
239+
@Test
240+
public void listFilesFileFilter() throws IOException {
241+
FileFilter ff = new FileFilter() {
242+
public boolean accept(File pathname) { return true; }
243+
};
244+
listFiles(x -> x.listFiles(ff));
245+
}
246+
247+
@Test
248+
public void listFilesNullFileFilter() throws IOException {
249+
listFiles(x -> x.listFiles((FileFilter)null));
250+
}
251+
252+
@Test
253+
public void listFilesFilenameFilter() throws IOException {
254+
FilenameFilter fnf = new FilenameFilter() {
255+
public boolean accept(File dir, String name) { return true; }
256+
};
257+
listFiles(x -> x.listFiles(fnf));
258+
}
259+
260+
@Test
261+
public void listFilesNullFilenameFilter() throws IOException {
262+
listFiles(x -> x.listFiles((FilenameFilter)null));
263+
}
264+
265+
private void listFiles(Function<File,File[]> func) throws IOException {
266+
String childName = "child" + System.nanoTime();
267+
File child = new File(f.getAbsoluteFile(), childName);
225268
assertTrue(child.createNewFile());
226269
child.deleteOnExit();
227270

228-
File[] files = f.listFiles();
271+
File[] files = func.apply(f);
229272
for (File file : files)
230273
assertEquals(-1, f.toString().indexOf(File.separatorChar));
231274

@@ -348,4 +391,9 @@ public String toString() {
348391
public void toURI() {
349392
assertEquals(f.toPath().toUri(), f.toURI());
350393
}
394+
395+
@Test
396+
public void toURL() throws MalformedURLException {
397+
assertEquals(f.toPath().toUri().toURL(), f.toURL());
398+
}
351399
}

0 commit comments

Comments
 (0)