I have a library function as follows:
public static InputStream getResource(String url) throws MalformedURLException,
IOException {
return new URL(url).openConnection().getInputStream();
}
What would be the best way to add logging to this method to record the actual url response. Will I have to read the InputStream and then reset it?
(You can assume that I just want to print the results to the console for the sake of this example.)
You might copy the contents of the stream to a byte array, using a
ByteArrayOutputStream, log the content of the byte array, and then return aByteArrayInputStreamconstructed from the byte array. This puts the whole contents in memory, though.