src/java.base/share/classes/java/io/RandomAccessFile.java
Print this page
rev 11079 : 8025619.01
rev 11080 : 8025619.02
rev 11081 : 8025619.03
*** 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
*** 57,77 ****
*/
public class RandomAccessFile implements DataOutput, DataInput, Closeable {
private FileDescriptor fd;
! private FileChannel channel = null;
private boolean rw;
/**
* 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;
--- 58,77 ----
*/
public class RandomAccessFile implements DataOutput, DataInput, Closeable {
private FileDescriptor fd;
! private volatile FileChannel channel;
private boolean rw;
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
! private final 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;
*** 274,291 ****
* @return the file channel associated with this file
*
* @since 1.4
* @spec JSR-51
*/
! public final FileChannel getChannel() {
synchronized (this) {
! if (channel == null) {
! channel = FileChannelImpl.open(fd, path, true, rw, this);
}
- return channel;
}
}
/**
* Opens a file and returns the file descriptor. The file is
* opened in read-write mode if the O_RDWR bit in {@code mode}
* is true, else the file is opened as read-only.
--- 274,302 ----
* @return the file channel associated with this file
*
* @since 1.4
* @spec JSR-51
*/
! public FileChannel getChannel() {
! FileChannel fc = this.channel;
! if (fc == null) {
synchronized (this) {
! fc = this.channel;
! if (fc == null) {
! this.channel = fc = FileChannelImpl.open(fd, path, true, rw, this);
! if (closed.get()) {
! try {
! fc.close();
! } catch (IOException ioe) {
! throw new InternalError(ioe); // should not happen
}
}
}
+ }
+ }
+ return fc;
+ }
/**
* Opens a file and returns the file descriptor. The file is
* opened in read-write mode if the O_RDWR bit in {@code mode}
* is true, else the file is opened as read-only.
*** 602,619 ****
*
* @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() {
public void close() throws IOException {
close0();
--- 613,630 ----
*
* @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;
}
!
! FileChannel fc = channel;
! if (fc != null) {
! fc.close();
}
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();