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

Print this page




 107      * @param      file   the file to be opened for reading.
 108      * @exception  FileNotFoundException  if the file does not exist,
 109      *                   is a directory rather than a regular file,
 110      *                   or for some other reason cannot be opened for
 111      *                   reading.
 112      * @exception  SecurityException      if a security manager exists and its
 113      *               <code>checkRead</code> method denies read access to the file.
 114      * @see        java.io.File#getPath()
 115      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
 116      */
 117     public FileInputStream(File file) throws FileNotFoundException {
 118         String name = (file != null ? file.getPath() : null);
 119         SecurityManager security = System.getSecurityManager();
 120         if (security != null) {
 121             security.checkRead(name);
 122         }
 123         if (name == null) {
 124             throw new NullPointerException();
 125         }
 126         fd = new FileDescriptor();
 127         fd.incrementAndGetUseCount();
 128         open(name);
 129     }
 130 
 131     /**
 132      * Creates a <code>FileInputStream</code> by using the file descriptor
 133      * <code>fdObj</code>, which represents an existing connection to an
 134      * actual file in the file system.
 135      * <p>
 136      * If there is a security manager, its <code>checkRead</code> method is
 137      * called with the file descriptor <code>fdObj</code> as its argument to
 138      * see if it's ok to read the file descriptor. If read access is denied
 139      * to the file descriptor a <code>SecurityException</code> is thrown.
 140      * <p>
 141      * If <code>fdObj</code> is null then a <code>NullPointerException</code>
 142      * is thrown.
 143      * <p>
 144      * This constructor does not throw an exception if <code>fdObj</code>
 145      * is {@link java.io.FileDescriptor#valid() invalid}.
 146      * However, if the methods are invoked on the resulting stream to attempt
 147      * I/O on the stream, an <code>IOException</code> is thrown.
 148      *
 149      * @param      fdObj   the file descriptor to be opened for reading.
 150      * @throws     SecurityException      if a security manager exists and its
 151      *                 <code>checkRead</code> method denies read access to the
 152      *                 file descriptor.
 153      * @see        SecurityManager#checkRead(java.io.FileDescriptor)
 154      */
 155     public FileInputStream(FileDescriptor fdObj) {
 156         SecurityManager security = System.getSecurityManager();
 157         if (fdObj == null) {
 158             throw new NullPointerException();
 159         }
 160         if (security != null) {
 161             security.checkRead(fdObj);
 162         }
 163         fd = fdObj;
 164 
 165         /*
 166          * FileDescriptor is being shared by streams.
 167          * Ensure that it's GC'ed only when all the streams/channels are done
 168          * using it.
 169          */
 170         fd.incrementAndGetUseCount();
 171     }
 172 
 173     /**
 174      * Opens the specified file for reading.
 175      * @param name the name of the file
 176      */
 177     private native void open(String name) throws FileNotFoundException;
 178 
 179     /**
 180      * Reads a byte of data from this input stream. This method blocks
 181      * if no input is yet available.
 182      *
 183      * @return     the next byte of data, or <code>-1</code> if the end of the
 184      *             file is reached.
 185      * @exception  IOException  if an I/O error occurs.
 186      */
 187     public native int read() throws IOException;
 188 
 189     /**
 190      * Reads a subarray as a sequence of bytes.


 277     /**
 278      * Closes this file input stream and releases any system resources
 279      * associated with the stream.
 280      *
 281      * <p> If this stream has an associated channel then the channel is closed
 282      * as well.
 283      *
 284      * @exception  IOException  if an I/O error occurs.
 285      *
 286      * @revised 1.4
 287      * @spec JSR-51
 288      */
 289     public void close() throws IOException {
 290         synchronized (closeLock) {
 291             if (closed) {
 292                 return;
 293             }
 294             closed = true;
 295         }
 296         if (channel != null) {
 297             /*
 298              * Decrement the FD use count associated with the channel
 299              * The use count is incremented whenever a new channel
 300              * is obtained from this stream.
 301              */
 302            fd.decrementAndGetUseCount();
 303            channel.close();
 304         }
 305 
 306         /*
 307          * Decrement the FD use count associated with this stream
 308          */
 309         int useCount = fd.decrementAndGetUseCount();
 310 
 311         /*
 312          * If FileDescriptor is still in use by another stream, we
 313          * will not close it.
 314          */
 315         if (useCount <= 0) {
 316             close0();
 317         }

 318     }
 319 
 320     /**
 321      * Returns the <code>FileDescriptor</code>
 322      * object  that represents the connection to
 323      * the actual file in the file system being
 324      * used by this <code>FileInputStream</code>.
 325      *
 326      * @return     the file descriptor object associated with this stream.
 327      * @exception  IOException  if an I/O error occurs.
 328      * @see        java.io.FileDescriptor
 329      */
 330     public final FileDescriptor getFD() throws IOException {
 331         if (fd != null) return fd;


 332         throw new IOException();
 333     }
 334 
 335     /**
 336      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 337      * object associated with this file input stream.
 338      *
 339      * <p> The initial {@link java.nio.channels.FileChannel#position()
 340      * </code>position<code>} of the returned channel will be equal to the
 341      * number of bytes read from the file so far.  Reading bytes from this
 342      * stream will increment the channel's position.  Changing the channel's
 343      * position, either explicitly or by reading, will change this stream's
 344      * file position.
 345      *
 346      * @return  the file channel associated with this file input stream
 347      *
 348      * @since 1.4
 349      * @spec JSR-51
 350      */
 351     public FileChannel getChannel() {
 352         synchronized (this) {
 353             if (channel == null) {
 354                 channel = FileChannelImpl.open(fd, true, false, this);
 355 
 356                 /*
 357                  * Increment fd's use count. Invoking the channel's close()
 358                  * method will result in decrementing the use count set for
 359                  * the channel.
 360                  */
 361                 fd.incrementAndGetUseCount();
 362             }
 363             return channel;
 364         }
 365     }
 366 
 367     private static native void initIDs();
 368 
 369     private native void close0() throws IOException;
 370 
 371     static {
 372         initIDs();
 373     }
 374 
 375     /**
 376      * Ensures that the <code>close</code> method of this file input stream is
 377      * called when there are no more references to it.
 378      *
 379      * @exception  IOException  if an I/O error occurs.
 380      * @see        java.io.FileInputStream#close()
 381      */
 382     protected void finalize() throws IOException {
 383         if ((fd != null) &&  (fd != FileDescriptor.in)) {





 384                 close();
 385         }
 386     }
 387 }


 107      * @param      file   the file to be opened for reading.
 108      * @exception  FileNotFoundException  if the file does not exist,
 109      *                   is a directory rather than a regular file,
 110      *                   or for some other reason cannot be opened for
 111      *                   reading.
 112      * @exception  SecurityException      if a security manager exists and its
 113      *               <code>checkRead</code> method denies read access to the file.
 114      * @see        java.io.File#getPath()
 115      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
 116      */
 117     public FileInputStream(File file) throws FileNotFoundException {
 118         String name = (file != null ? file.getPath() : null);
 119         SecurityManager security = System.getSecurityManager();
 120         if (security != null) {
 121             security.checkRead(name);
 122         }
 123         if (name == null) {
 124             throw new NullPointerException();
 125         }
 126         fd = new FileDescriptor();
 127         fd.attach(this);
 128         open(name);
 129     }
 130 
 131     /**
 132      * Creates a <code>FileInputStream</code> by using the file descriptor
 133      * <code>fdObj</code>, which represents an existing connection to an
 134      * actual file in the file system.
 135      * <p>
 136      * If there is a security manager, its <code>checkRead</code> method is
 137      * called with the file descriptor <code>fdObj</code> as its argument to
 138      * see if it's ok to read the file descriptor. If read access is denied
 139      * to the file descriptor a <code>SecurityException</code> is thrown.
 140      * <p>
 141      * If <code>fdObj</code> is null then a <code>NullPointerException</code>
 142      * is thrown.
 143      * <p>
 144      * This constructor does not throw an exception if <code>fdObj</code>
 145      * is {@link java.io.FileDescriptor#valid() invalid}.
 146      * However, if the methods are invoked on the resulting stream to attempt
 147      * I/O on the stream, an <code>IOException</code> is thrown.
 148      *
 149      * @param      fdObj   the file descriptor to be opened for reading.
 150      * @throws     SecurityException      if a security manager exists and its
 151      *                 <code>checkRead</code> method denies read access to the
 152      *                 file descriptor.
 153      * @see        SecurityManager#checkRead(java.io.FileDescriptor)
 154      */
 155     public FileInputStream(FileDescriptor fdObj) {
 156         SecurityManager security = System.getSecurityManager();
 157         if (fdObj == null) {
 158             throw new NullPointerException();
 159         }
 160         if (security != null) {
 161             security.checkRead(fdObj);
 162         }
 163         fd = fdObj;
 164 
 165         /*
 166          * FileDescriptor is being shared by streams.
 167          * Register this stream with FileDescriptor tracker.

 168          */
 169         fd.attach(this);
 170     }
 171 
 172     /**
 173      * Opens the specified file for reading.
 174      * @param name the name of the file
 175      */
 176     private native void open(String name) throws FileNotFoundException;
 177 
 178     /**
 179      * Reads a byte of data from this input stream. This method blocks
 180      * if no input is yet available.
 181      *
 182      * @return     the next byte of data, or <code>-1</code> if the end of the
 183      *             file is reached.
 184      * @exception  IOException  if an I/O error occurs.
 185      */
 186     public native int read() throws IOException;
 187 
 188     /**
 189      * Reads a subarray as a sequence of bytes.


 276     /**
 277      * Closes this file input stream and releases any system resources
 278      * associated with the stream.
 279      *
 280      * <p> If this stream has an associated channel then the channel is closed
 281      * as well.
 282      *
 283      * @exception  IOException  if an I/O error occurs.
 284      *
 285      * @revised 1.4
 286      * @spec JSR-51
 287      */
 288     public void close() throws IOException {
 289         synchronized (closeLock) {
 290             if (closed) {
 291                 return;
 292             }
 293             closed = true;
 294         }
 295         if (channel != null) {






 296            channel.close();
 297         }
 298 
 299         fd.closeAll(new Closeable() {
 300             public void close() throws IOException {








 301                close0();
 302            }
 303         });
 304     } 
 305 
 306     /**
 307      * Returns the <code>FileDescriptor</code>
 308      * object  that represents the connection to
 309      * the actual file in the file system being
 310      * used by this <code>FileInputStream</code>.
 311      *
 312      * @return     the file descriptor object associated with this stream.
 313      * @exception  IOException  if an I/O error occurs.
 314      * @see        java.io.FileDescriptor
 315      */
 316     public final FileDescriptor getFD() throws IOException {
 317         if (fd != null) {
 318             return fd;
 319         }
 320         throw new IOException();
 321     }
 322 
 323     /**
 324      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 325      * object associated with this file input stream.
 326      *
 327      * <p> The initial {@link java.nio.channels.FileChannel#position()
 328      * </code>position<code>} of the returned channel will be equal to the
 329      * number of bytes read from the file so far.  Reading bytes from this
 330      * stream will increment the channel's position.  Changing the channel's
 331      * position, either explicitly or by reading, will change this stream's
 332      * file position.
 333      *
 334      * @return  the file channel associated with this file input stream
 335      *
 336      * @since 1.4
 337      * @spec JSR-51
 338      */
 339     public FileChannel getChannel() {
 340         synchronized (this) {
 341             if (channel == null) {
 342                 channel = FileChannelImpl.open(fd, true, false, this);







 343             }
 344             return channel;
 345         }
 346     }
 347  
 348     private static native void initIDs();
 349 
 350     private native void close0() throws IOException;
 351 
 352     static {
 353         initIDs();
 354     }
 355 
 356     /**
 357      * Ensures that the <code>close</code> method of this file input stream is
 358      * called when there are no more references to it.
 359      *
 360      * @exception  IOException  if an I/O error occurs.
 361      * @see        java.io.FileInputStream#close()
 362      */
 363     protected void finalize() throws IOException {
 364         if ((fd != null) &&  (fd != FileDescriptor.in)) {
 365             /* if fd is shared, the references in FileDescriptor
 366              * will ensure that finalizer is only called when
 367              * safe to do so. All references using the fd have
 368              * become unreachable. We can call close()
 369              */
 370             close();
 371         }
 372     }
 373 }