I'm fetching a web page using the Apache httpcomponents Java library. After connecting the result I get is an HttpEntity
which has a method getContent()
which returns an InputStream
and also has a method writeTo()
which writes to an OutputStream.
I want to turn the result into a String for extracting information. What is the most elegant (and safe) way to do this?
Some possible solutions:
- Write to a
ByteArrayOutputStream
and then convert those bytes to a String with a String constructor - use InputStreamReader to read straight from the stream, and put into a StringBuilder
Both of these feel a bit ugly. Would you recommend choosing one of these or something else?