BufferedOutputStream 与 ByteArrayOutputStream
将 BufferedOutputStream 包装在 ByteArrayOutputStream 周围而不是仅使用 ByteArrayOutputStream 本身有什么优势吗?
Is there any advantage in wrapping a BufferedOutputStream around a ByteArrayOutputStream instead of just using the ByteArrrayOutputStream by itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,BufferedOutputStream 包装器主要用于避免频繁的磁盘或网络写入。单独编写许多小块可能比进行几个相当大的操作要昂贵得多。 ByteArrayOutputStream 在内存中运行,所以我认为包装是没有意义的。
如果您想知道确切的答案,请尝试创建一个简单的性能测量应用程序。
Generally BufferedOutputStream wrapper is mostly used to avoid frequent disk or network writes. It can be much more expensive to separately write a lot of small pieces than make several rather large operations. The ByteArrayOutputStream operates in memory, so I think the wrapping is pointless.
If you want to know the exact answer, try to create a simple performance-measuring application.
绝对没有。尽管 BufferedWriter 和 BufferedReader 确实提供了额外的功能,但如果您要对字符串进行操作。
Absolutely none. Though BufferedWriter and BufferedReader do offer extra functionality were you to be operating on strings.
如果您想获得高性能,不建议使用 ByteArrayOutputStream,但一个有趣的功能是发送未知长度的消息。要更好地理解这两种方法的工作原理,请参阅 http://java-performance.info/ java-io-bytearrayoutputstream/.
ByteArrayOutputStream is not recommended if you want to get high performance, but one interesting feature is to send a message with unknown length. For a better comprehension about how these two methods work, see http://java-performance.info/java-io-bytearrayoutputstream/.