# HG changeset patch # User bpb # Date 1417813786 28800 # Fri Dec 05 13:09:46 2014 -0800 # Node ID 2af4f2b969a41753550ae22ad93a1574f8d87954 # Parent b3620b8c9b47fb331e80470431432af15a043c15 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed Summary: FileKey.create() should throw an IOException directly instead of wrapping it in an Error. Reviewed-by: TBD diff --git a/src/java.base/share/classes/java/io/FileInputStream.java b/src/java.base/share/classes/java/io/FileInputStream.java --- a/src/java.base/share/classes/java/io/FileInputStream.java +++ b/src/java.base/share/classes/java/io/FileInputStream.java @@ -367,6 +367,13 @@ synchronized (this) { if (channel == null) { channel = FileChannelImpl.open(fd, path, true, false, this); + if (closed) { + try { + channel.close(); + } catch (IOException ioe) { + throw new InternalError(ioe); // should not happen + } + } } return channel; } diff --git a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java --- a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java @@ -109,6 +109,9 @@ // -- Standard channel operations -- protected void implCloseChannel() throws IOException { + if (!fd.valid()) + return; // nothing to do + // Release and invalidate any locks that we still hold if (fileLockTable != null) { for (FileLock fl: fileLockTable.removeAll()) { diff --git a/src/java.base/unix/classes/sun/nio/ch/FileKey.java b/src/java.base/unix/classes/sun/nio/ch/FileKey.java --- a/src/java.base/unix/classes/sun/nio/ch/FileKey.java +++ b/src/java.base/unix/classes/sun/nio/ch/FileKey.java @@ -38,13 +38,9 @@ private FileKey() { } - public static FileKey create(FileDescriptor fd) { + public static FileKey create(FileDescriptor fd) throws IOException { FileKey fk = new FileKey(); - try { - fk.init(fd); - } catch (IOException ioe) { - throw new Error(ioe); - } + fk.init(fd); return fk; } diff --git a/src/java.base/windows/classes/sun/nio/ch/FileKey.java b/src/java.base/windows/classes/sun/nio/ch/FileKey.java --- a/src/java.base/windows/classes/sun/nio/ch/FileKey.java +++ b/src/java.base/windows/classes/sun/nio/ch/FileKey.java @@ -39,13 +39,9 @@ private FileKey() { } - public static FileKey create(FileDescriptor fd) { + public static FileKey create(FileDescriptor fd) throws IOException { FileKey fk = new FileKey(); - try { - fk.init(fd); - } catch (IOException ioe) { - throw new Error(ioe); - } + fk.init(fd); return fk; } diff --git a/test/java/nio/channels/FileChannel/InvalidFileChannel.java b/test/java/nio/channels/FileChannel/InvalidFileChannel.java new file mode 100644 --- /dev/null +++ b/test/java/nio/channels/FileChannel/InvalidFileChannel.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8025619 + * @summary Test for IOException when using a channel from a closed stream. + */ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + +public class InvalidFileChannel { + + public static void main(String[] args) throws IOException { + int exceptions = 0; + + for (int i = 0; i < 3; i++) { + File f = File.createTempFile("fcbug", ".tmp"); + f.deleteOnExit(); + + FileChannel fc = null; + boolean shared = false; + switch (i) { + case 0: + System.out.print("FileInputStream..."); + FileInputStream fis = new FileInputStream(f); + fis.close(); + fc = fis.getChannel(); + shared = true; + break; + case 1: + System.out.print("FileOutputStream..."); + FileOutputStream fos = new FileOutputStream(f); + fos.close(); + fc = fos.getChannel(); + break; + case 2: + System.out.print("RandomAccessFile..."); + RandomAccessFile raf = new RandomAccessFile(f, "rw"); + raf.close(); + fc = raf.getChannel(); + break; + } + + try { + fc.tryLock(0, Long.MAX_VALUE, shared); + } catch (IOException e) { + System.out.println("OK"); + exceptions++; + } catch (Error err) { + System.err.println(err); + } + } + + if (exceptions != 3) { + throw new RuntimeException("FAILED: expected 3 IOExceptions but obtained "+exceptions); + } + } +} # HG changeset patch # User bpb # Date 1417815971 28800 # Fri Dec 05 13:46:11 2014 -0800 # Node ID 91a6aac300125b5cee40b95292a66a31d0e1562e # Parent 2af4f2b969a41753550ae22ad93a1574f8d87954 8025619.01 diff --git a/src/java.base/share/classes/java/io/FileInputStream.java b/src/java.base/share/classes/java/io/FileInputStream.java --- a/src/java.base/share/classes/java/io/FileInputStream.java +++ b/src/java.base/share/classes/java/io/FileInputStream.java @@ -26,6 +26,7 @@ package java.io; import java.nio.channels.FileChannel; +import java.util.concurrent.atomic.AtomicBoolean; import sun.nio.ch.FileChannelImpl; @@ -59,8 +60,7 @@ private FileChannel channel = null; - private final Object closeLock = new Object(); - private volatile boolean closed = false; + private AtomicBoolean closed = new AtomicBoolean(false); /** * Creates a FileInputStream by @@ -313,12 +313,11 @@ * @spec JSR-51 */ public void close() throws IOException { - synchronized (closeLock) { - if (closed) { - return; - } - closed = true; + if (!closed.compareAndSet(false, true)) { + // if compareAndSet() returns false closed was already true + return; } + if (channel != null) { channel.close(); } @@ -367,7 +366,7 @@ synchronized (this) { if (channel == null) { channel = FileChannelImpl.open(fd, path, true, false, this); - if (closed) { + if (closed.get()) { try { channel.close(); } catch (IOException ioe) { diff --git a/src/java.base/share/classes/java/io/FileOutputStream.java b/src/java.base/share/classes/java/io/FileOutputStream.java --- a/src/java.base/share/classes/java/io/FileOutputStream.java +++ b/src/java.base/share/classes/java/io/FileOutputStream.java @@ -26,6 +26,7 @@ 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; @@ -76,8 +77,7 @@ */ private final String path; - private final Object closeLock = new Object(); - private volatile boolean closed = false; + private AtomicBoolean closed = new AtomicBoolean(false); /** * Creates a file output stream to write to the file with the @@ -341,11 +341,9 @@ * @spec JSR-51 */ public void close() throws IOException { - synchronized (closeLock) { - if (closed) { - return; - } - closed = true; + if (!closed.compareAndSet(false, true)) { + // if compareAndSet() returns false closed was already true + return; } if (channel != null) { @@ -397,6 +395,13 @@ synchronized (this) { if (channel == null) { channel = FileChannelImpl.open(fd, path, false, true, this); + if (closed.get()) { + try { + channel.close(); + } catch (IOException ioe) { + throw new InternalError(ioe); // should not happen + } + } } return channel; } diff --git a/src/java.base/share/classes/java/io/RandomAccessFile.java b/src/java.base/share/classes/java/io/RandomAccessFile.java --- a/src/java.base/share/classes/java/io/RandomAccessFile.java +++ b/src/java.base/share/classes/java/io/RandomAccessFile.java @@ -26,6 +26,7 @@ package java.io; import java.nio.channels.FileChannel; +import java.util.concurrent.atomic.AtomicBoolean; import sun.nio.ch.FileChannelImpl; @@ -68,8 +69,7 @@ */ private final String path; - private Object closeLock = new Object(); - private volatile boolean closed = false; + private AtomicBoolean closed = new AtomicBoolean(false); private static final int O_RDONLY = 1; private static final int O_RDWR = 2; @@ -280,6 +280,13 @@ 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; } @@ -604,12 +611,11 @@ * @spec JSR-51 */ public void close() throws IOException { - synchronized (closeLock) { - if (closed) { - return; - } - closed = true; + if (!closed.compareAndSet(false, true)) { + // if compareAndSet() returns false closed was already true + return; } + if (channel != null) { channel.close(); }