src/java.base/share/classes/java/io/RandomAccessFile.java
Print this page
rev 11067 : 8025619.01
*** 24,33 ****
--- 24,34 ----
*/
package java.io;
import java.nio.channels.FileChannel;
+ import java.util.concurrent.atomic.AtomicBoolean;
import sun.nio.ch.FileChannelImpl;
/**
* Instances of this class support both reading and writing to a
*** 66,77 ****
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
! private Object closeLock = new Object();
! private volatile boolean closed = false;
private static final int O_RDONLY = 1;
private static final int O_RDWR = 2;
private static final int O_SYNC = 4;
private static final int O_DSYNC = 8;
--- 67,77 ----
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
! private AtomicBoolean closed = new AtomicBoolean(false);
private static final int O_RDONLY = 1;
private static final int O_RDWR = 2;
private static final int O_SYNC = 4;
private static final int O_DSYNC = 8;
*** 278,287 ****
--- 278,294 ----
*/
public final FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, path, true, rw, this);
+ if (closed.get()) {
+ try {
+ channel.close();
+ } catch (IOException ioe) {
+ throw new InternalError(ioe); // should not happen
+ }
+ }
}
return channel;
}
}
*** 602,617 ****
*
* @revised 1.4
* @spec JSR-51
*/
public void close() throws IOException {
! synchronized (closeLock) {
! if (closed) {
return;
}
! closed = true;
! }
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {
--- 609,623 ----
*
* @revised 1.4
* @spec JSR-51
*/
public void close() throws IOException {
! if (!closed.compareAndSet(false, true)) {
! // if compareAndSet() returns false closed was already true
return;
}
!
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {