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

Print this page
rev 9260 : [mq]: io-events-path


  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   JDK1.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     private FileChannel channel = null;
  55 
  56     private final Object closeLock = new Object();
  57     private volatile boolean closed = false;
  58 
  59     /**
  60      * Creates a <code>FileInputStream</code> by
  61      * opening a connection to an actual file,
  62      * the file named by the path name <code>name</code>
  63      * in the file system.  A new <code>FileDescriptor</code>
  64      * object is created to represent this file
  65      * connection.
  66      * <p>
  67      * First, if there is a security
  68      * manager, its <code>checkRead</code> method
  69      * is called with the <code>name</code> argument
  70      * as its argument.
  71      * <p>
  72      * If the named file does not exist, is a directory rather than a regular
  73      * file, or for some other reason cannot be opened for reading then a


 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         if (file.isInvalid()) {
 127             throw new FileNotFoundException("Invalid file path");
 128         }
 129         fd = new FileDescriptor();
 130         fd.attach(this);

 131         open(name);
 132     }
 133 
 134     /**
 135      * Creates a <code>FileInputStream</code> by using the file descriptor
 136      * <code>fdObj</code>, which represents an existing connection to an
 137      * actual file in the file system.
 138      * <p>
 139      * If there is a security manager, its <code>checkRead</code> method is
 140      * called with the file descriptor <code>fdObj</code> as its argument to
 141      * see if it's ok to read the file descriptor. If read access is denied
 142      * to the file descriptor a <code>SecurityException</code> is thrown.
 143      * <p>
 144      * If <code>fdObj</code> is null then a <code>NullPointerException</code>
 145      * is thrown.
 146      * <p>
 147      * This constructor does not throw an exception if <code>fdObj</code>
 148      * is {@link java.io.FileDescriptor#valid() invalid}.
 149      * However, if the methods are invoked on the resulting stream to attempt
 150      * I/O on the stream, an <code>IOException</code> is thrown.
 151      *
 152      * @param      fdObj   the file descriptor to be opened for reading.
 153      * @throws     SecurityException      if a security manager exists and its
 154      *                 <code>checkRead</code> method denies read access to the
 155      *                 file descriptor.
 156      * @see        SecurityManager#checkRead(java.io.FileDescriptor)
 157      */
 158     public FileInputStream(FileDescriptor fdObj) {
 159         SecurityManager security = System.getSecurityManager();
 160         if (fdObj == null) {
 161             throw new NullPointerException();
 162         }
 163         if (security != null) {
 164             security.checkRead(fdObj);
 165         }
 166         fd = fdObj;

 167 
 168         /*
 169          * FileDescriptor is being shared by streams.
 170          * Register this stream with FileDescriptor tracker.
 171          */
 172         fd.attach(this);
 173     }
 174 
 175     /**
 176      * Opens the specified file for reading.
 177      * @param name the name of the file
 178      */
 179     private native void open(String name) throws FileNotFoundException;
 180 
 181     /**
 182      * Reads a byte of data from this input stream. This method blocks
 183      * if no input is yet available.
 184      *
 185      * @return     the next byte of data, or <code>-1</code> if the end of the
 186      *             file is reached.


 328 
 329     /**
 330      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 331      * object associated with this file input stream.
 332      *
 333      * <p> The initial {@link java.nio.channels.FileChannel#position()
 334      * position} of the returned channel will be equal to the
 335      * number of bytes read from the file so far.  Reading bytes from this
 336      * stream will increment the channel's position.  Changing the channel's
 337      * position, either explicitly or by reading, will change this stream's
 338      * file position.
 339      *
 340      * @return  the file channel associated with this file input stream
 341      *
 342      * @since 1.4
 343      * @spec JSR-51
 344      */
 345     public FileChannel getChannel() {
 346         synchronized (this) {
 347             if (channel == null) {
 348                 channel = FileChannelImpl.open(fd, true, false, this);
 349             }
 350             return channel;
 351         }
 352     }
 353 
 354     private static native void initIDs();
 355 
 356     private native void close0() throws IOException;
 357 
 358     static {
 359         initIDs();
 360     }
 361 
 362     /**
 363      * Ensures that the <code>close</code> method of this file input stream is
 364      * called when there are no more references to it.
 365      *
 366      * @exception  IOException  if an I/O error occurs.
 367      * @see        java.io.FileInputStream#close()
 368      */


  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   JDK1.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


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


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