mirror of
https://github.com/corda/corda.git
synced 2025-01-03 19:54:13 +00:00
fix bugs in File.getParent and listFiles
getParent should return the same value regardless of whether it ends in a file separator, and listFiles should return null for non-directories.
This commit is contained in:
parent
c17710d2b0
commit
836fc21106
@ -148,9 +148,13 @@ public class File implements Serializable {
|
||||
}
|
||||
|
||||
public String getParent() {
|
||||
int index = path.lastIndexOf(FileSeparator);
|
||||
String p = path;
|
||||
while (p.endsWith(FileSeparator)) {
|
||||
p = p.substring(0, p.length() - 1);
|
||||
}
|
||||
int index = p.lastIndexOf(FileSeparator);
|
||||
if (index >= 0) {
|
||||
return path.substring(0, index);
|
||||
return p.substring(0, index);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -239,11 +243,15 @@ public class File implements Serializable {
|
||||
|
||||
public File[] listFiles(FilenameFilter filter) {
|
||||
String[] list = list(filter);
|
||||
File[] result = new File[list.length];
|
||||
for (int i = 0; i < list.length; ++i) {
|
||||
result[i] = new File(this, list[i]);
|
||||
if (list != null) {
|
||||
File[] result = new File[list.length];
|
||||
for (int i = 0; i < list.length; ++i) {
|
||||
result[i] = new File(this, list[i]);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String[] list() {
|
||||
@ -254,22 +262,26 @@ public class File implements Serializable {
|
||||
long handle = 0;
|
||||
try {
|
||||
handle = openDir(path);
|
||||
Pair list = null;
|
||||
int count = 0;
|
||||
for (String s = readDir(handle); s != null; s = readDir(handle)) {
|
||||
if (filter == null || filter.accept(this, s)) {
|
||||
list = new Pair(s, list);
|
||||
++ count;
|
||||
if (handle != 0) {
|
||||
Pair list = null;
|
||||
int count = 0;
|
||||
for (String s = readDir(handle); s != null; s = readDir(handle)) {
|
||||
if (filter == null || filter.accept(this, s)) {
|
||||
list = new Pair(s, list);
|
||||
++ count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String[] result = new String[count];
|
||||
for (int i = count - 1; i >= 0; --i) {
|
||||
result[i] = list.value;
|
||||
list = list.next;
|
||||
}
|
||||
String[] result = new String[count];
|
||||
for (int i = count - 1; i >= 0; --i) {
|
||||
result[i] = list.value;
|
||||
list = list.next;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} finally {
|
||||
if (handle != 0) {
|
||||
closeDir(handle);
|
||||
|
@ -74,6 +74,12 @@ public class Files {
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
|
||||
expect(new File("foo/bar").getParent().equals("foo"));
|
||||
expect(new File("foo/bar/").getParent().equals("foo"));
|
||||
expect(new File("foo/bar//").getParent().equals("foo"));
|
||||
|
||||
expect(new File("foo/nonexistent-directory").listFiles() == null);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user