java缓冲流和普通流的区别_java.io.Buffer * stream与普通流不同?

从BufferedInputStream

javadoc:

A BufferedInputStream adds functionality to another input stream-namely,the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created,an internal buffer array is created. As bytes from the stream are read or skipped,the internal buffer is refilled as necessary from the contained input stream,many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

在内部使用缓冲区数组,而不是从底层输入流单独读取字节,读取足够的字节来填充缓冲区.这通常导致更快的性能,因为底层输入流需要较少的读取.

对于BufferedOutputStream则是相反的.

mark()和reset()可以如下使用:

1 BufferedInputStream bis = new BufferedInputStream(is);

2 byte[] b = new byte[4];

3 bis.read(b); // read 4 bytes into b

4 bis.mark(10); // mark the stream at the current position - we can read 10 bytes before the mark point becomes invalid

5 bis.read(b); // read another 4 bytes into b

6 bis.reset(); // resets the position in the stream back to when mark was called

7 bis.read(b); // re-read the same 4 bytes as line 5 into b

解释标记/重置一些更多…

BufferInputStream内部记住缓冲区中的当前位置.当您读取字节时,位置将递增.标记(10)的电话将保存当前位置.随后的读取调用将继续增加当前位置,但是当调用mark时,调用reset将将当前位置设置为其值.

标记的参数指定在标记位置无效之前调用标记后可以读取的字节数.一旦标记位置无效,您就不能再调用重置来返回.

例如,如果在第4行中使用了mark(2),则当在第6行调用reset()时,将会抛出IOException,因为标记位置将被无效,因为我们读取超过2个字节.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值