Skip to content

Commit 2c7a9cf

Browse files
committed
is_readable: java optional, much faster
1 parent 73c63d8 commit 2c7a9cf

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

+stdlib/+java/is_readable.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function y = is_readable(p)
2+
3+
y = javaObject("java.io.File", p).canRead();
4+
5+
end

+stdlib/+native/is_readable.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function y = is_readable(p)
2+
3+
y = false;
4+
5+
if ~stdlib.exists(p), return, end
6+
7+
if ~isMATLABReleaseOlderThan('R2025a')
8+
9+
props = "Readable";
10+
if isunix
11+
props = [props, "GroupRead", "OtherRead"];
12+
end
13+
14+
t = getPermissions(filePermissions(p), props);
15+
y = t.Readable;
16+
17+
else
18+
19+
a = file_attributes_legacy(p);
20+
y = a.UserRead || a.GroupRead || a.OtherRead;
21+
22+
end
23+
24+
end

+stdlib/is_readable.m

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,16 @@
55
%% Outputs
66
% ok: logical array of the same size as p, true if file is readable
77

8-
function ok = is_readable(p)
8+
function y = is_readable(p)
99
arguments
10-
p string
10+
p {mustBeTextScalar}
1111
end
1212

13-
ok(size(p)) = false;
14-
15-
i = stdlib.exists(p);
16-
17-
if ~any(i), return, end
18-
19-
if ~isMATLABReleaseOlderThan('R2025a')
20-
21-
props = "Readable";
22-
if isunix
23-
props = [props, "GroupRead", "OtherRead"];
24-
end
25-
26-
t = getPermissions(filePermissions(p(i)), props);
27-
ok(i) = any(t{:,:}, 2);
28-
13+
if stdlib.has_java()
14+
y = stdlib.java.is_readable(p);
2915
else
30-
31-
a = file_attributes_legacy(p);
32-
ok = a.UserRead || a.GroupRead || a.OtherRead;
33-
16+
y = stdlib.native.is_readable(p);
3417
end
3518

19+
3620
end

0 commit comments

Comments
 (0)