Java IO字节输入流常见类进行分析(二)
2017-02-21 23:17
429 查看
一、FileInputStream
从文件系统中读取一个文件转换成的字节
数据结构:
重载的构造方法
FileDescriptor是FileInputStream与文件的连接桥梁,它的创建代表着文件的打开或者socket的打开。
部分方法:
close方法
getChannel() 增加FileDescriptor的数量
二、SocketInputStream
继承与FileInputStream,注意的是该类没有被public修饰。看一下类的结构
从这里可以看出,File IO和Socket IO非常的相似。
三、ObjectInputStream
一个反序列化之前写入到ObjectOutputSteam中的原生类型数据和对象类型。ObjectInputStream确保了在图中流所匹配在JVM中的java类。
从文件系统中读取一个文件转换成的字节
数据结构:
public class FileInputStream extends InputStream { /* File Descriptor - handle to the open file */ private final FileDescriptor fd; /* The path of the referenced file (null if the stream is created with a file descriptor) */ private final String path;//文件的路径 private FileChannel channel = null;//与文件相连的通道 private final Object closeLock = new Object(); private volatile boolean closed = false; private static final ThreadLocal<Boolean> runningFinalize = new ThreadLocal<>(); //... }
重载的构造方法
//通过文件来创建 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);//打开文件 } //通过文件描述器来创建文件输入流 public FileInputStream(FileDescriptor fdObj) { SecurityManager security = System.getSecurityManager(); if (fdObj == null) { throw new NullPointerException(); } if (security != null) { security.checkRead(fdObj); } fd = fdObj; path = null; /* * FileDescriptor is being shared by streams. * Ensure that it's GC'ed only when all the streams/channels are done * using it. */ fd.incrementAndGetUseCount(); } //通过文件的路径名来创建输入流 public FileInputStream(String name) throws FileNotFoundException { this(name != null ? new File(name) : null); }
FileDescriptor是FileInputStream与文件的连接桥梁,它的创建代表着文件的打开或者socket的打开。
部分方法:
close方法
public void close() throws IOException { synchronized (closeLock) {//同步锁 if (closed) { return; } closed = true; } if (channel != null) { //减少File Descriptor的数量 fd.decrementAndGetUseCount(); channel.close(); } /* *减少File Descriptor的数量 */ int useCount = fd.decrementAndGetUseCount(); /* * 如果File Descriptor的被其他流使用,那就将不会关闭 */ if ((useCount <= 0) || !isRunningFinalize()) { close0(); } }
getChannel() 增加FileDescriptor的数量
public FileChannel getChannel(){ synchronized (this) { if (channel == null) { channel = FileChannelImpl.open(fd, path, true, false, this); //增加FileDescriptor fd.incrementAndGetUseCount(); } return channel; } }
二、SocketInputStream
继承与FileInputStream,注意的是该类没有被public修饰。看一下类的结构
class SocketInputStream extends FileInputStream { private boolean eof;//结束标志 private AbstractPlainSocketImpl impl = null;//socket现实抽象类 private byte temp[];//缓冲数据 private Socket socket = null; }
从这里可以看出,File IO和Socket IO非常的相似。
三、ObjectInputStream
一个反序列化之前写入到ObjectOutputSteam中的原生类型数据和对象类型。ObjectInputStream确保了在图中流所匹配在JVM中的java类。
相关文章推荐
- Java IO字节输入流常见类进行分析(一)
- Java IO字节输出流常见类进行分析(三)
- Java IO字符流常见类进行分析(四)
- IO_缓冲流_转换流_字节转为字符_乱码分析_编码与解码JAVA152-154
- JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流
- IO_缓冲流_转换流_字节转为字符_乱码分析_编码与解码JAVA152-154
- JAVA IO分析一:File类、字节流、字符流、字节字符转换流
- Java io功能总结分析(一):字节流读写
- Java中 IO中的字节输入流InputStream,字节输出流InputStream
- java中使用文件输入流和文件输出流进行文件读写的用例!
- java.io实现分析
- JAVA视频教程,学习JAVA常见问题分析-JAVA学习方法系列
- 使用 java.io 进行服务器-Android通信
- java IO操作与字节流(三)文件输入流与输出流
- 错误:未报告的异常 java.io.IOException;必须对其进行捕捉或声明以便抛出
- java io 分析
- 字节输入输出流:java.io.FileInputStream和java.io.FileOutputStream
- 在java bean中对一个java文件进行语法分析 的方法
- JAVA进行类型转换与字节数组与JAVA类型转换
- java struts常见错误以及原因分析