< prev index next >

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

Print this page
rev 15897 : 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
Reviewed-by: alanb, shade, plevart


   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 volatile FileChannel channel;
  62 
  63     private final 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         FileChannel fc = channel;
 322         if (fc != null) {
 323            fc.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.


 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         FileChannel fc = this.channel;
 368         if (fc == null) {
 369             synchronized (this) {
 370                 fc = this.channel;
 371                 if (fc == null) {
 372                     this.channel = fc = FileChannelImpl.open(fd, path, true, false, this);
 373                     if (closed.get()) {
 374                         try {
 375                             fc.close();
 376                         } catch (IOException ioe) {
 377                             throw new InternalError(ioe); // should not happen
 378                         }
 379                     }
 380                 }
 381             }
 382         }
 383         return fc;
 384     }
 385 
 386     private static native void initIDs();
 387 
 388     private native void close0() throws IOException;
 389 
 390     static {
 391         initIDs();
 392     }
 393 


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


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


 359      *
 360      * <p> The initial {@link java.nio.channels.FileChannel#position()
 361      * position} of the returned channel will be equal to the
 362      * number of bytes read from the file so far.  Reading bytes from this
 363      * stream will increment the channel's position.  Changing the channel's
 364      * position, either explicitly or by reading, will change this stream's
 365      * file position.
 366      *
 367      * @return  the file channel associated with this file input stream
 368      *
 369      * @since 1.4
 370      * @spec JSR-51
 371      */
 372     public FileChannel getChannel() {
 373         FileChannel fc = this.channel;
 374         if (fc == null) {
 375             synchronized (this) {
 376                 fc = this.channel;
 377                 if (fc == null) {
 378                     this.channel = fc = FileChannelImpl.open(fd, path, true, false, this);
 379                     if (closed) {
 380                         try {
 381                             fc.close();
 382                         } catch (IOException ioe) {
 383                             throw new InternalError(ioe); // should not happen
 384                         }
 385                     }
 386                 }
 387             }
 388         }
 389         return fc;
 390     }
 391 
 392     private static native void initIDs();
 393 
 394     private native void close0() throws IOException;
 395 
 396     static {
 397         initIDs();
 398     }
 399 
< prev index next >