Add best-effort variant of File.getCanonicalFile()
See https://git.eclipse.org/r/#/c/58405/. Change-Id: I097c4b1369754f59137eb8848a680c61b06510ad Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
parent
80c7884ea6
commit
d9e9015a00
|
@ -735,4 +735,29 @@ public static String normalize(String name) {
|
|||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Best-effort variation of {@link File#getCanonicalFile()} returning the
|
||||
* input file if the file cannot be canonicalized instead of throwing
|
||||
* {@link IOException}.
|
||||
*
|
||||
* @param file
|
||||
* to be canonicalized; may be {@code null}
|
||||
* @return canonicalized file, or the unchanged input file if
|
||||
* canonicalization failed or if {@code file == null}
|
||||
* @throws SecurityException
|
||||
* if {@link File#getCanonicalFile()} throws one
|
||||
* @since 4.2
|
||||
*/
|
||||
public static File canonicalize(File file) {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return file.getCanonicalFile();
|
||||
} catch (IOException e) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue