File tree 1 file changed +26
-1
lines changed
src/main/java/org/scijava/io/handle
1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change 29
29
30
30
package org .scijava .io .handle ;
31
31
32
+ import java .io .File ;
32
33
import java .io .IOException ;
33
34
import java .io .RandomAccessFile ;
35
+ import java .nio .file .Files ;
36
+ import java .nio .file .Path ;
34
37
import java .util .Date ;
35
38
36
39
import org .scijava .io .location .FileLocation ;
@@ -50,7 +53,7 @@ public class FileHandle extends AbstractDataHandle<FileLocation> {
50
53
private RandomAccessFile raf ;
51
54
52
55
/** The mode of the {@link RandomAccessFile}. */
53
- private String mode = "rw" ;
56
+ private String mode ;
54
57
55
58
/** True iff the {@link #close()} has already been called. */
56
59
private boolean closed ;
@@ -232,6 +235,28 @@ public synchronized void close() throws IOException {
232
235
closed = true ;
233
236
}
234
237
238
+ // -- WrapperPlugin methods --
239
+
240
+ @ Override
241
+ public void set (FileLocation loc ) {
242
+ super .set (loc );
243
+
244
+ // Infer the initial mode based on file existence + permissions.
245
+ final File file = loc .getFile ();
246
+ String mode ;
247
+ if (file .exists ()) {
248
+ final Path path = loc .getFile ().toPath ();
249
+ mode = "" ;
250
+ if (Files .isReadable (path )) mode += "r" ;
251
+ if (Files .isWritable (path )) mode += "w" ;
252
+ }
253
+ else {
254
+ // Non-existent file; assume the intent is to create it.
255
+ mode = "rw" ;
256
+ }
257
+ setMode (mode );
258
+ }
259
+
235
260
// -- Typed methods --
236
261
237
262
@ Override
You can’t perform that action at this time.
0 commit comments