java BufferedOutputStream 与 ByteArrayOutputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6821887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BufferedOutputStream vs ByteArrayOutputStream
提问by Hank
Is there any advantage in wrapping a BufferedOutputStream around a ByteArrayOutputStream instead of just using the ByteArrrayOutputStream by itself?
将 BufferedOutputStream 包裹在 ByteArrayOutputStream 周围而不是单独使用 ByteArrrayOutputStream 是否有任何优势?
采纳答案by Binus
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.
通常 BufferedOutputStream 包装器主要用于避免频繁的磁盘或网络写入。单独编写许多小块比进行几个相当大的操作要昂贵得多。ByteArrayOutputStream 在内存中运行,所以我认为包装是没有意义的。
If you want to know the exact answer, try to create a simple performance-measuring application.
如果您想知道确切的答案,请尝试创建一个简单的性能测量应用程序。
回答by Dunes
Absolutely none. Though BufferedWriter and BufferedReader do offer extra functionality were you to be operating on strings.
绝对没有。尽管 BufferedWriter 和 BufferedReader 确实提供了额外的功能,但您需要对字符串进行操作。
回答by DrLuck
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/.
如果您想获得高性能,不建议使用 ByteArrayOutputStream,但一个有趣的功能是发送未知长度的消息。要更好地理解这两种方法的工作原理,请参阅http://java-performance.info/java-io-bytearrayoutputstream/。