0N/A/*
6092N/A * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.nio.channels.FileChannel;
0N/Aimport sun.nio.ch.FileChannelImpl;
5503N/Aimport sun.misc.IoTrace;
0N/A
0N/A
0N/A/**
0N/A * A <code>FileInputStream</code> obtains input bytes
0N/A * from a file in a file system. What files
0N/A * are available depends on the host environment.
0N/A *
0N/A * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
0N/A * such as image data. For reading streams of characters, consider using
0N/A * <code>FileReader</code>.
0N/A *
0N/A * @author Arthur van Hoff
0N/A * @see java.io.File
0N/A * @see java.io.FileDescriptor
0N/A * @see java.io.FileOutputStream
3471N/A * @see java.nio.file.Files#newInputStream
0N/A * @since JDK1.0
0N/A */
0N/Apublic
0N/Aclass FileInputStream extends InputStream
0N/A{
0N/A /* File Descriptor - handle to the open file */
23N/A private final FileDescriptor fd;
0N/A
5503N/A /* The path of the referenced file (null if the stream is created with a file descriptor) */
5503N/A private final String path;
5503N/A
0N/A private FileChannel channel = null;
0N/A
23N/A private final Object closeLock = new Object();
0N/A private volatile boolean closed = false;
0N/A
5182N/A private static final ThreadLocal<Boolean> runningFinalize =
5182N/A new ThreadLocal<>();
5182N/A
5182N/A private static boolean isRunningFinalize() {
5182N/A Boolean val;
5182N/A if ((val = runningFinalize.get()) != null)
5182N/A return val.booleanValue();
5182N/A return false;
5182N/A }
5182N/A
0N/A /**
0N/A * Creates a <code>FileInputStream</code> by
0N/A * opening a connection to an actual file,
0N/A * the file named by the path name <code>name</code>
0N/A * in the file system. A new <code>FileDescriptor</code>
0N/A * object is created to represent this file
0N/A * connection.
0N/A * <p>
0N/A * First, if there is a security
0N/A * manager, its <code>checkRead</code> method
0N/A * is called with the <code>name</code> argument
0N/A * as its argument.
0N/A * <p>
0N/A * If the named file does not exist, is a directory rather than a regular
0N/A * file, or for some other reason cannot be opened for reading then a
0N/A * <code>FileNotFoundException</code> is thrown.
0N/A *
0N/A * @param name the system-dependent file name.
0N/A * @exception FileNotFoundException if the file does not exist,
0N/A * is a directory rather than a regular file,
0N/A * or for some other reason cannot be opened for
0N/A * reading.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkRead</code> method denies read access
0N/A * to the file.
0N/A * @see java.lang.SecurityManager#checkRead(java.lang.String)
0N/A */
0N/A public FileInputStream(String name) throws FileNotFoundException {
0N/A this(name != null ? new File(name) : null);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>FileInputStream</code> by
0N/A * opening a connection to an actual file,
0N/A * the file named by the <code>File</code>
0N/A * object <code>file</code> in the file system.
0N/A * A new <code>FileDescriptor</code> object
0N/A * is created to represent this file connection.
0N/A * <p>
0N/A * First, if there is a security manager,
0N/A * its <code>checkRead</code> method is called
0N/A * with the path represented by the <code>file</code>
0N/A * argument as its argument.
0N/A * <p>
0N/A * If the named file does not exist, is a directory rather than a regular
0N/A * file, or for some other reason cannot be opened for reading then a
0N/A * <code>FileNotFoundException</code> is thrown.
0N/A *
0N/A * @param file the file to be opened for reading.
0N/A * @exception FileNotFoundException if the file does not exist,
0N/A * is a directory rather than a regular file,
0N/A * or for some other reason cannot be opened for
0N/A * reading.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkRead</code> method denies read access to the file.
0N/A * @see java.io.File#getPath()
0N/A * @see java.lang.SecurityManager#checkRead(java.lang.String)
0N/A */
0N/A public FileInputStream(File file) throws FileNotFoundException {
0N/A String name = (file != null ? file.getPath() : null);
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A security.checkRead(name);
0N/A }
0N/A if (name == null) {
0N/A throw new NullPointerException();
0N/A }
6092N/A if (file.isInvalid()) {
6092N/A throw new FileNotFoundException("Invalid file path");
6092N/A }
0N/A fd = new FileDescriptor();
5182N/A fd.incrementAndGetUseCount();
5503N/A this.path = name;
0N/A open(name);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>FileInputStream</code> by using the file descriptor
0N/A * <code>fdObj</code>, which represents an existing connection to an
0N/A * actual file in the file system.
0N/A * <p>
0N/A * If there is a security manager, its <code>checkRead</code> method is
0N/A * called with the file descriptor <code>fdObj</code> as its argument to
0N/A * see if it's ok to read the file descriptor. If read access is denied
0N/A * to the file descriptor a <code>SecurityException</code> is thrown.
0N/A * <p>
0N/A * If <code>fdObj</code> is null then a <code>NullPointerException</code>
0N/A * is thrown.
0N/A * <p>
0N/A * This constructor does not throw an exception if <code>fdObj</code>
23N/A * is {@link java.io.FileDescriptor#valid() invalid}.
0N/A * However, if the methods are invoked on the resulting stream to attempt
0N/A * I/O on the stream, an <code>IOException</code> is thrown.
0N/A *
0N/A * @param fdObj the file descriptor to be opened for reading.
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkRead</code> method denies read access to the
0N/A * file descriptor.
0N/A * @see SecurityManager#checkRead(java.io.FileDescriptor)
0N/A */
0N/A public FileInputStream(FileDescriptor fdObj) {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (fdObj == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (security != null) {
0N/A security.checkRead(fdObj);
0N/A }
0N/A fd = fdObj;
5503N/A path = null;
0N/A
0N/A /*
0N/A * FileDescriptor is being shared by streams.
5182N/A * Ensure that it's GC'ed only when all the streams/channels are done
5182N/A * using it.
0N/A */
5182N/A fd.incrementAndGetUseCount();
0N/A }
0N/A
0N/A /**
0N/A * Opens the specified file for reading.
0N/A * @param name the name of the file
0N/A */
0N/A private native void open(String name) throws FileNotFoundException;
0N/A
0N/A /**
0N/A * Reads a byte of data from this input stream. This method blocks
0N/A * if no input is yet available.
0N/A *
0N/A * @return the next byte of data, or <code>-1</code> if the end of the
0N/A * file is reached.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
5503N/A public int read() throws IOException {
5503N/A Object traceContext = IoTrace.fileReadBegin(path);
5503N/A int b = 0;
5503N/A try {
5503N/A b = read0();
5503N/A } finally {
5503N/A IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);
5503N/A }
5503N/A return b;
5503N/A }
5503N/A
5503N/A private native int read0() throws IOException;
0N/A
0N/A /**
0N/A * Reads a subarray as a sequence of bytes.
0N/A * @param b the data to be written
0N/A * @param off the start offset in the data
0N/A * @param len the number of bytes that are written
0N/A * @exception IOException If an I/O error has occurred.
0N/A */
0N/A private native int readBytes(byte b[], int off, int len) throws IOException;
0N/A
0N/A /**
0N/A * Reads up to <code>b.length</code> bytes of data from this input
0N/A * stream into an array of bytes. This method blocks until some input
0N/A * is available.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @return the total number of bytes read into the buffer, or
0N/A * <code>-1</code> if there is no more data because the end of
0N/A * the file has been reached.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public int read(byte b[]) throws IOException {
5503N/A Object traceContext = IoTrace.fileReadBegin(path);
5503N/A int bytesRead = 0;
5503N/A try {
5503N/A bytesRead = readBytes(b, 0, b.length);
5503N/A } finally {
5503N/A IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
5503N/A }
5503N/A return bytesRead;
0N/A }
0N/A
0N/A /**
0N/A * Reads up to <code>len</code> bytes of data from this input stream
0N/A * into an array of bytes. If <code>len</code> is not zero, the method
0N/A * blocks until some input is available; otherwise, no
0N/A * bytes are read and <code>0</code> is returned.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @param off the start offset in the destination array <code>b</code>
0N/A * @param len the maximum number of bytes read.
0N/A * @return the total number of bytes read into the buffer, or
0N/A * <code>-1</code> if there is no more data because the end of
0N/A * the file has been reached.
0N/A * @exception NullPointerException If <code>b</code> is <code>null</code>.
0N/A * @exception IndexOutOfBoundsException If <code>off</code> is negative,
0N/A * <code>len</code> is negative, or <code>len</code> is greater than
0N/A * <code>b.length - off</code>
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public int read(byte b[], int off, int len) throws IOException {
5503N/A Object traceContext = IoTrace.fileReadBegin(path);
5503N/A int bytesRead = 0;
5503N/A try {
5503N/A bytesRead = readBytes(b, off, len);
5503N/A } finally {
5503N/A IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
5503N/A }
5503N/A return bytesRead;
0N/A }
0N/A
0N/A /**
0N/A * Skips over and discards <code>n</code> bytes of data from the
0N/A * input stream.
0N/A *
0N/A * <p>The <code>skip</code> method may, for a variety of
0N/A * reasons, end up skipping over some smaller number of bytes,
0N/A * possibly <code>0</code>. If <code>n</code> is negative, an
0N/A * <code>IOException</code> is thrown, even though the <code>skip</code>
0N/A * method of the {@link InputStream} superclass does nothing in this case.
0N/A * The actual number of bytes skipped is returned.
0N/A *
0N/A * <p>This method may skip more bytes than are remaining in the backing
0N/A * file. This produces no exception and the number of bytes skipped
0N/A * may include some number of bytes that were beyond the EOF of the
0N/A * backing file. Attempting to read from the stream after skipping past
0N/A * the end will result in -1 indicating the end of the file.
0N/A *
0N/A * @param n the number of bytes to be skipped.
0N/A * @return the actual number of bytes skipped.
0N/A * @exception IOException if n is negative, if the stream does not
0N/A * support seek, or if an I/O error occurs.
0N/A */
0N/A public native long skip(long n) throws IOException;
0N/A
0N/A /**
0N/A * Returns an estimate of the number of remaining bytes that can be read (or
0N/A * skipped over) from this input stream without blocking by the next
0N/A * invocation of a method for this input stream. The next invocation might be
0N/A * the same thread or another thread. A single read or skip of this
0N/A * many bytes will not block, but may read or skip fewer bytes.
0N/A *
0N/A * <p> In some cases, a non-blocking read (or skip) may appear to be
0N/A * blocked when it is merely slow, for example when reading large
0N/A * files over slow networks.
0N/A *
0N/A * @return an estimate of the number of remaining bytes that can be read
0N/A * (or skipped over) from this input stream without blocking.
0N/A * @exception IOException if this file input stream has been closed by calling
0N/A * {@code close} or an I/O error occurs.
0N/A */
0N/A public native int available() throws IOException;
0N/A
0N/A /**
0N/A * Closes this file input stream and releases any system resources
0N/A * associated with the stream.
0N/A *
0N/A * <p> If this stream has an associated channel then the channel is closed
0N/A * as well.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A *
0N/A * @revised 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public void close() throws IOException {
0N/A synchronized (closeLock) {
0N/A if (closed) {
0N/A return;
0N/A }
0N/A closed = true;
0N/A }
0N/A if (channel != null) {
5182N/A /*
5182N/A * Decrement the FD use count associated with the channel
5182N/A * The use count is incremented whenever a new channel
5182N/A * is obtained from this stream.
5182N/A */
5182N/A fd.decrementAndGetUseCount();
0N/A channel.close();
0N/A }
5182N/A
5182N/A /*
5182N/A * Decrement the FD use count associated with this stream
5182N/A */
5182N/A int useCount = fd.decrementAndGetUseCount();
5182N/A
5182N/A /*
5182N/A * If FileDescriptor is still in use by another stream, the finalizer
5182N/A * will not close it.
5182N/A */
5182N/A if ((useCount <= 0) || !isRunningFinalize()) {
5182N/A close0();
5182N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>FileDescriptor</code>
0N/A * object that represents the connection to
0N/A * the actual file in the file system being
0N/A * used by this <code>FileInputStream</code>.
0N/A *
0N/A * @return the file descriptor object associated with this stream.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FileDescriptor
0N/A */
0N/A public final FileDescriptor getFD() throws IOException {
5182N/A if (fd != null) return fd;
0N/A throw new IOException();
0N/A }
0N/A
0N/A /**
0N/A * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
0N/A * object associated with this file input stream.
0N/A *
0N/A * <p> The initial {@link java.nio.channels.FileChannel#position()
0N/A * </code>position<code>} of the returned channel will be equal to the
0N/A * number of bytes read from the file so far. Reading bytes from this
0N/A * stream will increment the channel's position. Changing the channel's
0N/A * position, either explicitly or by reading, will change this stream's
0N/A * file position.
0N/A *
0N/A * @return the file channel associated with this file input stream
0N/A *
0N/A * @since 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public FileChannel getChannel() {
0N/A synchronized (this) {
0N/A if (channel == null) {
5503N/A channel = FileChannelImpl.open(fd, path, true, false, this);
5182N/A
5182N/A /*
5182N/A * Increment fd's use count. Invoking the channel's close()
5182N/A * method will result in decrementing the use count set for
5182N/A * the channel.
5182N/A */
5182N/A fd.incrementAndGetUseCount();
0N/A }
0N/A return channel;
0N/A }
0N/A }
0N/A
0N/A private static native void initIDs();
0N/A
0N/A private native void close0() throws IOException;
0N/A
0N/A static {
0N/A initIDs();
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the <code>close</code> method of this file input stream is
0N/A * called when there are no more references to it.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FileInputStream#close()
0N/A */
0N/A protected void finalize() throws IOException {
23N/A if ((fd != null) && (fd != FileDescriptor.in)) {
5182N/A
5182N/A /*
5182N/A * Finalizer should not release the FileDescriptor if another
5182N/A * stream is still using it. If the user directly invokes
5182N/A * close() then the FileDescriptor is also released.
4474N/A */
5182N/A runningFinalize.set(Boolean.TRUE);
5182N/A try {
5182N/A close();
5182N/A } finally {
5182N/A runningFinalize.set(Boolean.FALSE);
5182N/A }
0N/A }
0N/A }
0N/A}