# 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()`.

## Field Summary

### Fields inherited from class URLConnection

`allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches`

## Constructor Summary

Constructors
Constructor
Description
`CloseableURLConnection(URL url)`
Creates a new wrapper for the given URL by opening a connection.
`CloseableURLConnection(URLConnection connection)`
Creates a new wrapper for the given URLConnection.

## Method Summary

Modifier and Type
Method
Description
`void`
`addRequestProperty(String key, String value)`
`void`
`close()`
`void`
`connect()`
`boolean`
`getAllowUserInteraction()`
`URLConnection`
`getConnection()`
Returns the wrapped URLConnection.
`int`
`getConnectTimeout()`
`Object`
`getContent()`
`Object`
`getContent(Class<?>[] classes)`
`String`
`getContentEncoding()`
`int`
`getContentLength()`
`long`
`getContentLengthLong()`
`String`
`getContentType()`
`long`
`getDate()`
`boolean`
`getDefaultUseCaches()`
`boolean`
`getDoInput()`
`boolean`
`getDoOutput()`
`long`
`getExpiration()`
`String`
`getHeaderField(int n)`
`String`
`getHeaderField(String name)`
`long`
`getHeaderFieldDate(String name, long defaultValue)`
`int`
`getHeaderFieldInt(String name, int defaultValue)`
`String`
`getHeaderFieldKey(int n)`
`long`
`getHeaderFieldLong(String name, long defaultValue)`
`Map<String, List<String>>`
`getHeaderFields()`
`long`
`getIfModifiedSince()`
`InputStream`
`getInputStream()`
Returns the input stream for this URL connection.
`long`
`getLastModified()`
`OutputStream`
`getOutputStream()`
`Permission`
`getPermission()`
Deprecated.
`int`
`getReadTimeout()`
`Map<String, List<String>>`
`getRequestProperties()`
`String`
`getRequestProperty(String key)`
`URL`
`getURL()`
`boolean`
`getUseCaches()`
`void`
`setAllowUserInteraction(boolean allowuserinteraction)`
`void`
`setConnectTimeout(int timeout)`
`void`
`setDefaultUseCaches(boolean defaultusecaches)`
`void`
`setDoInput(boolean doinput)`
`void`
`setDoOutput(boolean dooutput)`
`void`
`setIfModifiedSince(long ifmodifiedsince)`
`void`
`setReadTimeout(int timeout)`
`void`
`setRequestProperty(String key, String value)`
`void`
`setUseCaches(boolean usecaches)`
`String`
`toString()`

### Methods inherited from class URLConnection

`getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getFileNameMap, guessContentTypeFromName, guessContentTypeFromStream, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setFileNameMap`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### CloseableURLConnection

public CloseableURLConnection(URL url) throws 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:
  `IOException` - if an I/O error occurs while opening the connection

### CloseableURLConnection

