0

Should I close HttpUrlConnection and InputStream in this case? Only closing the connection will close the stream also? I feel that it's a bad practice but don't know exactly why.

Closing both:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
try (AutoCloseable ac = con::disconnect) {
    int responseCode = con.getResponseCode();
    try (InputStream ins = responseCode >= 400 ? con.getErrorStream() : con.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(ins))) {
        // receive response
    }
}

Closing Connection only:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
try (AutoCloseable ac = con::disconnect) {
    int responseCode = con.getResponseCode();
    BufferedReader in = new BufferedReader(new InputStreamReader(ins)))
    // ins will close automatically when con closes?
    // receive response
}
jpcairesf
  • 5
  • 2
  • if uncle Bob know that you nest exception handling code he will find you :) do not say i did not worn you :)))) and i see Volodya Lombrozo explain it well –  Jan 24 '23 at 16:03
  • @justsomeone this code I got from another thread. In real life I made connection and input stream resources at the same try statement. Will I stay alive? – jpcairesf Jan 25 '23 at 01:32
  • loool you fine and this uncle bob video for clean code https://www.youtube.com/watch?v=7EmboKQH8lM&t=5811s and in my humble opinion everyone should learn about clean code –  Jan 25 '23 at 06:58

1 Answers1

0

When you disconnect the HttpURLConnection object, it MAY also close any InputStream or OutputStream that it has opened.

HttpURLConnection.disconnect() method description:

Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.

You can read more here.

In turn, Socket.close() method description:

Closing this socket will also close the socket's InputStream and OutputStream. If this socket has an associated channel then the channel is closed as well.

You can read more here.

But pay attention that "disconnecting" HttpURLConnection doesn’t mandatory close the Socket. It have been already discussed quite well in that thread:

(When keepAlive == true) If client called HttpURLConnection.getInputSteam().close(), the later call to HttpURLConnection.disconnect() will NOT close the Socket. i.e. The Socket is reused (cached) If client does not call close(), call disconnect() will close the InputStream and close the Socket. So in order to reuse the Socket, just call InputStream.close(). Do not call HttpURLConnection.disconnect().

On the other hand, in the official oracle tutorials they suggest to close InputStream explicitly in order to be sure that resources don’t leak.

Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34