public int read(byte[] b, int off, int len)
read中off参数 在API中是这么解释的,目标数组 b 中的起始偏移量。
这个是啥意思啊
顾名思义啊;
通俗来讲吧
首先有一个地方是起点,然后距离这个起点有多远,这个距离就是偏移量
最小的距离是一个字节
你要len个长度的数据装袋b但是你拿到一串数据你得告诉他从哪儿开始截不是,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();
以上是InputStream 类中的源代码, 要求不能 len > b.length - off 。
可知
b.length :为流一次往byte数组读取的 最大字节数,
off 偏移量为 :将读取的第一个字节存储在元素 b[off] 中,
len : 为一次读的字节数。
所以没有设置偏移量, len 一次读取的字节数可以 小于等于 b.length 。 设置了偏移量自然为 b.length > off + len