public CloseableURLConnection(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 Details

### getConnection

public 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 OutputStream getOutputStream() throws IOException

Overrides:
  `getOutputStream` in class `URLConnection`
Throws:
  `IOException`

### getConnectTimeout

public int getConnectTimeout()

Overrides:
  `getConnectTimeout` in class `URLConnection`

### setConnectTimeout

public void setConnectTimeout(int timeout)

Overrides:
  `setConnectTimeout` in class `URLConnection`

### getReadTimeout

public int getReadTimeout()

Overrides:
  `getReadTimeout` in class `URLConnection`

### setReadTimeout

public void setReadTimeout(int timeout)

Overrides:
  `setReadTimeout` in class `URLConnection`

### connect

public void connect() throws IOException

Specified by:
  `connect` in class `URLConnection`
Throws:
  `IOException`

### close

public void close()

Specified by:
  `close` in interface `AutoCloseable`

### getInputStream

public InputStream getInputStream() throws 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 `URLConnection`
Returns:
  the input stream
Throws:
  `IOException` - if an I/O error occurs

### getContentType

public String getContentType()

Overrides:
  `getContentType` in class `URLConnection`

### getContentLength

public int getContentLength()

Overrides:
  `getContentLength` in class `URLConnection`

### getContentLengthLong

public long getContentLengthLong()

Overrides:
  `getContentLengthLong` in class `URLConnection`

### getLastModified

public long getLastModified()

Overrides:
  `getLastModified` in class `URLConnection`

### getHeaderField

public String getHeaderField(String name)

Overrides:
  `getHeaderField` in class `URLConnection`

### getURL

public URL getURL()

Overrides:
  `getURL` in class `URLConnection`

### getContentEncoding

public String getContentEncoding()

Overrides:
  `getContentEncoding` in class `URLConnection`

### getExpiration

public long getExpiration()

Overrides:
  `getExpiration` in class `URLConnection`

### getDate

public long getDate()

Overrides:
  `getDate` in class `URLConnection`

### getHeaderFields

public Map<String, List<String>> getHeaderFields()

Overrides:
  `getHeaderFields` in class `URLConnection`

### getHeaderFieldInt

public int getHeaderFieldInt(String name, int defaultValue)

Overrides:
  `getHeaderFieldInt` in class `URLConnection`

### getHeaderFieldLong

public long getHeaderFieldLong(String name, long defaultValue)

Overrides:
  `getHeaderFieldLong` in class `URLConnection`

### getHeaderFieldDate

public long getHeaderFieldDate(String name, long defaultValue)

Overrides:
  `getHeaderFieldDate` in class `URLConnection`

### getHeaderFieldKey

public String getHeaderFieldKey(int n)

Overrides:
  `getHeaderFieldKey` in class `URLConnection`

### getHeaderField

public String getHeaderField(int n)

Overrides:
  `getHeaderField` in class `URLConnection`

### getContent

public Object getContent() throws IOException

Overrides:
  `getContent` in class `URLConnection`
Throws:
  `IOException`

### getContent

public Object getContent(Class<?>[] classes) throws IOException

Overrides:
  `getContent` in class `URLConnection`
Throws:
  `IOException`

### getPermission

@Deprecated public Permission getPermission() throws IOException
Deprecated.

Overrides:
  `getPermission` in class `URLConnection`
Throws:
  `IOException`

### toString

public String toString()

Overrides:
  `toString` in class `URLConnection`

### setDoInput

public void setDoInput(boolean doinput)

Overrides:
  `setDoInput` in class `URLConnection`

### getDoInput

public boolean getDoInput()

Overrides:
  `getDoInput` in class `URLConnection`

### setDoOutput

public void setDoOutput(boolean dooutput)

Overrides:
  `setDoOutput` in class `URLConnection`

### getDoOutput

public boolean getDoOutput()

Overrides:
  `getDoOutput` in class `URLConnection`

### setAllowUserInteraction

public void setAllowUserInteraction(boolean allowuserinteraction)

Overrides:
  `setAllowUserInteraction` in class `URLConnection`

### getAllowUserInteraction

public boolean getAllowUserInteraction()

Overrides:
  `getAllowUserInteraction` in class `URLConnection`

### setUseCaches

public void setUseCaches(boolean usecaches)

Overrides:
  `setUseCaches` in class `URLConnection`

### getUseCaches

public boolean getUseCaches()

Overrides:
  `getUseCaches` in class `URLConnection`

### setIfModifiedSince

public void setIfModifiedSince(long ifmodifiedsince)

Overrides:
  `setIfModifiedSince` in class `URLConnection`

### getIfModifiedSince

public long getIfModifiedSince()

Overrides:
  `getIfModifiedSince` in class `URLConnection`

### getDefaultUseCaches

public boolean getDefaultUseCaches()

Overrides:
  `getDefaultUseCaches` in class `URLConnection`

### setDefaultUseCaches

public void setDefaultUseCaches(boolean defaultusecaches)

Overrides:
  `setDefaultUseCaches` in class `URLConnection`

### setRequestProperty

public void setRequestProperty(String key, String value)

Overrides:
  `setRequestProperty` in class `URLConnection`

### addRequestProperty

public void addRequestProperty(String key, String value)

Overrides:
  `addRequestProperty` in class `URLConnection`

### getRequestProperty

public String getRequestProperty(String key)

Overrides:
  `getRequestProperty` in class `URLConnection`

### getRequestProperties

public Map<String, List<String>> getRequestProperties()

Overrides:
  `getRequestProperties` in class `URLConnection`