FileInputStream类源码


import java.io.FileDescriptor;
import java.io.InputStream;
import java.nio.channels.FileChannel;

public class FileInputStream extends InputStream {
	private FileDescriptor fd;

	private FileChannel channel = null;

	private Object closeLock = new Object();
	
	private volatile boolean closed = false;

	private static final ThreadLocal<Boolean> runningFinalize = new ThreadLocal<Boolean>();

	private static boolean isRunningFinalize() {
		Boolean val;
		if ((val = runningFinalize.get()) != null)
			return val.booleanValue();
		return false;
	}

	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();
		}
		fd = new FileDescriptor();
		fd.incrementAndGetUseCount();
		open(name);
	}

	public FileInputStream(String name) throws FileNotFoundException {
		this(name != null ? new File(name) : null);
	}

	public FileInputStream(FileDescriptor fdObj) {
		SecurityManager security = System.getSecurityManager();
		if (fdObj == null) {
			throw new NullPointerException();
		}
		if (security != null) {
			security.checkRead(fdObj);
		}
		fd = fdObj;
		fd.incrementAndGetUseCount();
	}

	// 打开文件
	private native void open(String name) throws FileNotFoundException;

	// 读取一个字节,如果没有读到数据方法将会阻塞,
	// 如果数据读完,返回-1
	public native int read() throws IOException;

	// 读取数据存到b[]中
	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);
	}


	public native long skip(long n) throws IOException;


	public native int available() throws IOException;

	
	public void close() throws IOException {
		synchronized (closeLock) {
			if (closed) {
				return;
			}
			closed = true;
		}
		if (channel != null) {
			fd.decrementAndGetUseCount();
			channel.close();
		}

		int useCount = fd.decrementAndGetUseCount();

		if ((useCount <= 0) || !isRunningFinalize()) {
			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, true, false, this);
				fd.incrementAndGetUseCount();

			}
			return channel;
		}
	}

	//本地方法
	private static native void initIDs();

	//本地方法
	private native void close0() throws IOException;

	//静态初始化块
	static {
		initIDs();
	}

	protected void finalize() throws IOException {
		if ((fd != null) && (fd != FileDescriptor.in)) {
			runningFinalize.set(Boolean.TRUE);
			try {
				close();
			} finally {
				runningFinalize.set(Boolean.FALSE);
			}
		}

	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值