On JVM only, I need to create an OutputStream to w...
# io
h
On JVM only, I need to create an OutputStream to write 10 MB (data comes from memory, json encoding) over the wire. Should I use ByteArrayOutputStream or kotlinx Buffer and asOutputSource? Are there any performance differences?
👀 1
f
So there's an API accepting an
OutputStream
and writing data into it; then that data needs to be transferred over the wire, did I get it correctly?
h
Yes
k
Do you have to write to an in-memory buffer first? Isn't there an OutputStream that writes directly over the wire? (Such as
Socket.getOutputStream()
)?
h
AFAIk yes, (using Apache Camel with streamcache disabled). There is only an api to set an outputstream but no api to get one.
But good hint, will check it again if I can get an outputstream
f
I have a prerecorded answer: it all depends, it's better to measure how it works for your particular case 😉 Although, answering the original question, I expect that writing to a
ByteArrayOutputStream
initialized with a buffer large enough to fit the whole JSON document will be faster.
h
Thank you, I did check it and I really need to create the outputstream by myself. And I will start using the ByteArrayOutputstream, and maybe will test it later 😅