Class CloseableURLConnection

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public final class CloseableURLConnection
    extends java.net.URLConnection
    implements java.lang.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().

    • Constructor Detail

      • CloseableURLConnection

        public CloseableURLConnection​(java.net.URL url)
                               throws java.io.IOException
        Creates a new wrapper for the given URL by opening a connection.

        setUseCaches(false) is called on the connection immediately.

        Parameters:
        url - the URL to connect to
        Throws:
        java.io.IOException - if an I/O error occurs while opening the connection
      • CloseableURLConnection

        public CloseableURLConnection​(java.net.URLConnection connection)
        Creates a new wrapper for the given URLConnection.

        setUseCaches(false) is called on the connection immediately.

        Parameters:
        connection - the URLConnection to wrap
    • Method Detail

      • getConnection

        public java.net.URLConnection getConnection()
        Returns the wrapped URLConnection. Some subclasses can have additional methods, in which case the wrapped URLConnection needs to be accessed.
        Returns:
        the wrapped URLConnection
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        Overrides:
        getOutputStream in class java.net.URLConnection
        Throws:
        java.io.IOException
      • getConnectTimeout

        public int getConnectTimeout()
        Overrides:
        getConnectTimeout in class java.net.URLConnection
      • setConnectTimeout

        public void setConnectTimeout​(int timeout)
        Overrides:
        setConnectTimeout in class java.net.URLConnection
      • getReadTimeout

        public int getReadTimeout()
        Overrides:
        getReadTimeout in class java.net.URLConnection
      • setReadTimeout

        public void setReadTimeout​(int timeout)
        Overrides:
        setReadTimeout in class java.net.URLConnection
      • connect

        public void connect()
                     throws java.io.IOException
        Specified by:
        connect in class java.net.URLConnection
        Throws:
        java.io.IOException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Returns the input stream for this URL connection. The stream is tracked and will be closed automatically when close() is called.
        Overrides:
        getInputStream in class java.net.URLConnection
        Returns:
        the input stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • getContentType

        public java.lang.String getContentType()
        Overrides:
        getContentType in class java.net.URLConnection
      • getContentLength

        public int getContentLength()
        Overrides:
        getContentLength in class java.net.URLConnection
      • getContentLengthLong

        public long getContentLengthLong()
        Overrides:
        getContentLengthLong in class java.net.URLConnection
      • getLastModified

        public long getLastModified()
        Overrides:
        getLastModified in class java.net.URLConnection
      • getHeaderField

        public java.lang.String getHeaderField​(java.lang.String name)
        Overrides:
        getHeaderField in class java.net.URLConnection
      • getURL

        public java.net.URL getURL()
        Overrides:
        getURL in class java.net.URLConnection
      • getContentEncoding

        public java.lang.String getContentEncoding()
        Overrides:
        getContentEncoding in class java.net.URLConnection
      • getExpiration

        public long getExpiration()
        Overrides:
        getExpiration in class java.net.URLConnection
      • getDate

        public long getDate()
        Overrides:
        getDate in class java.net.URLConnection
      • getHeaderFields

        public java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaderFields()
        Overrides:
        getHeaderFields in class java.net.URLConnection
      • getHeaderFieldInt

        public int getHeaderFieldInt​(java.lang.String name,
                                     int defaultValue)
        Overrides:
        getHeaderFieldInt in class java.net.URLConnection
      • getHeaderFieldLong

        public long getHeaderFieldLong​(java.lang.String name,
                                       long defaultValue)
        Overrides:
        getHeaderFieldLong in class java.net.URLConnection
      • getHeaderFieldDate

        public long getHeaderFieldDate​(java.lang.String name,
                                       long defaultValue)
        Overrides:
        getHeaderFieldDate in class java.net.URLConnection
      • getHeaderFieldKey

        public java.lang.String getHeaderFieldKey​(int n)
        Overrides:
        getHeaderFieldKey in class java.net.URLConnection
      • getHeaderField

        public java.lang.String getHeaderField​(int n)
        Overrides:
        getHeaderField in class java.net.URLConnection
      • getContent

        public java.lang.Object getContent()
                                    throws java.io.IOException
        Overrides:
        getContent in class java.net.URLConnection
        Throws:
        java.io.IOException
      • getContent

        public java.lang.Object getContent​(java.lang.Class[] classes)
                                    throws java.io.IOException
        Overrides:
        getContent in class java.net.URLConnection
        Throws:
        java.io.IOException
      • getPermission

        @Deprecated
        public java.security.Permission getPermission()
                                               throws java.io.IOException
        Deprecated.
        Overrides:
        getPermission in class java.net.URLConnection
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.net.URLConnection
      • setDoInput

        public void setDoInput​(boolean doinput)
        Overrides:
        setDoInput in class java.net.URLConnection
      • getDoInput

        public boolean getDoInput()
        Overrides:
        getDoInput in class java.net.URLConnection
      • setDoOutput

        public void setDoOutput​(boolean dooutput)
        Overrides:
        setDoOutput in class java.net.URLConnection
      • getDoOutput

        public boolean getDoOutput()
        Overrides:
        getDoOutput in class java.net.URLConnection
      • setAllowUserInteraction

        public void setAllowUserInteraction​(boolean allowuserinteraction)
        Overrides:
        setAllowUserInteraction in class java.net.URLConnection
      • getAllowUserInteraction

        public boolean getAllowUserInteraction()
        Overrides:
        getAllowUserInteraction in class java.net.URLConnection
      • setUseCaches

        public void setUseCaches​(boolean usecaches)
        Overrides:
        setUseCaches in class java.net.URLConnection
      • getUseCaches

        public boolean getUseCaches()
        Overrides:
        getUseCaches in class java.net.URLConnection
      • setIfModifiedSince

        public void setIfModifiedSince​(long ifmodifiedsince)
        Overrides:
        setIfModifiedSince in class java.net.URLConnection
      • getIfModifiedSince

        public long getIfModifiedSince()
        Overrides:
        getIfModifiedSince in class java.net.URLConnection
      • getDefaultUseCaches

        public boolean getDefaultUseCaches()
        Overrides:
        getDefaultUseCaches in class java.net.URLConnection
      • setDefaultUseCaches

        public void setDefaultUseCaches​(boolean defaultusecaches)
        Overrides:
        setDefaultUseCaches in class java.net.URLConnection
      • setRequestProperty

        public void setRequestProperty​(java.lang.String key,
                                       java.lang.String value)
        Overrides:
        setRequestProperty in class java.net.URLConnection
      • addRequestProperty

        public void addRequestProperty​(java.lang.String key,
                                       java.lang.String value)
        Overrides:
        addRequestProperty in class java.net.URLConnection
      • getRequestProperty

        public java.lang.String getRequestProperty​(java.lang.String key)
        Overrides:
        getRequestProperty in class java.net.URLConnection
      • getRequestProperties

        public java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getRequestProperties()
        Overrides:
        getRequestProperties in class java.net.URLConnection