Java IO 系列源码分析——InputStream和OutputStream
来源:互联网 发布:2017最新一手数据 编辑:程序博客网 时间:2024/05/18 00:02
InputStream
和OutputStream
是字节流的超类,它们分别提供了read()
和write()
方法来读取和写入字节。下面就InputStream
和OutputStream
源码进行简单分析。
1. InputStream
源码分析(基于jdk1.8.0_66
)
package java.io;public abstract class InputStream implements Closeable { //最大能skip的数目 private static final int MAX_SKIP_BUFFER_SIZE = 2048; //抽象方法,需要子类去实现 //从输入流中读取数据的下一个字节 public abstract int read() throws IOException; //将输入流中的数据读入byte数组 public int read(byte b[]) throws IOException { return read(b, 0, b.length); } //将输入流中最多len个字节读取到byte数组的off起始位置及后续位置 public int read(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int c = read(); if (c == -1) { return -1; } b[off] = (byte)c; int i = 1; try { for (; i < len ; i++) { c = read(); if (c == -1) { break; } b[off + i] = (byte)c; } } catch (IOException ee) { } return i; } //跳过输入流的n个字节 public long skip(long n) throws IOException { long remaining = n; int nr; if (n <= 0) { return 0; } int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining); byte[] skipBuffer = new byte[size]; while (remaining > 0) { nr = read(skipBuffer, 0, (int)Math.min(size, remaining)); if (nr < 0) { break; } remaining -= nr; } return n - remaining; } //获取输入流中还可以读取或者跳过的字节数(大致数目) //在超类InputStream中该方法永远返回0。 public int available() throws IOException { return 0; } public void close() throws IOException {} //给当前位置设置标志,当之后第一次使用reset方法时,将会从该标志位重新读取后续数据 //readlimit参数目前没有实际意义,可以设置为任意值 public synchronized void mark(int readlimit) {} public synchronized void reset() throws IOException { throw new IOException("mark/reset not supported"); } //标注方法是否支持mark方法,超类InputStream不支持。 public boolean markSupported() { return false; }}
OutputStream
源码分析(基于jdk1.8.0_66
)
package java.io;public abstract class OutputStream implements Closeable, Flushable { //将字节b写入到输出流中,该方法需要由子类实现。 public abstract void write(int b) throws IOException; //将byte数组中的字节数据写入到字节输出流中。 public void write(byte b[]) throws IOException { write(b, 0, b.length); } //将字节数组b中off起始位置开始的len长度的数据写入到字节输出流中 public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } for (int i = 0 ; i < len ; i++) { write(b[off + i]); } } //刷新字节输出流,将输出流中缓存的字节数据强制刷新输出 public void flush() throws IOException { } public void close() throws IOException { }}
InputStream
和OutputStream
作为字节流的超类,定义了读取和写入字节的read()
和write()
方法,从源代码中我们可以看到这两个方法需要子类根据自身需求进行实现(工厂方法)。在下面系列文章中我们将进一步分析具体子类的源码,观察各个子类是怎样实现和扩展read()
和wirte()
方法的。
0 0
- Java IO 系列源码分析——InputStream和OutputStream
- JDK 1.7 java.io 源码学习之InputStream和OutputStream
- Java.IO.InputStream-OutputStream
- Java IO - InputStream&OutputStream
- Java IO 字节流 InputStream和OutputStream
- java IO之OutputStream和InputStream
- JAVA IO-2 InputStream和OutputStream
- Java知识(IO流、.InputStream和OutputStream)
- Java IO流-InputStream和OutputStream
- 2 Java IO:InputStream 和 OutputStream
- IO流——(字节流)OutputStream和InputStream
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- java io系列03之 ByteArrayOutputStream的简介,源码分析和示例(包括OutputStream)
- java io系列03之 ByteArrayOutputStream的简介,源码分析和示例(包括OutputStream)
- java.io.InputStream.read和java.io.OutputStream.write
- Java IO系列0 InputStream与OutputStream(字节流)
- java InputStream 和 OutputStream
- 万圣节后的早晨
- 小波变换
- 【树状数组】树状数组の奇技淫巧专场
- Helpers方法
- noi.openjudge 1711 潜伏者答案及其分析
- Java IO 系列源码分析——InputStream和OutputStream
- Java面试系列之HashMap大扫盲汇总
- 【LeetCode】17. Letter Combinations of a Phone Number
- 分页内存和非分页内存区别
- 解决Linux 启动 Nginx报错 :nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or direc
- storm分布式安装
- SSH用通用dao更新出现的问题
- QT--------QLineEdit
- 【程序9】输出国际象棋棋盘