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实现上传源码 WEB +jsp+JavaBean+Smart+upload+Servlet+Tomcat
NULL 博文链接:https://coffeesweet.iteye.com/blog/812263
import java.io.FileInputStream; import java.util.Properties; public class ReadConfigation{ /** * 属性文件全名 */ private static final String PFILE ="Config.properties"; /** * 对应于...
public FileInputStream fis=null;//文件输出流 byte[] buf; public int patch,patch1; link(){} //创立连接 public void Screateconnect() { try { serversocket=new ServerSocket(9999);//...
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学生管理系统 package appstu; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util....
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 序列化的...
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 ...
源码 JavaIO ##JavaIO基础面试题 1. 什么是IO 它是一种数据的流从源头到目的地。比如文件拷贝,输入流和输出流都包括了。输入流是从文件中读取数据到存储到进程(process)中,输出流是从进程中读取数据然后写入到目标...
* 一、BufferedReader类 public class BufferedReader extends Reader * 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下...
FileInputStream,FIleOutputStream,BufferInputStream,BufferOutStream的读写使用与区别 //进行流的读写 byte[] buffer = new byte[1024 * 8]; //创建一个写到内存的字节数组输出流 ByteArrayOutputStream ...
Java生成 数字证书源码,从文件中读取数字证书,生成文件输入流,输入文件为c:/mycert.cer,获取一个处理X.509证书的证书工厂,获取证书,请查看以下相关代码: //生成文件输入流,输入文件为c:/mycert.cer ...
FileInputStream fis = new FileInputStream(file); CheckedInputStream checksum = new CheckedInputStream(fis,new Adler32()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream...
1 从Excel文件读取数据表 ... InputStream is = new FileInputStream(sourcefile); jxl.Workbook rwb = Workbook.getWorkbook(is); } catch (Exception e) { e.printStackTrace(); }
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 1. 背景 背景:swift引擎fr版本和开源版本维护两套代码(就包名不同)太麻烦,维护成本高 要求:代码启动时用原包名jar,但是打包fr版本时,要替换成com.fr.third的包名 预期:...
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; , 而且往往几百行,手动花上几分钟去删除很是麻烦,而且最后代码是不是我们想要的也很难说 ,由于经常碰上这种事,干脆花个时间去写出来,...
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