Java字节流:InputStream OutputStream

时间:2023-03-09 04:12:18
Java字节流:InputStream OutputStream

字节输入流:InputStream

类声明:

public abstract class InputStream implements Closeable

位于java.io包下,是一个抽象类.

官方对其说明:
  This abstract class is the superclass of all classes representing an input stream of bytes.
  (简单翻译:抽象类InputStream是所有字节输入流的父类)

主要方法:
  - int available();返回输入流中可读取的字符个数.
  - void close(): 关闭此输入流并释放与该流有关的系统资源.
  - void mark(int readlimit): 在此输入流中标记当前的位置.
  - boolean markSupported(): 检测此输入流是否支持mark和reset.
  - abstract int read(): 从输入流中读取数据的下一个字节.
  - int read(byte[] b): 从输入流中读取一定数量的字节,并将其存储在字节数组b中
  - int read(byte[] b,int off,int len): 从输入流中读取len个字节,并将其存储在字节数组b中off位置开始的地方
  - void reset(): 将此流重新定位到最后一次对此输入流调用mark方法时的位置.
  - long skip(long n): 跳过和丢弃此输入流中n个字节的数据.

  InputStream 是所有字节输入流的父类,其中有一个抽象方法read() 是字节输入流的核心,所有子类都必须实现此方法。
InputStream源代码分析:

 package java.io;

 /**
* 该抽象类是所有字节输入流的父类
*/
public abstract class InputStream implements Closeable { private static final int MAX_SKIP_BUFFER_SIZE = 2048;//最大允许跳过的字节个数 /*
功能: 从输入流中读取下一个字节数据,字节数据的值以0-255之间的int类型返回,如果已经到达流末尾,则返回值-1.
子类必须提供该方法的一个实现.
返回值:下一个字节数据;如果到达流的末尾则返回-1
*/
public abstract int read() throws IOException; public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
} /*
功能:从输入流中读取len个字节数据,并把这些字节数据写到字节数组b中(从off位置开始写)
参数:
byte b[]: 存储从输入流中读取的字节数据
int off: 开始位置
int len: 读取的字节个数
返回值: 实际从输入流中读取到字节数组b中的字节个数
*/
public int read(byte b[], int off, int len) throws IOException { //检查字节数组b是否为null,检查off len 等参数是否数组越界
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; //循环从输入流中读取len个字节数据到字节数组b中(如果循环读取过程中到达了输入流的末尾,则跳出循环不再读取)
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;
} /*
功能: 返回输入流中还可被读取的字节个数
*/
public int available() throws IOException {
return 0;
} /*
功能: 关闭输入流
*/
public void close() throws IOException {} /*
功能: 在输入流中标记一个位置
*/
public synchronized void mark(int readlimit) {} /*
功能: 将此流重新定位到最后一次对此输入流调用 mark 方法时的位置.
*/
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
} /*
功能: 检查该输入流是否支持mark()和reset()方法
*/
public boolean markSupported() {
return false;
}
}

字节输入流:OutputStream 

类声明:

public abstract class OutputStream implements Closeable

位于java.io包下,是一个抽象类.
官方对其说明:
  This abstract class is the superclass of all classes representing an output stream of bytes.
  (简单翻译:抽象类OutputStream是所有字节输出流的父类)

主要方法:
  - void close(): 关闭此输出流并释放与该流有关的系统资源.
  - void flush(): 刷新此输出流并强制写出所有缓冲的输出字节.
  - void write(byte[] b): 将b.length个字节从指定的byte数组写入此输出流.
  - void write(byte[] b,int off,int len): 将byte数组中从off位置开始的len个字节写入此输出流.
  - abstract void write(int b): 将指定的字节写入此输出流.

  OutputStream 是所有字节输出流的父类,其中有一个抽象方法write(int b) 是字节输出流的核心,所有子类都必须实现此方法。

OutputStream源代码分析:

 package java.io;

 /*
该抽象类是所有字节输出流的父类。输出流接收输出字节并将这些字节发送到某个接收器。
*/
public abstract class OutputStream implements Closeable, Flushable { /*
功能: 将指定的数据b写入到输出流中.
*/
public abstract void write(int b) throws IOException; /*
功能:将字节数组b中的全部数据写入到此输出流中
*/
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
} /*
功能: 将指定 byte 数组中从偏移量 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 {
}
}