I read this post but I am not following. I have seen this but have not seen a proper example of converting a ByteArrayInputStream to String using a ByteArrayOutputStream.
To retrieve the contents of a ByteArrayInputStream as a String, is using a ByteArrayOutputstream recommended or is there a more preferable way?
I was considering this example and extend ByteArrayInputStream and utilize a Decorator to increase functionality at run time. Any interest in this being a better solution to employing a ByteArrayOutputStream?
A
ByteArrayOutputStreamcan read from anyInputStreamand at the end yield abyte[].However with a
ByteArrayInputStreamit is simpler:For a
ByteArrayInputStreamavailable()yields the total number of bytes.Addendum 2021-11-16
Since java 9 you can use the shorter readAllBytes.
Answer to comment: using ByteArrayOutputStream
Here in may be any InputStream.
Since java 10 there also is a
ByteArrayOutputStream#toString(Charset).