Class CloseableURLConnection

java.lang.Object
java.net.URLConnection
org.apache.tomcat.util.buf.CloseableURLConnection
All Implemented Interfaces:
AutoCloseable

public final class CloseableURLConnection extends URLConnection implements AutoCloseable
AutoCloseable wrapper for URLConnection that ensures all resources are released on close.

Different URLConnection subclasses require different cleanup:

  • HttpURLConnection (including HttpsURLConnection) requires disconnect() to release the underlying socket.
  • JarURLConnection requires the input stream to be closed to release the underlying java.util.jar.JarFile. When setUseCaches(false) is set, closing the input stream closes the JarFile, and calling getInputStream() again will throw IllegalStateException.
  • Other URLConnection types only require any obtained streams to be closed.

This wrapper sets setUseCaches(false) on the wrapped connection, tracks whether getInputStream() was called, and performs the appropriate cleanup in close().