1 /* 2 * Copyright (c) 1994, 2013, 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 /* The path of the referenced file (null if the stream is created with a file descriptor) */ 55 private final String path; 56 57 private FileChannel channel = null; 58 59 private final Object closeLock = new Object(); 60 private volatile boolean closed = false; 61 62 /** 63 * Creates a <code>FileInputStream</code> by 64 * opening a connection to an actual file, 65 * the file named by the path name <code>name</code> 66 * in the file system. A new <code>FileDescriptor</code> 67 * object is created to represent this file 68 * connection. 69 * <p> 70 * First, if there is a security 71 * manager, its <code>checkRead</code> method 72 * is called with the <code>name</code> argument 73 * as its argument. 74 * <p> 75 * If the named file does not exist, is a directory rather than a regular 76 * file, or for some other reason cannot be opened for reading then a 77 * <code>FileNotFoundException</code> is thrown. 78 * 79 * @param name the system-dependent file name. 80 * @exception FileNotFoundException if the file does not exist, 81 * is a directory rather than a regular file, 82 * or for some other reason cannot be opened for 83 * reading. 84 * @exception SecurityException if a security manager exists and its 85 * <code>checkRead</code> method denies read access 86 * to the file. 87 * @see java.lang.SecurityManager#checkRead(java.lang.String) 88 */ 89 public FileInputStream(String name) throws FileNotFoundException { 90 this(name != null ? new File(name) : null); 91 } 92 93 /** 94 * Creates a <code>FileInputStream</code> by 95 * opening a connection to an actual file, 96 * the file named by the <code>File</code> 97 * object <code>file</code> in the file system. 98 * A new <code>FileDescriptor</code> object 99 * is created to represent this file connection. 100 * <p> 101 * First, if there is a security manager, 102 * its <code>checkRead</code> method is called 103 * with the path represented by the <code>file</code> 104 * argument as its argument. 105 * <p> 106 * If the named file does not exist, is a directory rather than a regular 107 * file, or for some other reason cannot be opened for reading then a 108 * <code>FileNotFoundException</code> is thrown. 109 * 110 * @param file the file to be opened for reading. 111 * @exception FileNotFoundException if the file does not exist, 112 * is a directory rather than a regular file, 113 * or for some other reason cannot be opened for 114 * reading. 115 * @exception SecurityException if a security manager exists and its 116 * <code>checkRead</code> method denies read access to the file. 117 * @see java.io.File#getPath() 118 * @see java.lang.SecurityManager#checkRead(java.lang.String) 119 */ 120 public FileInputStream(File file) throws FileNotFoundException { 121 String name = (file != null ? file.getPath() : null); 122 SecurityManager security = System.getSecurityManager(); 123 if (security != null) { 124 security.checkRead(name); 125 } 126 if (name == null) { 127 throw new NullPointerException(); 128 } 129 if (file.isInvalid()) { 130 throw new FileNotFoundException("Invalid file path"); 131 } 132 fd = new FileDescriptor(); 133 fd.attach(this); 134 path = name; 135 open(name); 136 } 137 138 /** 139 * Creates a <code>FileInputStream</code> by using the file descriptor 140 * <code>fdObj</code>, which represents an existing connection to an 141 * actual file in the file system. 142 * <p> 143 * If there is a security manager, its <code>checkRead</code> method is 144 * called with the file descriptor <code>fdObj</code> as its argument to 145 * see if it's ok to read the file descriptor. If read access is denied 146 * to the file descriptor a <code>SecurityException</code> is thrown. 147 * <p> 148 * If <code>fdObj</code> is null then a <code>NullPointerException</code> 149 * is thrown. 150 * <p> 151 * This constructor does not throw an exception if <code>fdObj</code> 152 * is {@link java.io.FileDescriptor#valid() invalid}. 153 * However, if the methods are invoked on the resulting stream to attempt 154 * I/O on the stream, an <code>IOException</code> is thrown. 155 * 156 * @param fdObj the file descriptor to be opened for reading. 157 * @throws SecurityException if a security manager exists and its 158 * <code>checkRead</code> method denies read access to the 159 * file descriptor. 160 * @see SecurityManager#checkRead(java.io.FileDescriptor) 161 */ 162 public FileInputStream(FileDescriptor fdObj) { 163 SecurityManager security = System.getSecurityManager(); 164 if (fdObj == null) { 165 throw new NullPointerException(); 166 } 167 if (security != null) { 168 security.checkRead(fdObj); 169 } 170 fd = fdObj; 171 path = null; 172 173 /* 174 * FileDescriptor is being shared by streams. 175 * Register this stream with FileDescriptor tracker. 176 */ 177 fd.attach(this); 178 } 179 180 /** 181 * Opens the specified file for reading. 182 * @param name the name of the file 183 */ 184 private native void open(String name) throws FileNotFoundException; 185 186 /** 187 * Reads a byte of data from this input stream. This method blocks 188 * if no input is yet available. 189 * 190 * @return the next byte of data, or <code>-1</code> if the end of the 191 * file is reached. 192 * @exception IOException if an I/O error occurs. 193 */ 194 public native int read() throws IOException; 195 196 /** 197 * Reads a subarray as a sequence of bytes. 198 * @param b the data to be written 199 * @param off the start offset in the data 200 * @param len the number of bytes that are written 201 * @exception IOException If an I/O error has occurred. 202 */ 203 private native int readBytes(byte b[], int off, int len) throws IOException; 204 205 /** 206 * Reads up to <code>b.length</code> bytes of data from this input 207 * stream into an array of bytes. This method blocks until some input 208 * is available. 209 * 210 * @param b the buffer into which the data is read. 211 * @return the total number of bytes read into the buffer, or 212 * <code>-1</code> if there is no more data because the end of 213 * the file has been reached. 214 * @exception IOException if an I/O error occurs. 215 */ 216 public int read(byte b[]) throws IOException { 217 return readBytes(b, 0, b.length); 218 } 219 220 /** 221 * Reads up to <code>len</code> bytes of data from this input stream 222 * into an array of bytes. If <code>len</code> is not zero, the method 223 * blocks until some input is available; otherwise, no 224 * bytes are read and <code>0</code> is returned. 225 * 226 * @param b the buffer into which the data is read. 227 * @param off the start offset in the destination array <code>b</code> 228 * @param len the maximum number of bytes read. 229 * @return the total number of bytes read into the buffer, or 230 * <code>-1</code> if there is no more data because the end of 231 * the file has been reached. 232 * @exception NullPointerException If <code>b</code> is <code>null</code>. 233 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 234 * <code>len</code> is negative, or <code>len</code> is greater than 235 * <code>b.length - off</code> 236 * @exception IOException if an I/O error occurs. 237 */ 238 public int read(byte b[], int off, int len) throws IOException { 239 return readBytes(b, off, len); 240 } 241 242 /** 243 * Skips over and discards <code>n</code> bytes of data from the 244 * input stream. 245 * 246 * <p>The <code>skip</code> method may, for a variety of 247 * reasons, end up skipping over some smaller number of bytes, 248 * possibly <code>0</code>. If <code>n</code> is negative, the method 249 * will try to skip backwards. In case the backing file does not support 250 * backward skip at its current position, an <code>IOException</code> is 251 * thrown. The actual number of bytes skipped is returned. If it skips 252 * forwards, it returns a positive value. If it skips backwards, it 253 * returns a negative value. 254 * 255 * <p>This method may skip more bytes than what are remaining in the 256 * backing file. This produces no exception and the number of bytes skipped 257 * may include some number of bytes that were beyond the EOF of the 258 * backing file. Attempting to read from the stream after skipping past 259 * the end will result in -1 indicating the end of the file. 260 * 261 * @param n the number of bytes to be skipped. 262 * @return the actual number of bytes skipped. 263 * @exception IOException if n is negative, if the stream does not 264 * support seek, or if an I/O error occurs. 265 */ 266 public native long skip(long n) throws IOException; 267 268 /** 269 * Returns an estimate of the number of remaining bytes that can be read (or 270 * skipped over) from this input stream without blocking by the next 271 * invocation of a method for this input stream. Returns 0 when the file 272 * position is beyond EOF. The next invocation might be the same thread 273 * or another thread. A single read or skip of this many bytes will not 274 * block, but may read or skip fewer bytes. 275 * 276 * <p> In some cases, a non-blocking read (or skip) may appear to be 277 * blocked when it is merely slow, for example when reading large 278 * files over slow networks. 279 * 280 * @return an estimate of the number of remaining bytes that can be read 281 * (or skipped over) from this input stream without blocking. 282 * @exception IOException if this file input stream has been closed by calling 283 * {@code close} or an I/O error occurs. 284 */ 285 public native int available() throws IOException; 286 287 /** 288 * Closes this file input stream and releases any system resources 289 * associated with the stream. 290 * 291 * <p> If this stream has an associated channel then the channel is closed 292 * as well. 293 * 294 * @exception IOException if an I/O error occurs. 295 * 296 * @revised 1.4 297 * @spec JSR-51 298 */ 299 public void close() throws IOException { 300 synchronized (closeLock) { 301 if (closed) { 302 return; 303 } 304 closed = true; 305 } 306 if (channel != null) { 307 channel.close(); 308 } 309 310 fd.closeAll(new Closeable() { 311 public void close() throws IOException { 312 close0(); 313 } 314 }); 315 } 316 317 /** 318 * Returns the <code>FileDescriptor</code> 319 * object that represents the connection to 320 * the actual file in the file system being 321 * used by this <code>FileInputStream</code>. 322 * 323 * @return the file descriptor object associated with this stream. 324 * @exception IOException if an I/O error occurs. 325 * @see java.io.FileDescriptor 326 */ 327 public final FileDescriptor getFD() throws IOException { 328 if (fd != null) { 329 return fd; 330 } 331 throw new IOException(); 332 } 333 334 /** 335 * Returns the unique {@link java.nio.channels.FileChannel FileChannel} 336 * object associated with this file input stream. 337 * 338 * <p> The initial {@link java.nio.channels.FileChannel#position() 339 * position} of the returned channel will be equal to the 340 * number of bytes read from the file so far. Reading bytes from this 341 * stream will increment the channel's position. Changing the channel's 342 * position, either explicitly or by reading, will change this stream's 343 * file position. 344 * 345 * @return the file channel associated with this file input stream 346 * 347 * @since 1.4 348 * @spec JSR-51 349 */ 350 public FileChannel getChannel() { 351 synchronized (this) { 352 if (channel == null) { 353 channel = FileChannelImpl.open(fd, path, true, false, this); 354 } 355 return channel; 356 } 357 } 358 359 private static native void initIDs(); 360 361 private native void close0() throws IOException; 362 363 static { 364 initIDs(); 365 } 366 367 /** 368 * Ensures that the <code>close</code> method of this file input stream is 369 * called when there are no more references to it. 370 * 371 * @exception IOException if an I/O error occurs. 372 * @see java.io.FileInputStream#close() 373 */ 374 protected void finalize() throws IOException { 375 if ((fd != null) && (fd != FileDescriptor.in)) { 376 /* if fd is shared, the references in FileDescriptor 377 * will ensure that finalizer is only called when 378 * safe to do so. All references using the fd have 379 * become unreachable. We can call close() 380 */ 381 close(); 382 } 383 } 384 }