`
默默的小熊
  • 浏览: 227528 次
社区版块
存档分类
最新评论

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);
			}
		}

	}
}

分享到:
评论

相关推荐

    smartupload实现上传源码

    smartupload实现上传源码 WEB +jsp+JavaBean+Smart+upload+Servlet+Tomcat

    有关java上传和File以及FileInputStream的区别

    NULL 博文链接:https://coffeesweet.iteye.com/blog/812263

    Java读取资源文件工具类--ReadConfigation(java源码)

    import java.io.FileInputStream; import java.util.Properties; public class ReadConfigation{ /** * 属性文件全名 */ private static final String PFILE ="Config.properties"; /** * 对应于...

    桌面管家源码(java)

    public FileInputStream fis=null;//文件输出流 byte[] buf; public int patch,patch1; link(){} //创立连接 public void Screateconnect() { try { serversocket=new ServerSocket(9999);//...

    Struts Updownload 源码

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; ...

    项目源码-java学生管理系统

    【项目源码】-java学生管理系统 package appstu; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util....

    java基础案例与开发详解案例源码全

    13.4.1 FileInputStream和FileOutputStream336 13.4.2 FileReader和FileWriter338 13.5 缓冲流340 13.6 转换流341 13.7 数据流342 13.8 打印流344 13.9 对象流345 13.9.1 序列化和反序列化操作345 13.9.2 序列化的...

    TCP并发上传——java源码

    InputStream in = new FileInputStream("D:\\Client"); byte[] buf = new byte[1024*1024]; int len; ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); while (true) { len = in.read(buf); if ...

    java7源码-JavaIO:JavaIO

    源码 JavaIO ##JavaIO基础面试题 1. 什么是IO 它是一种数据的流从源头到目的地。比如文件拷贝,输入流和输出流都包括了。输入流是从文件中读取数据到存储到进程(process)中,输出流是从进程中读取数据然后写入到目标...

    Java文件读写操作函数实现

    * 一、BufferedReader类 public class BufferedReader extends Reader * 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下...

    Android 网络html源码查看器详解及实例

    FileInputStream,FIleOutputStream,BufferInputStream,BufferOutStream的读写使用与区别 //进行流的读写 byte[] buffer = new byte[1024 * 8]; //创建一个写到内存的字节数组输出流 ByteArrayOutputStream ...

    Java从文件中读取数字证书.rar

    Java生成 数字证书源码,从文件中读取数字证书,生成文件输入流,输入文件为c:/mycert.cer,获取一个处理X.509证书的证书工厂,获取证书,请查看以下相关代码:  //生成文件输入流,输入文件为c:/mycert.cer  ...

    java压缩文件源码--ZipUtils

    FileInputStream fis = new FileInputStream(file); CheckedInputStream checksum = new CheckedInputStream(fis,new Adler32()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream...

    java_对excel_的操作源码(有备无患)

    1 从Excel文件读取数据表 ... InputStream is = new FileInputStream(sourcefile); jxl.Workbook rwb = Workbook.getWorkbook(is); } catch (Exception e) { e.printStackTrace(); }

    SmartFoxServer解密源码

    FileInputStream in = readFile(licenceFile); //KEY的长度 int keyLen = in.read(); //key key = new byte[keyLen]; in.read(key, 0, keyLen); //加密的配置文件 byte[] config = new byte[in.available()];...

    word源码java-replace-maven-plugin:替换项目代码不规范部分,不改变源码,实现修改jar中的class文件

    word源码java replace-maven-plugin 1. 背景 背景:swift引擎fr版本和开源版本维护两套代码(就包名不同)太麻烦,维护成本高 要求:代码启动时用原包名jar,但是打包fr版本时,要替换成com.fr.third的包名 预期:...

    FTP源代码例子

    import java.io.FileInputStream; import java.io.IOException; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; public class MainCtrl extends HttpServlet { private FtpClient ftpClient; ...

    去除网页复制的行号

    5.import java.io.FileInputStream; 6.import javax.imageio.ImageIO; , 而且往往几百行,手动花上几分钟去删除很是麻烦,而且最后代码是不是我们想要的也很难说 ,由于经常碰上这种事,干脆花个时间去写出来,...

    使用Java读取文件的N种方法

    Reading a file is a task which every developer is required to perform. Java provides many different ways to read a ...  Method 1 : Using java.io.FileInputStream static void readUsingStream(String fil

Global site tag (gtag.js) - Google Analytics