src/java.base/share/classes/java/io/FileOutputStream.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.misc.SharedSecrets;
import sun.misc.JavaIOFileDescriptorAccess;
import sun.nio.ch.FileChannelImpl;
*** 66,85 ****
private final FileDescriptor fd;
/**
* The associated channel, initialized lazily.
*/
! private FileChannel channel;
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
! private final Object closeLock = new Object();
! private volatile boolean closed = false;
/**
* Creates a file output stream to write to the file with the
* specified name. A new <code>FileDescriptor</code> object is
* created to represent this file connection.
--- 67,85 ----
private final FileDescriptor fd;
/**
* The associated channel, initialized lazily.
*/
! private volatile FileChannel channel;
/**
* 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);
/**
* Creates a file output stream to write to the file with the
* specified name. A new <code>FileDescriptor</code> object is
* created to represent this file connection.
*** 339,357 ****
*
* @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();
--- 339,356 ----
*
* @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();
*** 392,407 ****
*
* @since 1.4
* @spec JSR-51
*/
public FileChannel getChannel() {
synchronized (this) {
! if (channel == null) {
! channel = FileChannelImpl.open(fd, path, false, true, this);
}
- return channel;
}
}
/**
* Cleans up the connection to the file, and ensures that the
* <code>close</code> method of this file output stream is
--- 391,417 ----
*
* @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, false, true, this);
! if (closed.get()) {
! try {
! fc.close();
! } catch (IOException ioe) {
! throw new InternalError(ioe); // should not happen
! }
! }
! }
}
}
+ return fc;
}
/**
* Cleans up the connection to the file, and ensures that the
* <code>close</code> method of this file output stream is