超详细的逐句介绍Java高级接口之文件输入流函数FileInputStream函数源码讲解(全)

一、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();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝域时空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值