Package org.apache.tomcat.util.buf
Class CloseableURLConnection
- java.lang.Object
-
- java.net.URLConnection
-
- org.apache.tomcat.util.buf.CloseableURLConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public final class CloseableURLConnection extends java.net.URLConnection implements java.lang.AutoCloseableAutoCloseable wrapper forURLConnectionthat 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 whethergetInputStream()was called, and performs the appropriate cleanup inclose().
-
-
Constructor Summary
Constructors Constructor Description CloseableURLConnection(java.net.URL url)Creates a new wrapper for the given URL by opening a connection.CloseableURLConnection(java.net.URLConnection connection)Creates a new wrapper for the given URLConnection.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddRequestProperty(java.lang.String key, java.lang.String value)voidclose()voidconnect()booleangetAllowUserInteraction()java.net.URLConnectiongetConnection()Returns the wrapped URLConnection.intgetConnectTimeout()java.lang.ObjectgetContent()java.lang.ObjectgetContent(java.lang.Class[] classes)java.lang.StringgetContentEncoding()intgetContentLength()longgetContentLengthLong()java.lang.StringgetContentType()longgetDate()booleangetDefaultUseCaches()booleangetDoInput()booleangetDoOutput()longgetExpiration()java.lang.StringgetHeaderField(int n)java.lang.StringgetHeaderField(java.lang.String name)longgetHeaderFieldDate(java.lang.String name, long defaultValue)intgetHeaderFieldInt(java.lang.String name, int defaultValue)java.lang.StringgetHeaderFieldKey(int n)longgetHeaderFieldLong(java.lang.String name, long defaultValue)java.util.Map<java.lang.String,java.util.List<java.lang.String>>getHeaderFields()longgetIfModifiedSince()java.io.InputStreamgetInputStream()Returns the input stream for this URL connection.longgetLastModified()java.io.OutputStreamgetOutputStream()java.security.PermissiongetPermission()Deprecated.intgetReadTimeout()java.util.Map<java.lang.String,java.util.List<java.lang.String>>getRequestProperties()java.lang.StringgetRequestProperty(java.lang.String key)java.net.URLgetURL()booleangetUseCaches()voidsetAllowUserInteraction(boolean allowuserinteraction)voidsetConnectTimeout(int timeout)voidsetDefaultUseCaches(boolean defaultusecaches)voidsetDoInput(boolean doinput)voidsetDoOutput(boolean dooutput)voidsetIfModifiedSince(long ifmodifiedsince)voidsetReadTimeout(int timeout)voidsetRequestProperty(java.lang.String key, java.lang.String value)voidsetUseCaches(boolean usecaches)java.lang.StringtoString()-
Methods inherited from class java.net.URLConnection
getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getFileNameMap, guessContentTypeFromName, guessContentTypeFromStream, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setFileNameMap
-
-
-
-
Constructor Detail
-
CloseableURLConnection
public CloseableURLConnection(java.net.URL url) throws java.io.IOExceptionCreates 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:
getOutputStreamin classjava.net.URLConnection- Throws:
java.io.IOException
-
getConnectTimeout
public int getConnectTimeout()
- Overrides:
getConnectTimeoutin classjava.net.URLConnection
-
setConnectTimeout
public void setConnectTimeout(int timeout)
- Overrides:
setConnectTimeoutin classjava.net.URLConnection
-
getReadTimeout
public int getReadTimeout()
- Overrides:
getReadTimeoutin classjava.net.URLConnection
-
setReadTimeout
public void setReadTimeout(int timeout)
- Overrides:
setReadTimeoutin classjava.net.URLConnection
-
connect
public void connect() throws java.io.IOException- Specified by:
connectin classjava.net.URLConnection- Throws:
java.io.IOException
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOExceptionReturns the input stream for this URL connection. The stream is tracked and will be closed automatically whenclose()is called.- Overrides:
getInputStreamin classjava.net.URLConnection- Returns:
- the input stream
- Throws:
java.io.IOException- if an I/O error occurs
-
getContentType
public java.lang.String getContentType()
- Overrides:
getContentTypein classjava.net.URLConnection
-
getContentLength
public int getContentLength()
- Overrides:
getContentLengthin classjava.net.URLConnection
-
getContentLengthLong
public long getContentLengthLong()
- Overrides:
getContentLengthLongin classjava.net.URLConnection
-
getLastModified
public long getLastModified()
- Overrides:
getLastModifiedin classjava.net.URLConnection
-
getHeaderField
public java.lang.String getHeaderField(java.lang.String name)
- Overrides:
getHeaderFieldin classjava.net.URLConnection
-
getURL
public java.net.URL getURL()
- Overrides:
getURLin classjava.net.URLConnection
-
getContentEncoding
public java.lang.String getContentEncoding()
- Overrides:
getContentEncodingin classjava.net.URLConnection
-
getExpiration
public long getExpiration()
- Overrides:
getExpirationin classjava.net.URLConnection
-
getDate
public long getDate()
- Overrides:
getDatein classjava.net.URLConnection
-
getHeaderFields
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaderFields()
- Overrides:
getHeaderFieldsin classjava.net.URLConnection
-
getHeaderFieldInt
public int getHeaderFieldInt(java.lang.String name, int defaultValue)- Overrides:
getHeaderFieldIntin classjava.net.URLConnection
-
getHeaderFieldLong
public long getHeaderFieldLong(java.lang.String name, long defaultValue)- Overrides:
getHeaderFieldLongin classjava.net.URLConnection
-
getHeaderFieldDate
public long getHeaderFieldDate(java.lang.String name, long defaultValue)- Overrides:
getHeaderFieldDatein classjava.net.URLConnection
-
getHeaderFieldKey
public java.lang.String getHeaderFieldKey(int n)
- Overrides:
getHeaderFieldKeyin classjava.net.URLConnection
-
getHeaderField
public java.lang.String getHeaderField(int n)
- Overrides:
getHeaderFieldin classjava.net.URLConnection
-
getContent
public java.lang.Object getContent() throws java.io.IOException- Overrides:
getContentin classjava.net.URLConnection- Throws:
java.io.IOException
-
getContent
public java.lang.Object getContent(java.lang.Class[] classes) throws java.io.IOException- Overrides:
getContentin classjava.net.URLConnection- Throws:
java.io.IOException
-
getPermission
@Deprecated public java.security.Permission getPermission() throws java.io.IOExceptionDeprecated.- Overrides:
getPermissionin classjava.net.URLConnection- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.net.URLConnection
-
setDoInput
public void setDoInput(boolean doinput)
- Overrides:
setDoInputin classjava.net.URLConnection
-
getDoInput
public boolean getDoInput()
- Overrides:
getDoInputin classjava.net.URLConnection
-
setDoOutput
public void setDoOutput(boolean dooutput)
- Overrides:
setDoOutputin classjava.net.URLConnection
-
getDoOutput
public boolean getDoOutput()
- Overrides:
getDoOutputin classjava.net.URLConnection
-
setAllowUserInteraction
public void setAllowUserInteraction(boolean allowuserinteraction)
- Overrides:
setAllowUserInteractionin classjava.net.URLConnection
-
getAllowUserInteraction
public boolean getAllowUserInteraction()
- Overrides:
getAllowUserInteractionin classjava.net.URLConnection
-
setUseCaches
public void setUseCaches(boolean usecaches)
- Overrides:
setUseCachesin classjava.net.URLConnection
-
getUseCaches
public boolean getUseCaches()
- Overrides:
getUseCachesin classjava.net.URLConnection
-
setIfModifiedSince
public void setIfModifiedSince(long ifmodifiedsince)
- Overrides:
setIfModifiedSincein classjava.net.URLConnection
-
getIfModifiedSince
public long getIfModifiedSince()
- Overrides:
getIfModifiedSincein classjava.net.URLConnection
-
getDefaultUseCaches
public boolean getDefaultUseCaches()
- Overrides:
getDefaultUseCachesin classjava.net.URLConnection
-
setDefaultUseCaches
public void setDefaultUseCaches(boolean defaultusecaches)
- Overrides:
setDefaultUseCachesin classjava.net.URLConnection
-
setRequestProperty
public void setRequestProperty(java.lang.String key, java.lang.String value)- Overrides:
setRequestPropertyin classjava.net.URLConnection
-
addRequestProperty
public void addRequestProperty(java.lang.String key, java.lang.String value)- Overrides:
addRequestPropertyin classjava.net.URLConnection
-
getRequestProperty
public java.lang.String getRequestProperty(java.lang.String key)
- Overrides:
getRequestPropertyin classjava.net.URLConnection
-
getRequestProperties
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getRequestProperties()
- Overrides:
getRequestPropertiesin classjava.net.URLConnection
-
-