首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

FileInputStream种源码

2012-10-28 
FileInputStream类源码import java.io.FileDescriptorimport java.io.InputStreamimport java.nio.chann

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;// 读取一个字节,如果没有读到数据方法将会阻塞,// 如果数据读完,返回-1public 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);}}}}

热点排行