BufferedOutputStream vs ByteArrayOutputStream

扶醉桌前 提交于 2019-12-20 17:25:54

问题


Is there any advantage in wrapping a BufferedOutputStream around a ByteArrayOutputStream instead of just using the ByteArrrayOutputStream by itself?


回答1:


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.




回答2:


Absolutely none. Though BufferedWriter and BufferedReader do offer extra functionality were you to be operating on strings.




回答3:


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/.



来源:https://stackoverflow.com/questions/6821887/bufferedoutputstream-vs-bytearrayoutputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!