Class CloseableURLConnection
java.lang.Object
java.net.URLConnection
org.apache.tomcat.util.buf.CloseableURLConnection
- All Implemented Interfaces:
AutoCloseable
AutoCloseable wrapper for
URLConnection that ensures all resources are released on close.
Different URLConnection subclasses require different cleanup:
HttpURLConnection(includingHttpsURLConnection) requiresdisconnect()to release the underlying socket.JarURLConnectionrequires the input stream to be closed to release the underlyingjava.util.jar.JarFile. WhensetUseCaches(false)is set, closing the input stream closes theJarFile, and callinggetInputStream()again will throwIllegalStateException.- 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
ConstructorsConstructorDescriptionCreates 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 TypeMethodDescriptionvoidaddRequestProperty(String key, String value) voidclose()voidconnect()booleanReturns the wrapped URLConnection.intgetContent(Class<?>[] classes) intlonglonggetDate()booleanbooleanbooleanlonggetHeaderField(int n) getHeaderField(String name) longgetHeaderFieldDate(String name, long defaultValue) intgetHeaderFieldInt(String name, int defaultValue) getHeaderFieldKey(int n) longgetHeaderFieldLong(String name, long defaultValue) longReturns the input stream for this URL connection.longDeprecated.intgetRequestProperty(String key) getURL()booleanvoidsetAllowUserInteraction(boolean allowuserinteraction) voidsetConnectTimeout(int timeout) voidsetDefaultUseCaches(boolean defaultusecaches) voidsetDoInput(boolean doinput) voidsetDoOutput(boolean dooutput) voidsetIfModifiedSince(long ifmodifiedsince) voidsetReadTimeout(int timeout) voidsetRequestProperty(String key, String value) voidsetUseCaches(boolean usecaches) toString()Methods inherited from class URLConnection
getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getFileNameMap, guessContentTypeFromName, guessContentTypeFromStream, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setFileNameMap
-
Constructor Details
-
CloseableURLConnection
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
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
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
- Overrides:
getOutputStreamin classURLConnection- Throws:
IOException
-
getConnectTimeout
public int getConnectTimeout()- Overrides:
getConnectTimeoutin classURLConnection
-
setConnectTimeout
public void setConnectTimeout(int timeout) - Overrides:
setConnectTimeoutin classURLConnection
-
getReadTimeout
public int getReadTimeout()- Overrides:
getReadTimeoutin classURLConnection
-
setReadTimeout
public void setReadTimeout(int timeout) - Overrides:
setReadTimeoutin classURLConnection
-
connect
- Specified by:
connectin classURLConnection- Throws:
IOException
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getInputStream
Returns the input stream for this URL connection. The stream is tracked and will be closed automatically whenclose()is called.- Overrides:
getInputStreamin classURLConnection- Returns:
- the input stream
- Throws:
IOException- if an I/O error occurs
-
getContentType
- Overrides:
getContentTypein classURLConnection
-
getContentLength
public int getContentLength()- Overrides:
getContentLengthin classURLConnection
-
getContentLengthLong
public long getContentLengthLong()- Overrides:
getContentLengthLongin classURLConnection
-
getLastModified
public long getLastModified()- Overrides:
getLastModifiedin classURLConnection
-
getHeaderField
- Overrides:
getHeaderFieldin classURLConnection
-
getURL
- Overrides:
getURLin classURLConnection
-
getContentEncoding
- Overrides:
getContentEncodingin classURLConnection
-
getExpiration
public long getExpiration()- Overrides:
getExpirationin classURLConnection
-
getDate
public long getDate()- Overrides:
getDatein classURLConnection
-
getHeaderFields
- Overrides:
getHeaderFieldsin classURLConnection
-
getHeaderFieldInt
- Overrides:
getHeaderFieldIntin classURLConnection
-
getHeaderFieldLong
- Overrides:
getHeaderFieldLongin classURLConnection
-
getHeaderFieldDate
- Overrides:
getHeaderFieldDatein classURLConnection
-
getHeaderFieldKey
- Overrides:
getHeaderFieldKeyin classURLConnection
-
getHeaderField
- Overrides:
getHeaderFieldin classURLConnection
-
getContent
- Overrides:
getContentin classURLConnection- Throws:
IOException
-
getContent
- Overrides:
getContentin classURLConnection- Throws:
IOException
-
getPermission
Deprecated.- Overrides:
getPermissionin classURLConnection- Throws:
IOException
-
toString
- Overrides:
toStringin classURLConnection
-
setDoInput
public void setDoInput(boolean doinput) - Overrides:
setDoInputin classURLConnection
-
getDoInput
public boolean getDoInput()- Overrides:
getDoInputin classURLConnection
-
setDoOutput
public void setDoOutput(boolean dooutput) - Overrides:
setDoOutputin classURLConnection
-
getDoOutput
public boolean getDoOutput()- Overrides:
getDoOutputin classURLConnection
-
setAllowUserInteraction
public void setAllowUserInteraction(boolean allowuserinteraction) - Overrides:
setAllowUserInteractionin classURLConnection
-
getAllowUserInteraction
public boolean getAllowUserInteraction()- Overrides:
getAllowUserInteractionin classURLConnection
-
setUseCaches
public void setUseCaches(boolean usecaches) - Overrides:
setUseCachesin classURLConnection
-
getUseCaches
public boolean getUseCaches()- Overrides:
getUseCachesin classURLConnection
-
setIfModifiedSince
public void setIfModifiedSince(long ifmodifiedsince) - Overrides:
setIfModifiedSincein classURLConnection
-
getIfModifiedSince
public long getIfModifiedSince()- Overrides:
getIfModifiedSincein classURLConnection
-
getDefaultUseCaches
public boolean getDefaultUseCaches()- Overrides:
getDefaultUseCachesin classURLConnection
-
setDefaultUseCaches
public void setDefaultUseCaches(boolean defaultusecaches) - Overrides:
setDefaultUseCachesin classURLConnection
-
setRequestProperty
- Overrides:
setRequestPropertyin classURLConnection
-
addRequestProperty
- Overrides:
addRequestPropertyin classURLConnection
-
getRequestProperty
- Overrides:
getRequestPropertyin classURLConnection
-
getRequestProperties
- Overrides:
getRequestPropertiesin classURLConnection
-