java.io

Class FileInputStream

Implemented Interfaces:
Closeable

public class FileInputStream
extends InputStream

This class is a stream that reads its bytes from a file.

Constructor Summary

FileInputStream(File file)
This method initializes a FileInputStream to read from the specified File object.
FileInputStream(FileDescriptor fdObj)
This method initializes a FileInputStream to read from the specified FileDescriptor object.
FileInputStream(String name)
This method initializes a FileInputStream to read from the specified named file.

Method Summary

int
available()
This method returns the number of bytes that can be read from this stream before a read can block.
void
close()
This method closes the stream.
protected void
finalize()
Called on an object by the Virtual Machine at most once, at some point after the Object is determined unreachable but before it is destroyed.
FileChannel
getChannel()
This method creates a java.nio.channels.FileChannel.
FileDescriptor
getFD()
This method returns a FileDescriptor object representing the underlying native file handle of the file this stream is reading from
int
read()
This method reads an unsigned byte from the input stream and returns it as an int in the range of 0-255.
int
read(byte[] buf)
This method reads bytes from a stream and stores them into a caller supplied buffer.
int
read(byte[] buf, int offset, int len)
This method read bytes from a stream and stores them into a caller supplied buffer.
long
skip(long numBytes)
This method skips the specified number of bytes in the stream.

Methods inherited from class java.io.InputStream

available, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

FileInputStream

public FileInputStream(File file)
            throws FileNotFoundException
This method initializes a FileInputStream to read from the specified File object. A security check is first made to determine whether or not access to this file is allowed. This is done by calling the checkRead() method of the SecurityManager (if one exists) with the name of this file. An exception is thrown if reading is not allowed. If the file does not exist, an exception is also thrown.
Parameters:
file - The File object this stream should read from
Throws:
SecurityException - If read access to the file is not allowed
FileNotFoundException - If the file does not exist or if it is a directory.

FileInputStream

public FileInputStream(FileDescriptor fdObj)
This method initializes a FileInputStream to read from the specified FileDescriptor object. A security check is first made to determine whether or not access to this file is allowed. This is done by calling the checkRead() method of the SecurityManager (if one exists) with the specified FileDescriptor An exception is thrown if reading is not allowed.
Parameters:
fdObj - The FileDescriptor object this stream should read from
Throws:
SecurityException - If read access to the file is not allowed

FileInputStream

public FileInputStream(String name)
            throws FileNotFoundException
This method initializes a FileInputStream to read from the specified named file. A security check is first made to determine whether or not access to this file is allowed. This is done by calling the checkRead() method of the SecurityManager (if one exists) with the name of this file. An exception is thrown if reading is not allowed. If the file does not exist, an exception is also thrown.
Parameters:
name - The name of the file this stream should read from
Throws:
SecurityException - If read access to the file is not allowed
FileNotFoundException - If the file does not exist or if it is a directory

Method Details

available

public int available()
            throws IOException
This method returns the number of bytes that can be read from this stream before a read can block. A return of 0 indicates that blocking might (or might not) occur on the very next read attempt.

This method returns the number of unread bytes remaining in the file if the descriptor being read from is an actual file. If this method is reading from a ''special'' file such a the standard input, this method will return the appropriate value for the stream being read.

Be aware that reads on plain files that do not reside locally might possibly block even if this method says they should not. For example, a remote server might crash, preventing an NFS mounted file from being read.

Overrides:
available in interface InputStream
Returns:
The number of bytes that can be read before blocking could occur
Throws:
IOException - If an error occurs

close

public void close()
            throws IOException
This method closes the stream. Any futher attempts to read from the stream will likely generate an IOException since the underlying file will be closed.
Specified by:
close in interface Closeable
Overrides:
close in interface InputStream
Throws:
IOException - If an error occurs.

finalize

protected void finalize()
            throws IOException
Called on an object by the Virtual Machine at most once, at some point after the Object is determined unreachable but before it is destroyed. You would think that this means it eventually is called on every Object, but this is not necessarily the case. If execution terminates abnormally, garbage collection does not always happen. Thus you cannot rely on this method to always work. For finer control over garbage collection, use references from the java.lang.ref package.

Virtual Machines are free to not call this method if they can determine that it does nothing important; for example, if your class extends Object and overrides finalize to do simply super.finalize().

finalize() will be called by a Thread that has no locks on any Objects, and may be called concurrently. There are no guarantees on the order in which multiple objects are finalized. This means that finalize() is usually unsuited for performing actions that must be thread-safe, and that your implementation must be use defensive programming if it is to always work.

If an Exception is thrown from finalize() during garbage collection, it will be patently ignored and the Object will still be destroyed.

It is allowed, although not typical, for user code to call finalize() directly. User invocation does not affect whether automatic invocation will occur. It is also permitted, although not recommended, for a finalize() method to "revive" an object by making it reachable from normal code again.

Unlike constructors, finalize() does not get called for an object's superclass unless the implementation specifically calls super.finalize().

The default implementation does nothing.

Overrides:
finalize in interface Object

getChannel

public FileChannel getChannel()
This method creates a java.nio.channels.FileChannel. Nio does not allow one to create a file channel directly. A file channel must be created by first creating an instance of Input/Output/RandomAccessFile and invoking the getChannel() method on it.

getFD

public final FileDescriptor getFD()
            throws IOException
This method returns a FileDescriptor object representing the underlying native file handle of the file this stream is reading from
Returns:
A FileDescriptor for this stream
Throws:
IOException - If an error occurs

read

public int read()
            throws IOException
This method reads an unsigned byte from the input stream and returns it as an int in the range of 0-255. This method also will return -1 if the end of the stream has been reached.

This method will block until the byte can be read.

Overrides:
read in interface InputStream
Returns:
The byte read or -1 if end of stream
Throws:
IOException - If an error occurs

read

public int read(byte[] buf)
            throws IOException
This method reads bytes from a stream and stores them into a caller supplied buffer. This method attempts to completely fill the buffer, but can return before doing so. The actual number of bytes read is returned as an int. A -1 is returned to indicate the end of the stream.

This method will block until some data can be read.

This method operates by calling an overloaded read method like so: read(buf, 0, buf.length)

Overrides:
read in interface InputStream
Parameters:
buf - The buffer into which the bytes read will be stored.
Returns:
The number of bytes read or -1 if end of stream.
Throws:
IOException - If an error occurs.

read

public int read(byte[] buf,
                int offset,
                int len)
            throws IOException
This method read bytes from a stream and stores them into a caller supplied buffer. It starts storing the data at index offset into the buffer and attempts to read len bytes. This method can return before reading the number of bytes requested. The actual number of bytes read is returned as an int. A -1 is returned to indicate the end of the stream.

This method will block until some data can be read.

Overrides:
read in interface InputStream
Parameters:
buf - The array into which the bytes read should be stored
offset - The offset into the array to start storing bytes
len - The requested number of bytes to read
Returns:
The actual number of bytes read, or -1 if end of stream.
Throws:
IOException - If an error occurs.

skip

public long skip(long numBytes)
            throws IOException
This method skips the specified number of bytes in the stream. It returns the actual number of bytes skipped, which may be less than the requested amount.

Overrides:
skip in interface InputStream
Parameters:
numBytes - The requested number of bytes to skip
Returns:
The actual number of bytes skipped.
Throws:
IOException - If an error occurs

FileInputStream.java -- An input stream that reads from disk files. Copyright (C) 1998, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.