src/java.base/share/classes/java/io/FileInputStream.java

Print this page
rev 11066 : 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
rev 11067 : 8025619.01


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.nio.channels.FileChannel;

  29 import sun.nio.ch.FileChannelImpl;
  30 
  31 
  32 /**
  33  * A <code>FileInputStream</code> obtains input bytes
  34  * from a file in a file system. What files
  35  * are  available depends on the host environment.
  36  *
  37  * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
  38  * such as image data. For reading streams of characters, consider using
  39  * <code>FileReader</code>.
  40  *
  41  * @author  Arthur van Hoff
  42  * @see     java.io.File
  43  * @see     java.io.FileDescriptor
  44  * @see     java.io.FileOutputStream
  45  * @see     java.nio.file.Files#newInputStream
  46  * @since   1.0
  47  */
  48 public
  49 class FileInputStream extends InputStream
  50 {
  51     /* File Descriptor - handle to the open file */
  52     private final FileDescriptor fd;
  53 
  54     /**
  55      * The path of the referenced file
  56      * (null if the stream is created with a file descriptor)
  57      */
  58     private final String path;
  59 
  60     private FileChannel channel = null;
  61 
  62     private final Object closeLock = new Object();
  63     private volatile boolean closed = false;
  64 
  65     /**
  66      * Creates a <code>FileInputStream</code> by
  67      * opening a connection to an actual file,
  68      * the file named by the path name <code>name</code>
  69      * in the file system.  A new <code>FileDescriptor</code>
  70      * object is created to represent this file
  71      * connection.
  72      * <p>
  73      * First, if there is a security
  74      * manager, its <code>checkRead</code> method
  75      * is called with the <code>name</code> argument
  76      * as its argument.
  77      * <p>
  78      * If the named file does not exist, is a directory rather than a regular
  79      * file, or for some other reason cannot be opened for reading then a
  80      * <code>FileNotFoundException</code> is thrown.
  81      *
  82      * @param      name   the system-dependent file name.
  83      * @exception  FileNotFoundException  if the file does not exist,


 296      * @return     an estimate of the number of remaining bytes that can be read
 297      *             (or skipped over) from this input stream without blocking.
 298      * @exception  IOException  if this file input stream has been closed by calling
 299      *             {@code close} or an I/O error occurs.
 300      */
 301     public native int available() throws IOException;
 302 
 303     /**
 304      * Closes this file input stream and releases any system resources
 305      * associated with the stream.
 306      *
 307      * <p> If this stream has an associated channel then the channel is closed
 308      * as well.
 309      *
 310      * @exception  IOException  if an I/O error occurs.
 311      *
 312      * @revised 1.4
 313      * @spec JSR-51
 314      */
 315     public void close() throws IOException {
 316         synchronized (closeLock) {
 317             if (closed) {
 318                 return;
 319             }
 320             closed = true;
 321         }
 322         if (channel != null) {
 323            channel.close();
 324         }
 325 
 326         fd.closeAll(new Closeable() {
 327             public void close() throws IOException {
 328                close0();
 329            }
 330         });
 331     }
 332 
 333     /**
 334      * Returns the <code>FileDescriptor</code>
 335      * object  that represents the connection to
 336      * the actual file in the file system being
 337      * used by this <code>FileInputStream</code>.
 338      *
 339      * @return     the file descriptor object associated with this stream.
 340      * @exception  IOException  if an I/O error occurs.
 341      * @see        java.io.FileDescriptor


 350     /**
 351      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 352      * object associated with this file input stream.
 353      *
 354      * <p> The initial {@link java.nio.channels.FileChannel#position()
 355      * position} of the returned channel will be equal to the
 356      * number of bytes read from the file so far.  Reading bytes from this
 357      * stream will increment the channel's position.  Changing the channel's
 358      * position, either explicitly or by reading, will change this stream's
 359      * file position.
 360      *
 361      * @return  the file channel associated with this file input stream
 362      *
 363      * @since 1.4
 364      * @spec JSR-51
 365      */
 366     public FileChannel getChannel() {
 367         synchronized (this) {
 368             if (channel == null) {
 369                 channel = FileChannelImpl.open(fd, path, true, false, this);







 370             }
 371             return channel;
 372         }
 373     }
 374 
 375     private static native void initIDs();
 376 
 377     private native void close0() throws IOException;
 378 
 379     static {
 380         initIDs();
 381     }
 382 
 383     /**
 384      * Ensures that the <code>close</code> method of this file input stream is
 385      * called when there are no more references to it.
 386      *
 387      * @exception  IOException  if an I/O error occurs.
 388      * @see        java.io.FileInputStream#close()
 389      */


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.nio.channels.FileChannel;
  29 import java.util.concurrent.atomic.AtomicBoolean;
  30 import sun.nio.ch.FileChannelImpl;
  31 
  32 
  33 /**
  34  * A <code>FileInputStream</code> obtains input bytes
  35  * from a file in a file system. What files
  36  * are  available depends on the host environment.
  37  *
  38  * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
  39  * such as image data. For reading streams of characters, consider using
  40  * <code>FileReader</code>.
  41  *
  42  * @author  Arthur van Hoff
  43  * @see     java.io.File
  44  * @see     java.io.FileDescriptor
  45  * @see     java.io.FileOutputStream
  46  * @see     java.nio.file.Files#newInputStream
  47  * @since   1.0
  48  */
  49 public
  50 class FileInputStream extends InputStream
  51 {
  52     /* File Descriptor - handle to the open file */
  53     private final FileDescriptor fd;
  54 
  55     /**
  56      * The path of the referenced file
  57      * (null if the stream is created with a file descriptor)
  58      */
  59     private final String path;
  60 
  61     private FileChannel channel = null;
  62 
  63     private AtomicBoolean closed = new AtomicBoolean(false);

  64 
  65     /**
  66      * Creates a <code>FileInputStream</code> by
  67      * opening a connection to an actual file,
  68      * the file named by the path name <code>name</code>
  69      * in the file system.  A new <code>FileDescriptor</code>
  70      * object is created to represent this file
  71      * connection.
  72      * <p>
  73      * First, if there is a security
  74      * manager, its <code>checkRead</code> method
  75      * is called with the <code>name</code> argument
  76      * as its argument.
  77      * <p>
  78      * If the named file does not exist, is a directory rather than a regular
  79      * file, or for some other reason cannot be opened for reading then a
  80      * <code>FileNotFoundException</code> is thrown.
  81      *
  82      * @param      name   the system-dependent file name.
  83      * @exception  FileNotFoundException  if the file does not exist,


 296      * @return     an estimate of the number of remaining bytes that can be read
 297      *             (or skipped over) from this input stream without blocking.
 298      * @exception  IOException  if this file input stream has been closed by calling
 299      *             {@code close} or an I/O error occurs.
 300      */
 301     public native int available() throws IOException;
 302 
 303     /**
 304      * Closes this file input stream and releases any system resources
 305      * associated with the stream.
 306      *
 307      * <p> If this stream has an associated channel then the channel is closed
 308      * as well.
 309      *
 310      * @exception  IOException  if an I/O error occurs.
 311      *
 312      * @revised 1.4
 313      * @spec JSR-51
 314      */
 315     public void close() throws IOException {
 316         if (!closed.compareAndSet(false, true)) {
 317             // if compareAndSet() returns false closed was already true
 318             return;
 319         }
 320 

 321         if (channel != null) {
 322            channel.close();
 323         }
 324 
 325         fd.closeAll(new Closeable() {
 326             public void close() throws IOException {
 327                close0();
 328            }
 329         });
 330     }
 331 
 332     /**
 333      * Returns the <code>FileDescriptor</code>
 334      * object  that represents the connection to
 335      * the actual file in the file system being
 336      * used by this <code>FileInputStream</code>.
 337      *
 338      * @return     the file descriptor object associated with this stream.
 339      * @exception  IOException  if an I/O error occurs.
 340      * @see        java.io.FileDescriptor


 349     /**
 350      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 351      * object associated with this file input stream.
 352      *
 353      * <p> The initial {@link java.nio.channels.FileChannel#position()
 354      * position} of the returned channel will be equal to the
 355      * number of bytes read from the file so far.  Reading bytes from this
 356      * stream will increment the channel's position.  Changing the channel's
 357      * position, either explicitly or by reading, will change this stream's
 358      * file position.
 359      *
 360      * @return  the file channel associated with this file input stream
 361      *
 362      * @since 1.4
 363      * @spec JSR-51
 364      */
 365     public FileChannel getChannel() {
 366         synchronized (this) {
 367             if (channel == null) {
 368                 channel = FileChannelImpl.open(fd, path, true, false, this);
 369                 if (closed.get()) {
 370                     try {
 371                         channel.close();
 372                     } catch (IOException ioe) {
 373                         throw new InternalError(ioe); // should not happen
 374                     }
 375                 }
 376             }
 377             return channel;
 378         }
 379     }
 380 
 381     private static native void initIDs();
 382 
 383     private native void close0() throws IOException;
 384 
 385     static {
 386         initIDs();
 387     }
 388 
 389     /**
 390      * Ensures that the <code>close</code> method of this file input stream is
 391      * called when there are no more references to it.
 392      *
 393      * @exception  IOException  if an I/O error occurs.
 394      * @see        java.io.FileInputStream#close()
 395      */