目录
write(byte[] b, int off, int len)
深入解析 Java IO 源码:OutputStream
在 Java IO 中,OutputStream
是处理字节输出的抽象类。它是所有字节输出流类的超类,提供了一些基本的方法来写入字节数据。本文将深入解析 OutputStream
的源码,帮助您更好地理解其设计和实现。
什么是 OutputStream?
OutputStream
是一个抽象类,位于 java.io
包中,用于表示字节输出流。它是所有字节输出流类的超类,提供了一些基本的方法来写入字节数据。以下是 OutputStream
类的定义:
public abstract class OutputStream implements Closeable, Flushable {
// 抽象方法,写入一个字节的数据
public abstract void write(int b) throws IOException;
// 写入字节数组的数据
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
// 从指定偏移量开始写入字节数组的数据
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 {
}
}