A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 何竹冬 中级黑马   /  2013-1-5 15:46  /  1306 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 何竹冬 于 2013-1-6 13:42 编辑

在查看字节缓冲输出流的源码时候发现这段代码看不懂,谁能解释一下啊
  1. /**
  2. * Writes <code>len</code> bytes from the specified byte array
  3. * starting at offset <code>off</code> to this buffered output stream.
  4. *
  5. * <p> Ordinarily this method stores bytes from the given array into this
  6. * stream's buffer, flushing the buffer to the underlying output stream as
  7. * needed. If the requested length is at least as large as this stream's
  8. * buffer, however, then this method will flush the buffer and write the
  9. * bytes directly to the underlying output stream. Thus redundant
  10. * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
  11. *
  12. * @param b the data.
  13. * @param off the start offset in the data.
  14. * @param len the number of bytes to write.
  15. * @exception IOException if an I/O error occurs.
  16. */
  17. public synchronized void write(byte b[], int off, int len) throws IOException {
  18. if (len >= buf.length) {
  19. /* If the request length exceeds the size of the output buffer,
  20. flush the output buffer and then write the data directly.
  21. In this way buffered streams will cascade harmlessly. */
  22. flushBuffer();
  23. out.write(b, off, len);
  24. return;
  25. }
  26. if (len > buf.length - count) {
  27. flushBuffer();
  28. }
  29. System.arraycopy(b, off, buf, count, len);
  30. count += len;
  31. }
复制代码
首先在写字节数组的时候又调用了写字节数组的方法,这样不会无限递归吗
下面第二个if我怎么感觉不会执行到呢count默认值是0啊,如果第一个if不满足第二个if应该也不会满足条件的啊。
还有下面的几行麻烦代价解释一下

评分

参与人数 1技术分 +1 收起 理由
高境 + 1 很给力!

查看全部评分

2 个回复

正序浏览
代码看着太费劲,,,,
回复 使用道具 举报
第一个if意思是说:当传入的len大于等于buf.length时,就将数据刷新并且写到相应的缓存中去。第二个if是说:当len大于buf.length - count时就向缓存刷新一次。count从后面的代码看,应该是用来计算数据的长度的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马