Skip to content

Commit 5f4b56a

Browse files
committed
FileHandle: improve behavior of close() method
This change does two things: * It synchronizes close(), so that the RAF cannot be in the process of initializing while the close() operation is happening. * It guards all operations against an already-closed state, such that an IOException will be thrown if the handle has already been closed.
1 parent 5204925 commit 5f4b56a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/main/java/org/scijava/io/handle/FileHandle.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class FileHandle extends AbstractDataHandle<FileLocation> {
5555
/** The mode of the {@link RandomAccessFile}. */
5656
private String mode = "rw";
5757

58+
/** True iff the {@link #close()} has already been called. */
59+
private boolean closed;
60+
5861
// -- FileHandle methods --
5962

6063
/** Gets the random access file object backing this FileHandle. */
@@ -289,8 +292,9 @@ public void writeUTF(final String str) throws IOException {
289292
// -- Closeable methods --
290293

291294
@Override
292-
public void close() throws IOException {
295+
public synchronized void close() throws IOException {
293296
if (raf != null) raf().close();
297+
closed = true;
294298
}
295299

296300
// -- Typed methods --
@@ -308,6 +312,7 @@ private RandomAccessFile raf() throws IOException {
308312
}
309313

310314
private synchronized void initRAF() throws IOException {
315+
if (closed) throw new IOException("Handle already closed");
311316
if (raf != null) return;
312317
raf = new RandomAccessFile(get().getFile(), getMode());
313318
}

0 commit comments

Comments
 (0)