一、FileInputStream函数
FileInputStream函数主要实现将程序的内容通过这个接口将数据读入到程序中。下面我将从源码角度进行对FileInputStream的内部方法进行介绍。
二、内部源码介绍
首先FileInputStream函数继承于InputStream函数
public
class FileInputStream extends InputStream
{
}
下面定义了文件修饰符
private final FileDescriptor fd;
下面定义了文件路径path
private final String path;
下面定义了文件通道
private FileChannel channel = null;
定义了文件关闭锁和一个转瞬即逝变量关闭标志符
private final Object closeLock = new Object();
private volatile boolean closed = false;
通过打开与实际文件的连接来创建 FileInputStream,该文件由文件系统中的路径名命名
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
下面方法为FileInputStream实现方法
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.attach(this);
path = name;
open(name);
}
下面方法实现利用文件修饰符进行文件输出流函数的连接
public FileInputStream(FileDescriptor fdObj) {
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkRead(fdObj);
}
fd = fdObj;
path = null;
/*
* FileDescriptor is being shared by streams.
* Register this stream with FileDescriptor tracker.
*/
fd.attach(this);
}
打开文件读取,默认调用JVM
private native void open0(String name) throws FileNotFoundException;
实现open方法
private void open(String name) throws FileNotFoundException {
open0(name);
}
调用JVM的读方法
private native int read0() throws IOException;
实现读方法
public int read() throws IOException {
return read0();
}
调用JVM的读取字的方法
private native int readBytes(byte b[], int off, int len) throws IOException;
实现读取字符数组的方法
public int read(byte b[]) throws IOException {
return readBytes(b, 0, b.length);
}
实现读取某一区间位置的方法
public int read(byte b[], int off, int len) throws IOException {
return readBytes(b, off, len);
}
调用JVM实现跳过的方法
private native long skip0(long n) throws IOException;
实现跳过方法
public long skip(long n) throws IOException {
return skip0(n);
}
调用JVM实现剩余方法
private native int available0() throws IOException;
实现可用的方法
public int available() throws IOException {
return available0();
}
调用JVM的关闭方法
private native void close0() throws IOException;
定义文件关闭方法
public void close() throws IOException {
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
}
});
}
定义获取文件标志符方法
public final FileDescriptor getFD() throws IOException {
if (fd != null) {
return fd;
}
throw new IOException();
}
定义获取管道方法
public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, path, true, false, this);
}
return channel;
}
}
调用JVM的init方法
private static native void initIDs();
静态代码块
static {
initIDs();
}
确保在没有更多引用时调用此文件输入流的 close 方法。
protected void finalize() throws IOException {
if ((fd != null) && (fd != FileDescriptor.in)) {
close();
}
}