java filechannel inputstream_Java IO 之 FileInputStream & FileOutputStream源码分析

Writer :李强强一、引子文件,作为常见的数据源。关于操作文件的字节流就是 — FileInputStream & FileOutputStream。它们是Basic IO字节流中重要的实现类。二、FileInputStream源码分析FileInputStream源码如下:/*** FileInputStream 从文件系统的文件中获取输入字节流。文件取决于主机系统。* 比...
摘要由CSDN通过智能技术生成

Writer      :李强强

一、引子

文件,作为常见的数据源。关于操作文件的字节流就是 — FileInputStream & FileOutputStream。它们是Basic IO字节流中重要的实现类。

二、FileInputStream源码分析

FileInputStream源码如下:

/**

* FileInputStream 从文件系统的文件中获取输入字节流。文件取决于主机系统。

* 比如读取图片等的原始字节流。如果读取字符流,考虑使用 FiLeReader。

*/

public class SFileInputStream extends InputStream

{

/* 文件描述符类---此处用于打开文件的句柄 */

private final FileDescriptor fd;

/* 引用文件的路径 */

private final String path;

/* 文件通道,NIO部分 */

private FileChannel channel = null;

private final Object closeLock = new Object();

private volatile boolean closed = false;

private static final ThreadLocal runningFinalize =

new ThreadLocal<>();

private static boolean isRunningFinalize() {

Boolean val;

if ((val = runningFinalize.get()) != null)

return val.booleanValue();

return 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.incrementAndGetUseCount();

this.path = name;

open(name);

}

/* 通过文件描述符类来创建FileInputStream */

public FileInputStream(FileDescriptor fdObj) {

SecurityManager security = System.getSecurityManager();

if (fdObj == null) {

throw new NullPointerException();

}

if (security != null) {

security.checkRead(fdObj);

}

fd = fdObj;

path = null;

fd.incrementAndGetUseCount();

}

/* 打开文件,为了下一步读取文件内容。native方法 */

private native void open(String name) throws FileNotFoundException;

/* 从此输入流中读取一个数据字节 */

public int read() throws IOException {

Object traceContext = IoTrace.fileReadBegin(path);

int b = 0;

try {

b = read0();

} finally {

IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);

}

return b;

}

/* 从此输入流中读取一个数据字节。native方法 */

private native int read0() throws IOException;

/* 从此输入流中读取多个字节到byte数组中。native方法 */

private native int readBytes(byte b[], int off, int len) throws IOException;

/* 从此输入流中读取多个字节到byte数组中。 */

public int read(byte b[]) throws IOException {

Object traceContext = IoTrace.fileReadBegin(path);

int bytesRead = 0;

try {

bytesRead = readBytes(b, 0, b.length);

} finally {

IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);

}

return bytesRead;

}

/* 从此输入流中读取最多len个字节到byte数组中。 */

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值