J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1994-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | * @since JDK1.0 |
| 46 | */ |
| 47 | public |
| 48 | class FileInputStream extends InputStream |
| 49 | { |
| 50 | /* File Descriptor - handle to the open file */ |
| 51 | private FileDescriptor fd; |
| 52 | |
| 53 | private FileChannel channel = null; |
| 54 | |
| 55 | private Object closeLock = new Object(); |
| 56 | private volatile boolean closed = false; |
| 57 | |
| 58 | private static ThreadLocal<Boolean> runningFinalize = |
| 59 | new ThreadLocal<Boolean>(); |
| 60 | |
| 61 | private static boolean isRunningFinalize() { |
| 62 | Boolean val; |
| 63 | if ((val = runningFinalize.get()) != null) |
| 64 | return val.booleanValue(); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a <code>FileInputStream</code> by |
| 70 | * opening a connection to an actual file, |
| 71 | * the file named by the path name <code>name</code> |
| 72 | * in the file system. A new <code>FileDescriptor</code> |
| 73 | * object is created to represent this file |
| 74 | * connection. |
| 75 | * <p> |
| 76 | * First, if there is a security |
| 77 | * manager, its <code>checkRead</code> method |
| 78 | * is called with the <code>name</code> argument |
| 79 | * as its argument. |
| 80 | * <p> |
| 81 | * If the named file does not exist, is a directory rather than a regular |
| 82 | * file, or for some other reason cannot be opened for reading then a |
| 83 | * <code>FileNotFoundException</code> is thrown. |
| 84 | * |
| 85 | * @param name the system-dependent file name. |
| 86 | * @exception FileNotFoundException if the file does not exist, |
| 87 | * is a directory rather than a regular file, |
| 88 | * or for some other reason cannot be opened for |
| 89 | * reading. |
| 90 | * @exception SecurityException if a security manager exists and its |
| 91 | * <code>checkRead</code> method denies read access |
| 92 | * to the file. |
| 93 | * @see java.lang.SecurityManager#checkRead(java.lang.String) |
| 94 | */ |
| 95 | public FileInputStream(String name) throws FileNotFoundException { |
| 96 | this(name != null ? new File(name) : null); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates a <code>FileInputStream</code> by |
| 101 | * opening a connection to an actual file, |
| 102 | * the file named by the <code>File</code> |
| 103 | * object <code>file</code> in the file system. |
| 104 | * A new <code>FileDescriptor</code> object |
| 105 | * is created to represent this file connection. |
| 106 | * <p> |
| 107 | * First, if there is a security manager, |
| 108 | * its <code>checkRead</code> method is called |
| 109 | * with the path represented by the <code>file</code> |
| 110 | * argument as its argument. |
| 111 | * <p> |
| 112 | * If the named file does not exist, is a directory rather than a regular |
| 113 | * file, or for some other reason cannot be opened for reading then a |
| 114 | * <code>FileNotFoundException</code> is thrown. |
| 115 | * |
| 116 | * @param file the file to be opened for reading. |
| 117 | * @exception FileNotFoundException if the file does not exist, |
| 118 | * is a directory rather than a regular file, |
| 119 | * or for some other reason cannot be opened for |
| 120 | * reading. |
| 121 | * @exception SecurityException if a security manager exists and its |
| 122 | * <code>checkRead</code> method denies read access to the file. |
| 123 | * @see java.io.File#getPath() |
| 124 | * @see java.lang.SecurityManager#checkRead(java.lang.String) |
| 125 | */ |
| 126 | public FileInputStream(File file) throws FileNotFoundException { |
| 127 | String name = (file != null ? file.getPath() : null); |
| 128 | SecurityManager security = System.getSecurityManager(); |
| 129 | if (security != null) { |
| 130 | security.checkRead(name); |
| 131 | } |
| 132 | if (name == null) { |
| 133 | throw new NullPointerException(); |
| 134 | } |
| 135 | fd = new FileDescriptor(); |
| 136 | fd.incrementAndGetUseCount(); |
| 137 | open(name); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Creates a <code>FileInputStream</code> by using the file descriptor |
| 142 | * <code>fdObj</code>, which represents an existing connection to an |
| 143 | * actual file in the file system. |
| 144 | * <p> |
| 145 | * If there is a security manager, its <code>checkRead</code> method is |
| 146 | * called with the file descriptor <code>fdObj</code> as its argument to |
| 147 | * see if it's ok to read the file descriptor. If read access is denied |
| 148 | * to the file descriptor a <code>SecurityException</code> is thrown. |
| 149 | * <p> |
| 150 | * If <code>fdObj</code> is null then a <code>NullPointerException</code> |
| 151 | * is thrown. |
| 152 | * <p> |
| 153 | * This constructor does not throw an exception if <code>fdObj</code> |
| 154 | * is {link java.io.FileDescriptor#valid() invalid}. |
| 155 | * However, if the methods are invoked on the resulting stream to attempt |
| 156 | * I/O on the stream, an <code>IOException</code> is thrown. |
| 157 | * |
| 158 | * @param fdObj the file descriptor to be opened for reading. |
| 159 | * @throws SecurityException if a security manager exists and its |
| 160 | * <code>checkRead</code> method denies read access to the |
| 161 | * file descriptor. |
| 162 | * @see SecurityManager#checkRead(java.io.FileDescriptor) |
| 163 | */ |
| 164 | public FileInputStream(FileDescriptor fdObj) { |
| 165 | SecurityManager security = System.getSecurityManager(); |
| 166 | if (fdObj == null) { |
| 167 | throw new NullPointerException(); |
| 168 | } |
| 169 | if (security != null) { |
| 170 | security.checkRead(fdObj); |
| 171 | } |
| 172 | fd = fdObj; |
| 173 | |
| 174 | /* |
| 175 | * FileDescriptor is being shared by streams. |
| 176 | * Ensure that it's GC'ed only when all the streams/channels are done |
| 177 | * using it. |
| 178 | */ |
| 179 | fd.incrementAndGetUseCount(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Opens the specified file for reading. |
| 184 | * @param name the name of the file |
| 185 | */ |
| 186 | private native void open(String name) throws FileNotFoundException; |
| 187 | |
| 188 | /** |
| 189 | * Reads a byte of data from this input stream. This method blocks |
| 190 | * if no input is yet available. |
| 191 | * |
| 192 | * @return the next byte of data, or <code>-1</code> if the end of the |
| 193 | * file is reached. |
| 194 | * @exception IOException if an I/O error occurs. |
| 195 | */ |
| 196 | public native int read() throws IOException; |
| 197 | |
| 198 | /** |
| 199 | * Reads a subarray as a sequence of bytes. |
| 200 | * @param b the data to be written |
| 201 | * @param off the start offset in the data |
| 202 | * @param len the number of bytes that are written |
| 203 | * @exception IOException If an I/O error has occurred. |
| 204 | */ |
| 205 | private native int readBytes(byte b[], int off, int len) throws IOException; |
| 206 | |
| 207 | /** |
| 208 | * Reads up to <code>b.length</code> bytes of data from this input |
| 209 | * stream into an array of bytes. This method blocks until some input |
| 210 | * is available. |
| 211 | * |
| 212 | * @param b the buffer into which the data is read. |
| 213 | * @return the total number of bytes read into the buffer, or |
| 214 | * <code>-1</code> if there is no more data because the end of |
| 215 | * the file has been reached. |
| 216 | * @exception IOException if an I/O error occurs. |
| 217 | */ |
| 218 | public int read(byte b[]) throws IOException { |
| 219 | return readBytes(b, 0, b.length); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Reads up to <code>len</code> bytes of data from this input stream |
| 224 | * into an array of bytes. If <code>len</code> is not zero, the method |
| 225 | * blocks until some input is available; otherwise, no |
| 226 | * bytes are read and <code>0</code> is returned. |
| 227 | * |
| 228 | * @param b the buffer into which the data is read. |
| 229 | * @param off the start offset in the destination array <code>b</code> |
| 230 | * @param len the maximum number of bytes read. |
| 231 | * @return the total number of bytes read into the buffer, or |
| 232 | * <code>-1</code> if there is no more data because the end of |
| 233 | * the file has been reached. |
| 234 | * @exception NullPointerException If <code>b</code> is <code>null</code>. |
| 235 | * @exception IndexOutOfBoundsException If <code>off</code> is negative, |
| 236 | * <code>len</code> is negative, or <code>len</code> is greater than |
| 237 | * <code>b.length - off</code> |
| 238 | * @exception IOException if an I/O error occurs. |
| 239 | */ |
| 240 | public int read(byte b[], int off, int len) throws IOException { |
| 241 | return readBytes(b, off, len); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Skips over and discards <code>n</code> bytes of data from the |
| 246 | * input stream. |
| 247 | * |
| 248 | * <p>The <code>skip</code> method may, for a variety of |
| 249 | * reasons, end up skipping over some smaller number of bytes, |
| 250 | * possibly <code>0</code>. If <code>n</code> is negative, an |
| 251 | * <code>IOException</code> is thrown, even though the <code>skip</code> |
| 252 | * method of the {@link InputStream} superclass does nothing in this case. |
| 253 | * The actual number of bytes skipped is returned. |
| 254 | * |
| 255 | * <p>This method may skip more bytes than are remaining in the backing |
| 256 | * 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. The next invocation might be |
| 272 | * the same thread or another thread. A single read or skip of this |
| 273 | * many bytes will not block, but may read or skip fewer bytes. |
| 274 | * |
| 275 | * <p> In some cases, a non-blocking read (or skip) may appear to be |
| 276 | * blocked when it is merely slow, for example when reading large |
| 277 | * files over slow networks. |
| 278 | * |
| 279 | * @return an estimate of the number of remaining bytes that can be read |
| 280 | * (or skipped over) from this input stream without blocking. |
| 281 | * @exception IOException if this file input stream has been closed by calling |
| 282 | * {@code close} or an I/O error occurs. |
| 283 | */ |
| 284 | public native int available() throws IOException; |
| 285 | |
| 286 | /** |
| 287 | * Closes this file input stream and releases any system resources |
| 288 | * associated with the stream. |
| 289 | * |
| 290 | * <p> If this stream has an associated channel then the channel is closed |
| 291 | * as well. |
| 292 | * |
| 293 | * @exception IOException if an I/O error occurs. |
| 294 | * |
| 295 | * @revised 1.4 |
| 296 | * @spec JSR-51 |
| 297 | */ |
| 298 | public void close() throws IOException { |
| 299 | synchronized (closeLock) { |
| 300 | if (closed) { |
| 301 | return; |
| 302 | } |
| 303 | closed = true; |
| 304 | } |
| 305 | if (channel != null) { |
| 306 | /* |
| 307 | * Decrement the FD use count associated with the channel |
| 308 | * The use count is incremented whenever a new channel |
| 309 | * is obtained from this stream. |
| 310 | */ |
| 311 | fd.decrementAndGetUseCount(); |
| 312 | channel.close(); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Decrement the FD use count associated with this stream |
| 317 | */ |
| 318 | int useCount = fd.decrementAndGetUseCount(); |
| 319 | |
| 320 | /* |
| 321 | * If FileDescriptor is still in use by another stream, the finalizer |
| 322 | * will not close it. |
| 323 | */ |
| 324 | if ((useCount <= 0) || !isRunningFinalize()) { |
| 325 | close0(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Returns the <code>FileDescriptor</code> |
| 331 | * object that represents the connection to |
| 332 | * the actual file in the file system being |
| 333 | * used by this <code>FileInputStream</code>. |
| 334 | * |
| 335 | * @return the file descriptor object associated with this stream. |
| 336 | * @exception IOException if an I/O error occurs. |
| 337 | * @see java.io.FileDescriptor |
| 338 | */ |
| 339 | public final FileDescriptor getFD() throws IOException { |
| 340 | if (fd != null) return fd; |
| 341 | throw new IOException(); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
| 346 | * object associated with this file input stream. |
| 347 | * |
| 348 | * <p> The initial {@link java.nio.channels.FileChannel#position() |
| 349 | * </code>position<code>} of the returned channel will be equal to the |
| 350 | * number of bytes read from the file so far. Reading bytes from this |
| 351 | * stream will increment the channel's position. Changing the channel's |
| 352 | * position, either explicitly or by reading, will change this stream's |
| 353 | * file position. |
| 354 | * |
| 355 | * @return the file channel associated with this file input stream |
| 356 | * |
| 357 | * @since 1.4 |
| 358 | * @spec JSR-51 |
| 359 | */ |
| 360 | public FileChannel getChannel() { |
| 361 | synchronized (this) { |
| 362 | if (channel == null) { |
| 363 | channel = FileChannelImpl.open(fd, true, false, this); |
| 364 | |
| 365 | /* |
| 366 | * Increment fd's use count. Invoking the channel's close() |
| 367 | * method will result in decrementing the use count set for |
| 368 | * the channel. |
| 369 | */ |
| 370 | fd.incrementAndGetUseCount(); |
| 371 | } |
| 372 | return channel; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | private static native void initIDs(); |
| 377 | |
| 378 | private native void close0() throws IOException; |
| 379 | |
| 380 | static { |
| 381 | initIDs(); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Ensures that the <code>close</code> method of this file input stream is |
| 386 | * called when there are no more references to it. |
| 387 | * |
| 388 | * @exception IOException if an I/O error occurs. |
| 389 | * @see java.io.FileInputStream#close() |
| 390 | */ |
| 391 | protected void finalize() throws IOException { |
| 392 | if ((fd != null) && (fd != fd.in)) { |
| 393 | |
| 394 | /* |
| 395 | * Finalizer should not release the FileDescriptor if another |
| 396 | * stream is still using it. If the user directly invokes |
| 397 | * close() then the FileDescriptor is also released. |
| 398 | */ |
| 399 | runningFinalize.set(Boolean.TRUE); |
| 400 | try { |
| 401 | close(); |
| 402 | } finally { |
| 403 | runningFinalize.set(Boolean.FALSE); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |