FileInputStreampublic class FileInputStream extends InputStream implements CloseableA specialized {@link InputStream} that reads from a file in the file system.
All read requests made by calling methods in this class are directly
forwarded to the equivalent function of the underlying operating system.
Since this may induce some performance penalty, in particular if many small
read requests are made, a FileInputStream is often wrapped by a
BufferedInputStream. |
Fields Summary |
---|
FileDescriptor | fdThe {@link FileDescriptor} representing this {@code FileInputStream}. | private FileChannel | channel | boolean | innerFD | private org.apache.harmony.luni.platform.IFileSystem | fileSystem | private Object | repositioningLock |
Constructors Summary |
---|
public FileInputStream(File file)Constructs a new {@code FileInputStream} based on {@code file}.
super();
SecurityManager security = System.getSecurityManager();
if (security != null) {
String filePath = (null == file ? null : file.getPath());
security.checkRead(filePath);
}
fd = new FileDescriptor();
fd.readOnly = true;
fd.descriptor = fileSystem.open(file.properPath(true),
IFileSystem.O_RDONLY);
innerFD = true;
// BEGIN android-removed
// channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
// IFileSystem.O_RDONLY);
// END android-removed
| public FileInputStream(FileDescriptor fd)Constructs a new {@code FileInputStream} on the {@link FileDescriptor}
{@code fd}. The file must already be open, therefore no
{@code FileNotFoundException} will be thrown.
super();
if (fd == null) {
throw new NullPointerException();
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(fd);
}
this.fd = fd;
innerFD = false;
// BEGIN android-removed
// channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
// IFileSystem.O_RDONLY);
// END android-removed
| public FileInputStream(String fileName)Constructs a new {@code FileInputStream} on the file named
{@code fileName}. The path of {@code fileName} may be absolute or
relative to the system property {@code "user.dir"}.
this(null == fileName ? (File) null : new File(fileName));
|
Methods Summary |
---|
public int | available()Returns the number of bytes that are available before this stream will
block. This method always returns the size of the file minus the current
position.
openCheck();
// BEGIN android-added
// Android always uses the ioctl() method of determining bytes
// available. See the long discussion in
// org_apache_harmony_luni_platform_OSFileSystem.cpp about its
// use.
return fileSystem.ioctlAvailable(fd.descriptor);
// END android-added
// BEGIN android-deleted
// synchronized (repositioningLock) {
// // stdin requires special handling
// if (fd == FileDescriptor.in) {
// return (int) fileSystem.ttyAvailable();
// }
//
// long currentPosition = fileSystem.seek(fd.descriptor, 0L,
// IFileSystem.SEEK_CUR);
// long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
// IFileSystem.SEEK_END);
// fileSystem.seek(fd.descriptor, currentPosition,
// IFileSystem.SEEK_SET);
// return (int) (endOfFilePosition - currentPosition);
// }
// END android-deleted
| public void | close()Closes this stream.
// BEGIN android-changed
synchronized (this) {
if (channel != null && channel.isOpen()) {
channel.close();
channel = null;
}
if (fd != null && fd.descriptor >= 0 && innerFD) {
fileSystem.close(fd.descriptor);
fd.descriptor = -1;
}
}
// END android-changed
| protected void | finalize()Ensures that all resources for this stream are released when it is about
to be garbage collected.
close();
| public java.nio.channels.FileChannel | getChannel()Returns the {@link FileChannel} equivalent to this input stream.
The file channel is read-only and has an initial position within the file
that is the same as the current position of this stream within the file.
All changes made to the underlying file descriptor state via the channel
are visible by the input stream and vice versa.
// BEGIN android-changed
synchronized(this) {
if (channel == null) {
channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
IFileSystem.O_RDONLY);
}
return channel;
}
// END android-changed
| public final java.io.FileDescriptor | getFD()Returns the {@link FileDescriptor} representing the operating system
resource for this stream.
return fd;
| private synchronized void | openCheck()
if (fd.descriptor < 0) {
throw new IOException();
}
| public int | read(byte[] buffer)Reads bytes from this stream and stores them in the byte array
{@code buffer}.
return read(buffer, 0, buffer.length);
| public int | read(byte[] buffer, int offset, int count)Reads at most {@code count} bytes from this stream and stores them in the
byte array {@code buffer} starting at {@code offset}.
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// made implicit null check explicit,
// used (offset | count) < 0 instead of (offset < 0) || (count < 0)
// to safe one operation
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((count | offset) < 0 || count > buffer.length - offset) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
if (0 == count) {
return 0;
}
openCheck();
synchronized (repositioningLock) {
// stdin requires special handling
if (fd == FileDescriptor.in) {
return (int) fileSystem.ttyRead(buffer, offset, count);
}
return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
}
| public int | read()Reads a single byte from this stream and returns it as an integer in the
range from 0 to 255. Returns -1 if the end of this stream has been
reached.
byte[] readed = new byte[1];
int result = read(readed, 0, 1);
return result == -1 ? -1 : readed[0] & 0xff;
| public long | skip(long count)Skips {@code count} number of bytes in this stream. Subsequent
{@code read()}'s will not return these bytes unless {@code reset()} is
used. This method may perform multiple reads to read {@code count} bytes.
openCheck();
if (count == 0) {
return 0;
}
if (count < 0) {
// KA013=Number of bytes to skip cannot be negative
throw new IOException(Msg.getString("KA013")); //$NON-NLS-1$
}
// stdin requires special handling
if (fd == FileDescriptor.in) {
// Read and discard count bytes in 8k chunks
long skipped = 0, numRead;
int chunk = count < 8192 ? (int) count : 8192;
byte[] buffer = new byte[chunk];
for (long i = count / chunk; i >= 0; i--) {
numRead = fileSystem.ttyRead(buffer, 0, chunk);
skipped += numRead;
if (numRead < chunk) {
return skipped;
}
}
return skipped;
}
synchronized (repositioningLock) {
final long currentPosition = fileSystem.seek(fd.descriptor, 0L,
IFileSystem.SEEK_CUR);
final long newPosition = fileSystem.seek(fd.descriptor,
currentPosition + count, IFileSystem.SEEK_SET);
return newPosition - currentPosition;
}
|
|