I have a method that opens a HttpURLConnection and then returns the InputStream from the response to the caller:
// Callers are responsible for closing the returned input stream
public InputStream get()
{
final URL url= new URL("http://example.com");
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setRequestMethod("GET");
return httpUrlConnection.getInputStream();
// Don't close httpUrlConnection as that will close the returned input stream
// which the caller is responsible for closing.
}
My problem is I cannot close the HttpURLConnection in this method as that would close the underlying InputStream, which the caller of this method is responsible for closing. I have no control over the callers.
How bad is it that the HttpUrlConnection is not closed? Will it eventually be closed? Or should I implement some mechanism to close it after a period of time has elapsed? Or maybe copy/clone the InputStream and return the copy, which would allow me to close the HttpURLConnection?