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

Print this page
rev 6099 : [mq]: io-trace


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


 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.
 190      * @param b the data to be written
 191      * @param off the start offset in the data
 192      * @param len the number of bytes that are written
 193      * @exception IOException If an I/O error has occurred.
 194      */
 195     private native int readBytes(byte b[], int off, int len) throws IOException;
 196 
 197     /**
 198      * Reads up to <code>b.length</code> bytes of data from this input
 199      * stream into an array of bytes. This method blocks until some input
 200      * is available.
 201      *
 202      * @param      b   the buffer into which the data is read.
 203      * @return     the total number of bytes read into the buffer, or
 204      *             <code>-1</code> if there is no more data because the end of
 205      *             the file has been reached.
 206      * @exception  IOException  if an I/O error occurs.
 207      */
 208     public int read(byte b[]) throws IOException {
 209         return readBytes(b, 0, b.length);







 210     }
 211 
 212     /**
 213      * Reads up to <code>len</code> bytes of data from this input stream
 214      * into an array of bytes. If <code>len</code> is not zero, the method
 215      * blocks until some input is available; otherwise, no
 216      * bytes are read and <code>0</code> is returned.
 217      *
 218      * @param      b     the buffer into which the data is read.
 219      * @param      off   the start offset in the destination array <code>b</code>
 220      * @param      len   the maximum number of bytes read.
 221      * @return     the total number of bytes read into the buffer, or
 222      *             <code>-1</code> if there is no more data because the end of
 223      *             the file has been reached.
 224      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 225      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 226      * <code>len</code> is negative, or <code>len</code> is greater than
 227      * <code>b.length - off</code>
 228      * @exception  IOException  if an I/O error occurs.
 229      */
 230     public int read(byte b[], int off, int len) throws IOException {
 231         return readBytes(b, off, len);







 232     }
 233 
 234     /**
 235      * Skips over and discards <code>n</code> bytes of data from the
 236      * input stream.
 237      *
 238      * <p>The <code>skip</code> method may, for a variety of
 239      * reasons, end up skipping over some smaller number of bytes,
 240      * possibly <code>0</code>. If <code>n</code> is negative, an
 241      * <code>IOException</code> is thrown, even though the <code>skip</code>
 242      * method of the {@link InputStream} superclass does nothing in this case.
 243      * The actual number of bytes skipped is returned.
 244      *
 245      * <p>This method may skip more bytes than are remaining in the backing
 246      * file. This produces no exception and the number of bytes skipped
 247      * may include some number of bytes that were beyond the EOF of the
 248      * backing file. Attempting to read from the stream after skipping past
 249      * the end will result in -1 indicating the end of the file.
 250      *
 251      * @param      n   the number of bytes to be skipped.


 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      */


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


 113      * @exception  FileNotFoundException  if the file does not exist,
 114      *                   is a directory rather than a regular file,
 115      *                   or for some other reason cannot be opened for
 116      *                   reading.
 117      * @exception  SecurityException      if a security manager exists and its
 118      *               <code>checkRead</code> method denies read access to the file.
 119      * @see        java.io.File#getPath()
 120      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
 121      */
 122     public FileInputStream(File file) throws FileNotFoundException {
 123         String name = (file != null ? file.getPath() : null);
 124         SecurityManager security = System.getSecurityManager();
 125         if (security != null) {
 126             security.checkRead(name);
 127         }
 128         if (name == null) {
 129             throw new NullPointerException();
 130         }
 131         fd = new FileDescriptor();
 132         fd.attach(this);
 133         this.path = name;
 134         open(name);
 135     }
 136 
 137     /**
 138      * Creates a <code>FileInputStream</code> by using the file descriptor
 139      * <code>fdObj</code>, which represents an existing connection to an
 140      * actual file in the file system.
 141      * <p>
 142      * If there is a security manager, its <code>checkRead</code> method is
 143      * called with the file descriptor <code>fdObj</code> as its argument to
 144      * see if it's ok to read the file descriptor. If read access is denied
 145      * to the file descriptor a <code>SecurityException</code> is thrown.
 146      * <p>
 147      * If <code>fdObj</code> is null then a <code>NullPointerException</code>
 148      * is thrown.
 149      * <p>
 150      * This constructor does not throw an exception if <code>fdObj</code>
 151      * is {@link java.io.FileDescriptor#valid() invalid}.
 152      * However, if the methods are invoked on the resulting stream to attempt
 153      * I/O on the stream, an <code>IOException</code> is thrown.
 154      *
 155      * @param      fdObj   the file descriptor to be opened for reading.
 156      * @throws     SecurityException      if a security manager exists and its
 157      *                 <code>checkRead</code> method denies read access to the
 158      *                 file descriptor.
 159      * @see        SecurityManager#checkRead(java.io.FileDescriptor)
 160      */
 161     public FileInputStream(FileDescriptor fdObj) {
 162         SecurityManager security = System.getSecurityManager();
 163         if (fdObj == null) {
 164             throw new NullPointerException();
 165         }
 166         if (security != null) {
 167             security.checkRead(fdObj);
 168         }
 169         fd = fdObj;
 170         path = null;
 171 
 172         /*
 173          * FileDescriptor is being shared by streams.
 174          * Register this stream with FileDescriptor tracker.
 175          */
 176         fd.attach(this);
 177     }
 178 
 179     /**
 180      * Opens the specified file for reading.
 181      * @param name the name of the file
 182      */
 183     private native void open(String name) throws FileNotFoundException;
 184 
 185     /**
 186      * Reads a byte of data from this input stream. This method blocks
 187      * if no input is yet available.
 188      *
 189      * @return     the next byte of data, or <code>-1</code> if the end of the
 190      *             file is reached.
 191      * @exception  IOException  if an I/O error occurs.
 192      */
 193     public int read() throws IOException {
 194         IoTraceContext traceContext = IoTrace.fileReadBegin(path);
 195         int b = 0;
 196         try {
 197             b = read0();
 198         } finally {
 199             IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);
 200         }
 201         return b;
 202     }
 203 
 204     private native int read0() throws IOException;
 205 
 206     /**
 207      * Reads a subarray as a sequence of bytes.
 208      * @param b the data to be written
 209      * @param off the start offset in the data
 210      * @param len the number of bytes that are written
 211      * @exception IOException If an I/O error has occurred.
 212      */
 213     private native int readBytes(byte b[], int off, int len) throws IOException;
 214 
 215     /**
 216      * Reads up to <code>b.length</code> bytes of data from this input
 217      * stream into an array of bytes. This method blocks until some input
 218      * is available.
 219      *
 220      * @param      b   the buffer into which the data is read.
 221      * @return     the total number of bytes read into the buffer, or
 222      *             <code>-1</code> if there is no more data because the end of
 223      *             the file has been reached.
 224      * @exception  IOException  if an I/O error occurs.
 225      */
 226     public int read(byte b[]) throws IOException {
 227         IoTraceContext traceContext = IoTrace.fileReadBegin(path);
 228         int bytesRead = 0;
 229         try {
 230             bytesRead = readBytes(b, 0, b.length);
 231         } finally {
 232             IoTrace.fileReadEnd(traceContext, bytesRead);
 233         }
 234         return bytesRead;
 235     }
 236 
 237     /**
 238      * Reads up to <code>len</code> bytes of data from this input stream
 239      * into an array of bytes. If <code>len</code> is not zero, the method
 240      * blocks until some input is available; otherwise, no
 241      * bytes are read and <code>0</code> is returned.
 242      *
 243      * @param      b     the buffer into which the data is read.
 244      * @param      off   the start offset in the destination array <code>b</code>
 245      * @param      len   the maximum number of bytes read.
 246      * @return     the total number of bytes read into the buffer, or
 247      *             <code>-1</code> if there is no more data because the end of
 248      *             the file has been reached.
 249      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 250      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 251      * <code>len</code> is negative, or <code>len</code> is greater than
 252      * <code>b.length - off</code>
 253      * @exception  IOException  if an I/O error occurs.
 254      */
 255     public int read(byte b[], int off, int len) throws IOException {
 256         IoTraceContext traceContext = IoTrace.fileReadBegin(path);
 257         int bytesRead = 0;
 258         try {
 259             bytesRead = readBytes(b, off, len);
 260         } finally {
 261             IoTrace.fileReadEnd(traceContext, bytesRead);
 262         }
 263         return bytesRead;
 264     }
 265 
 266     /**
 267      * Skips over and discards <code>n</code> bytes of data from the
 268      * input stream.
 269      *
 270      * <p>The <code>skip</code> method may, for a variety of
 271      * reasons, end up skipping over some smaller number of bytes,
 272      * possibly <code>0</code>. If <code>n</code> is negative, an
 273      * <code>IOException</code> is thrown, even though the <code>skip</code>
 274      * method of the {@link InputStream} superclass does nothing in this case.
 275      * The actual number of bytes skipped is returned.
 276      *
 277      * <p>This method may skip more bytes than are remaining in the backing
 278      * file. This produces no exception and the number of bytes skipped
 279      * may include some number of bytes that were beyond the EOF of the
 280      * backing file. Attempting to read from the stream after skipping past
 281      * the end will result in -1 indicating the end of the file.
 282      *
 283      * @param      n   the number of bytes to be skipped.


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