黑马程序员技术交流社区
标题:
关于BufferedOutputStream
[打印本页]
作者:
何竹冬
时间:
2013-1-5 15:46
标题:
关于BufferedOutputStream
本帖最后由 何竹冬 于 2013-1-6 13:42 编辑
在查看字节缓冲输出流的源码时候发现这段代码看不懂,谁能解释一下啊
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this buffered output stream.
*
* <p> Ordinarily this method stores bytes from the given array into this
* stream's buffer, flushing the buffer to the underlying output stream as
* needed. If the requested length is at least as large as this stream's
* buffer, however, then this method will flush the buffer and write the
* bytes directly to the underlying output stream. Thus redundant
* <code>BufferedOutputStream</code>s will not copy data unnecessarily.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
*/
public synchronized void write(byte b[], int off, int len) throws IOException {
if (len >= buf.length) {
/* If the request length exceeds the size of the output buffer,
flush the output buffer and then write the data directly.
In this way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(b, off, len);
return;
}
if (len > buf.length - count) {
flushBuffer();
}
System.arraycopy(b, off, buf, count, len);
count += len;
}
复制代码
首先在写字节数组的时候又调用了写字节数组的方法,这样不会无限递归吗
下面第二个if我怎么感觉不会执行到呢count默认值是0啊,如果第一个if不满足第二个if应该也不会满足条件的啊。
还有下面的几行麻烦代价解释一下
作者:
郑传庆
时间:
2013-1-5 15:59
第一个if意思是说:当传入的len大于等于buf.length时,就将数据刷新并且写到相应的缓存中去。第二个if是说:当len大于buf.length - count时就向缓存刷新一次。count从后面的代码看,应该是用来计算数据的长度的。
作者:
王少雷
时间:
2013-1-5 17:12
代码看着太费劲,,,,
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2