Skip to content

Commit 8603798

Browse files
committed
javaFileObject() code dedupe
1 parent 3c3bc02 commit 8603798

26 files changed

+105
-133
lines changed

+stdlib/canonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
% like C++ filesystem weakly_canonical()
5252

5353
if use_java
54-
c = java.io.File(c).getCanonicalPath();
54+
c = javaFileObject(c).getCanonicalPath();
5555
else
5656
c = stdlib.normalize(c, use_java);
5757
end

+stdlib/diskfree.m

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@
1010
d (1,1) string
1111
end
1212

13-
if stdlib.isoctave()
14-
o = javaObject("java.io.File", d);
15-
else
16-
o = java.io.File(d);
17-
end
18-
19-
f = o.getUsableSpace();
13+
f = javaFileObject(d).getUsableSpace();
2014

2115
end
2216

+stdlib/exists.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
use_java (1,1) logical = false
1717
end
1818

19-
if stdlib.isoctave()
20-
ok = javaObject("java.io.File", p).exists();
21-
elseif use_java
22-
19+
if use_java
2320
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#exists(java.nio.file.Path,java.nio.file.LinkOption...)
2421
% this takes 2x longer than java.io.File.exists()
2522
% opt = java.nio.file.LinkOption.values;
2623
% ok = java.nio.file.Files.exists(java.io.File(p).toPath(), opt);
2724

2825
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#exists()
2926
% takes 2x longer than native Matlab isfile || isfolder
30-
ok = java.io.File(p).exists();
27+
ok = javaFileObject(p).exists();
3128
else
3229
ok = isfile(p) || isfolder(p);
3330
end

+stdlib/file_size.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
function s = file_size(p, use_java)
44
arguments
55
p (1,1) string {mustBeFile}
6-
use_java (1,1) logical = false
6+
use_java (1,1) logical = true
77
end
88

9-
if stdlib.isoctave()
10-
s = javaObject("java.io.File", p).length();
11-
elseif use_java
12-
% several percent slower than native Matlab
13-
s = java.io.File(p).length;
9+
if use_java
10+
s = javaFileObject(p).length();
1411
else
1512
s = dir(p);
1613
if isempty(s)

+stdlib/filesystem_type.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
p (1,1) string
88
end
99

10+
op = javaFileObject(p).toPath();
11+
1012
if stdlib.isoctave()
11-
op = javaObject("java.io.File", p).toPath();
1213
t = javaMethod("getFileStore", "java.nio.file.Files", op).type;
1314
else
14-
op = java.io.File(p).toPath();
1515
t = string(java.nio.file.Files.getFileStore(op).type);
1616
end
1717

+stdlib/get_modtime.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
end
77

88
if stdlib.exists(p)
9-
if stdlib.isoctave()
10-
o = javaObject("java.io.File", p);
11-
else
12-
o = java.io.File(p);
13-
end
14-
utc = o.lastModified() / 1000;
9+
10+
utc = javaFileObject(p).lastModified() / 1000;
1511

1612
try
1713
t = datetime(utc, "ConvertFrom", "PosixTime");

+stdlib/get_owner.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#getOwner(java.nio.file.Path,java.nio.file.LinkOption...)
99
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/LinkOption.html
1010

11+
op = javaFileObject(p).toPath();
12+
1113
if stdlib.isoctave()
1214
opt = javaMethod("values", "java.nio.file.LinkOption");
13-
op = javaObject("java.io.File", p).toPath();
1415
n = javaMethod("getOwner", "java.nio.file.Files", op, opt).toString();
1516
else
1617
opt = java.nio.file.LinkOption.values;
17-
n = string(java.nio.file.Files.getOwner(java.io.File(p).toPath(), opt));
18+
n = string(java.nio.file.Files.getOwner(op, opt));
1819
end
1920

2021
end

+stdlib/hard_link_count.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
return
1515
end
1616

17+
op = javaFileObject(p).toPath();
18+
1719
if stdlib.isoctave()
1820
opt = javaMethod("values", "java.nio.file.LinkOption");
19-
op = javaObject("java.io.File", p).toPath();
2021
c = javaMethod("getAttribute", "java.nio.file.Files", op, "unix:nlink", opt);
2122
else
2223
opt = java.nio.file.LinkOption.values;
23-
op = java.io.File(p).toPath();
2424
c = java.nio.file.Files.getAttribute(op, "unix:nlink", opt);
2525
end
2626

+stdlib/is_absolute.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
use_java (1,1) logical = false
1010
end
1111

12-
if stdlib.isoctave()
13-
% not is_absolute_filename() because this is a stricter check for "c:" false
14-
isabs = javaObject("java.io.File", p).isAbsolute();
15-
elseif use_java
12+
% not Octave is_absolute_filename() because this is a stricter check for "c:" false
13+
14+
if use_java
1615
% java is about 5x to 10x slower than intrinsic
1716
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#isAbsolute()
18-
isabs = java.io.File(p).toPath().isAbsolute();
17+
isabs = javaFileObject(p).toPath().isAbsolute();
18+
1919
else
20+
2021
L = strlength(p);
2122
if ispc
2223
s = "";
@@ -27,6 +28,7 @@
2728
else
2829
isabs = L >= 1 && startsWith(p, "/");
2930
end
31+
3032
end
3133

3234
end

+stdlib/is_exe.m

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use_java (1,1) logical = true
1010
end
1111

12+
13+
ok = false;
14+
1215
if ~isfile(p)
13-
ok = false;
1416
return
1517
end
1618

@@ -20,26 +22,13 @@
2022
% more complicated
2123
% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.canonical(p)).toPath());
2224

23-
try
24-
ok = java.io.File(p).canExecute();
25-
catch e
26-
if strcmp(e.identifier, "Octave:undefined-function")
27-
ok = javaObject("java.io.File", p).canExecute();
28-
else
29-
rethrow(e);
30-
end
31-
end
25+
ok = javaFileObject(p).canExecute();
3226

3327
else
3428

35-
if ~strlength(p)
36-
ok = false;
37-
return
38-
end
39-
40-
[status, v] = fileattrib(p);
29+
[status, v] = fileattrib(p);
4130

42-
ok = status ~= 0 && (v.UserExecute || (~isnan(v.GroupExecute) && v.GroupExecute) || (~isnan(v.OtherExecute) && v.OtherExecute));
31+
ok = status ~= 0 && (v.UserExecute || (~isnan(v.GroupExecute) && v.GroupExecute) || (~isnan(v.OtherExecute) && v.OtherExecute));
4332

4433
end
4534

0 commit comments

Comments
 (0)