|
5 | 5 | function ok = is_readable(file, use_java) |
6 | 6 | arguments |
7 | 7 | file (1,1) string |
8 | | - use_java (1,1) logical = false |
| 8 | + use_java (1,1) logical = true |
9 | 9 | end |
10 | 10 |
|
| 11 | +ok = false; |
| 12 | +if ~stdlib.exists(file, use_java), return, end |
| 13 | +% exists() check speeds up by factor of 50x on macOS for Java or non-Java |
| 14 | + |
11 | 15 | if use_java |
12 | | - % java is about 10x slower than fileattrib |
| 16 | + % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path) |
| 17 | + % java.nio.file.Files is about 10x slower than fileattrib |
13 | 18 | % needs absolute() |
14 | | - file = stdlib.absolute(file, "", false, use_java); |
| 19 | + % file = stdlib.absolute(file, "", false, true); |
| 20 | + % ok = java.nio.file.Files.isReadable(java.io.File(file).toPath()); |
| 21 | +try |
| 22 | + ok = java.io.File(file).canRead(); |
| 23 | +catch e |
| 24 | + if strcmp(e.identifier, "Octave:undefined-function") |
| 25 | + ok = javaObject("java.io.File", file).canRead(); |
| 26 | + else |
| 27 | + rethrow(e); |
| 28 | + end |
| 29 | +end |
15 | 30 |
|
16 | | - % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path) |
17 | | - %ok = java.nio.file.Files.isReadable(java.io.File(file).toPath()); |
| 31 | +else % use_java |
18 | 32 |
|
19 | | - % java.io.File().canRead() is about twice as fast as |
20 | | - % java.nio.file.Files.isReadable() |
21 | | - ok = java.io.File(file).canRead(); |
22 | | -else |
23 | | - [status, v] = fileattrib(file); |
| 33 | +[status, v] = fileattrib(file); |
| 34 | + |
| 35 | +ok = status ~= 0 && (v.UserRead || (~isnan(v.GroupRead) && v.GroupRead) || (~isnan(v.OtherRead) && v.OtherRead)); |
24 | 36 |
|
25 | | - ok = status ~= 0 && (v.UserRead || (~isnan(v.GroupRead) && v.GroupRead) || (~isnan(v.OtherRead) && v.OtherRead)); |
26 | 37 | end |
27 | 38 |
|
28 | 39 | end |
|
0 commit comments