| Constructor: |
public FileInputStream(File file) throws FileNotFoundException {
super();
SecurityManager security = System.getSecurityManager();
if (security != null) {
// For compatibility, nulls are passed to the manager.
String filePath = (null == file ? null : file.getPath());
security.checkRead(filePath);
}
if (file == null) {
// luni.4D=Argument must not be null
throw new NullPointerException(Messages.getString("luni.4D")); //$NON-NLS-1$
}
fd = new FileDescriptor();
fd.readOnly = true;
fd.descriptor = fileSystem.open(file.properPath(true),
IFileSystem.O_RDONLY);
innerFD = true;
channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
IFileSystem.O_RDONLY);
}
Constructs a new {@code FileInputStream} based on {@code file}. Parameters:
file -
the file from which this stream reads.
Throws:
FileNotFoundException -
if {@code file} does not exist.
SecurityException -
if a {@code SecurityManager} is installed and it denies the
read request.
|
public FileInputStream(FileDescriptor fd) {
super();
if (fd == null) {
throw new NullPointerException();
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(fd);
}
this.fd = fd;
innerFD = false;
channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
IFileSystem.O_RDONLY);
}
Constructs a new {@code FileInputStream} on the FileDescriptor
{@code fd}. The file must already be open, therefore no
{@code FileNotFoundException} will be thrown. Parameters:
fd -
the FileDescriptor from which this stream reads.
Throws:
NullPointerException -
if {@code fd} is {@code null}.
SecurityException -
if a {@code SecurityManager} is installed and it denies the
read request.
|
public FileInputStream(String fileName) throws FileNotFoundException {
this(null == fileName ? (File) null : new File(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"}. Parameters:
fileName -
the path and name of the file from which this stream reads.
Throws:
FileNotFoundException -
if there is no file named {@code fileName}.
SecurityException -
if a {@code SecurityManager} is installed and it denies the
read request.
|
| Method from java.io.FileInputStream Detail: |
public int available() throws IOException {
openCheck();
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);
}
}
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. |
public void close() throws IOException {
if (fd == null) {
// if fd is null, then the underlying file is not opened, so nothing
// to close
return;
}
if (channel != null) {
synchronized (channel) {
if (channel.isOpen()) {
channel.close();
}
}
}
synchronized (this) {
if (fd.descriptor >= 0 && innerFD) {
fileSystem.close(fd.descriptor);
fd.descriptor = -1;
}
}
}
|
protected void finalize() throws IOException {
close();
}
Ensures that all resources for this stream are released when it is about
to be garbage collected. |
public FileChannel getChannel() {
return channel;
}
Returns the 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. |
public final FileDescriptor getFD() throws IOException {
return fd;
}
Returns the FileDescriptor representing the operating system
resource for this stream. |
public int read() throws IOException {
byte[] readed = new byte[1];
int result = read(readed, 0, 1);
return result == -1 ? -1 : readed[0] & 0xff;
}
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. |
public int read(byte[] buffer) throws IOException {
return read(buffer, 0, buffer.length);
}
Reads bytes from this stream and stores them in the byte array
{@code buffer}. |
public int read(byte[] buffer,
int offset,
int count) throws IOException {
if (count > buffer.length - offset || count < 0 || offset < 0) {
throw new IndexOutOfBoundsException();
}
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);
}
}
Reads at most {@code count} bytes from this stream and stores them in the
byte array {@code buffer} starting at {@code offset}. |
public long skip(long count) throws IOException {
openCheck();
if (count == 0) {
return 0;
}
if (count < 0) {
// luni.AC=Number of bytes to skip cannot be negative
throw new IOException(Messages.getString("luni.AC")); //$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;
}
}
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. |