1 /* 2 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 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 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 74 * <code>FileNotFoundException</code> is thrown. 75 * 76 * @param name the system-dependent file name. 77 * @exception FileNotFoundException if the file does not exist, 78 * is a directory rather than a regular file, 79 * or for some other reason cannot be opened for 80 * reading. 81 * @exception SecurityException if a security manager exists and its 82 * <code>checkRead</code> method denies read access 83 * to the file. 84 * @see java.lang.SecurityManager#checkRead(java.lang.String) 85 */ 86 public FileInputStream(String name) throws FileNotFoundException { 87 this(name != null ? new File(name) : null); 88 } 89 90 /** 91 * Creates a <code>FileInputStream</code> by 92 * opening a connection to an actual file, 93 * the file named by the <code>File</code> 94 * object <code>file</code> in the file system. 95 * A new <code>FileDescriptor</code> object 96 * is created to represent this file connection. 97 * <p> 98 * First, if there is a security manager, 99 * its <code>checkRead</code> method is called 100 * with the path represented by the <code>file</code> 101 * argument as its argument. 102 * <p> 103 * If the named file does not exist, is a directory rather than a regular 104 * file, or for some other reason cannot be opened for reading then a 105 * <code>FileNotFoundException</code> is thrown. 106 * 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. 191 * @param b the data to be written 192 * @param off the start offset in the data 193 * @param len the number of bytes that are written 194 * @exception IOException If an I/O error has occurred. 195 */ 196 private native int readBytes(byte b[], int off, int len) throws IOException; 197 198 /** 199 * Reads up to <code>b.length</code> bytes of data from this input 200 * stream into an array of bytes. This method blocks until some input 201 * is available. 202 * 203 * @param b the buffer into which the data is read. 204 * @return the total number of bytes read into the buffer, or 205 * <code>-1</code> if there is no more data because the end of 206 * the file has been reached. 207 * @exception IOException if an I/O error occurs. 208 */ 209 public int read(byte b[]) throws IOException { 210 return readBytes(b, 0, b.length); 211 } 212 213 /** 214 * Reads up to <code>len</code> bytes of data from this input stream 215 * into an array of bytes. If <code>len</code> is not zero, the method 216 * blocks until some input is available; otherwise, no 217 * bytes are read and <code>0</code> is returned. 218 * 219 * @param b the buffer into which the data is read. 220 * @param off the start offset in the destination array <code>b</code> 221 * @param len the maximum number of bytes read. 222 * @return the total number of bytes read into the buffer, or 223 * <code>-1</code> if there is no more data because the end of 224 * the file has been reached. 225 * @exception NullPointerException If <code>b</code> is <code>null</code>. 226 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 227 * <code>len</code> is negative, or <code>len</code> is greater than 228 * <code>b.length - off</code> 229 * @exception IOException if an I/O error occurs. 230 */ 231 public int read(byte b[], int off, int len) throws IOException { 232 return readBytes(b, off, len); 233 } 234 235 /** 236 * Skips over and discards <code>n</code> bytes of data from the 237 * input stream. 238 * 239 * <p>The <code>skip</code> method may, for a variety of 240 * reasons, end up skipping over some smaller number of bytes, 241 * possibly <code>0</code>. If <code>n</code> is negative, an 242 * <code>IOException</code> is thrown, even though the <code>skip</code> 243 * method of the {@link InputStream} superclass does nothing in this case. 244 * The actual number of bytes skipped is returned. 245 * 246 * <p>This method may skip more bytes than are remaining in the backing 247 * file. This produces no exception and the number of bytes skipped 248 * may include some number of bytes that were beyond the EOF of the 249 * backing file. Attempting to read from the stream after skipping past 250 * the end will result in -1 indicating the end of the file. 251 * 252 * @param n the number of bytes to be skipped. 253 * @return the actual number of bytes skipped. 254 * @exception IOException if n is negative, if the stream does not 255 * support seek, or if an I/O error occurs. 256 */ 257 public native long skip(long n) throws IOException; 258 259 /** 260 * Returns an estimate of the number of remaining bytes that can be read (or 261 * skipped over) from this input stream without blocking by the next 262 * invocation of a method for this input stream. The next invocation might be 263 * the same thread or another thread. A single read or skip of this 264 * many bytes will not block, but may read or skip fewer bytes. 265 * 266 * <p> In some cases, a non-blocking read (or skip) may appear to be 267 * blocked when it is merely slow, for example when reading large 268 * files over slow networks. 269 * 270 * @return an estimate of the number of remaining bytes that can be read 271 * (or skipped over) from this input stream without blocking. 272 * @exception IOException if this file input stream has been closed by calling 273 * {@code close} or an I/O error occurs. 274 */ 275 public native int available() throws IOException; 276 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 }