“BufferedInputStream” vs “FileInputStream with configured size”

huangapple go评论85阅读模式
英文:

"BufferedInputStream" vs "FileInputStream with configured size"

问题

Case 1 => 创建了BufferedInputStream,我想每次读取12个字节,但为了提高性能,它会缓冲多达1024字节(例如)。因此,它会在单个sys调用中使用BufferedInputStream构造函数中提供的FileInputStream读取1024字节。

Case 2 => 在FileInputStream.read(byte[])中,我可以传递一个大小为1024的字节数组。这意味着在每次sys调用中,我的FileInputStream读取1024字节。

从Case 1和Case 2来看,我无法看到BufferedInputStream在运行时性能方面提供的任何改进。它只是将FileInputStream包装起来,并执行与Case 2中所做的相同操作。

有人能帮我更好地理解吗?

英文:

Case 1 => BufferedInputStream is created and I would like to read() 12 bytes at a time, but it buffers upto 1024 bytes (say for instance) for performance improvement. So, it reads 1024 bytes in a single sys call USING the passed FileInputStream provided in BufferedInputStream constructor.

Case 2 => In FileInputStream.read(byte[]), I can pass a byte array of size 1024. Which means in each sys call, my FileInputStream reads 1024 bytes.

From Case 1 and Case 2, I cannot see any betterment BufferedInputStream provides in terms of run time performance. All it does is that wraps FileInputStream around and does the same what I did in Case 2.

can anyone help me understand better?

答案1

得分: 1

是的,您可以通过从底层的InputStream中读取固定的块来有效地重新实现BufferedInputStream的功能,但这并不太容易:

  • 首先,read(byte[])调用不能保证填充数组,您必须检查返回值。BufferedInputStream会为您处理这一点。
  • 如果您手动管理缓冲区,那么在多个缓冲区实例之间跨越一些数据结构可能会很麻烦,BufferedInputStream提供了众所周知的InputStream接口,而无需您的代码关心缓冲。
  • 由于上述原因,BufferedInputStream可以传递给接受InputStream的任何东西。

最终,BufferedInputStream只是纯粹的Java代码,因此您可以手动执行它所做的任何操作,但几乎没有理由这样做。

简而言之BufferedInputStream只是一种通过减少系统调用和缓冲读取来提高性能的简单方法,同时仍然提供正常的InputStream接口。您可以在没有它的情况下实现它提供的所有功能,但几乎没有理由这样做。

英文:

Yes, you can effectively re-implement what BufferedInputStream does by reading fixed blocks from the underlying InputStream, but it's not quite as easy:

  • first, a read(byte[]) call is not guaranteed to fill the array, you have to check the return value. A BufferedInputStream handles this for you.
  • If you manually manage your buffer then having some data structures cross multiple buffere instances can be a pain to implement, BufferedInputStream provides the well-known InputStream interface without your code having to care about the buffereing.
  • Due to that last point BufferedInputStream can be passed to anything that accepts an InputStream.

In the end BufferedInputStream is just pure Java code, so you can do anything it does manually as well, but there are very few reasons to do so.

tl;dr BufferedInputStream is just a simple way to gain some performance by reducing system calls and buffering reads while still providing the normal InputStream interface. You could implement everything it provides without it, but there is almost never a reason to.

答案2

得分: 0

也许在你学习装饰器设计模式之后,你可以更好地理解BufferedInputStream。

BufferedInputStream不仅仅缓冲你的数据,更重要的是它实现了InputStream接口。你可以像对待其他任何InputStream一样对待它,并将它与其他InputStream结合起来,创建一个更强大的InputStream。

你可以按照自己的需求实现自己的BufferedInputStream,但我认为这并不像你想象的那么简单。

英文:

Maybe after you learn about decorator design pattern, you can understand better about BufferedInputstream.

BufferedInputStream not just buffer your data, more important is that it implements the InputStream interface. You can treat it like anyother InputStream and combine it with other InputStream to create a more powerful InputStream.

You can implement your own BufferedInputStream as you want, but I think it's not as easy as you think.

huangapple
  • 本文由 发表于 2020年8月11日 15:32:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63353521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定