Package com.helger.io.resource
Class URLResource
java.lang.Object
com.helger.io.resource.URLResource
- All Implemented Interfaces:
com.helger.base.io.iface.IHasInputStream,com.helger.base.io.iface.IHasInputStreamAndReader,IReadableResource,IResourceBase
Implementation of the
IReadableResource interface for URL objects.- Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final int -
Constructor Summary
ConstructorsConstructorDescriptionURLResource(@NonNull String sURL) Create a new URL resource from a URL string.URLResource(@NonNull URI aURI) Create a new URL resource from a URI.URLResource(@NonNull URL aURL) Create a new URL resource from a URL. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanexists()Check if this URL resource exists.@Nullable FileGet this URL resource as a file.@Nullable URIgetAsURI()Get this URL resource as a URI.@NonNull URLgetAsURL()Get this URL resource as a URL.@Nullable InputStreamGet an input stream for this URL resource using default timeouts.@Nullable InputStreamgetInputStream(int nConnectTimeoutMS, int nReadTimeoutMS) Get an input stream for this URL resource with specified timeouts.@Nullable InputStreamgetInputStream(int nConnectTimeoutMS, int nReadTimeoutMS, @Nullable com.helger.base.wrapper.IMutableWrapper<IOException> aExceptionHolder) Get an input stream for this URL resource with specified timeouts and exception holder.@Nullable InputStreamgetInputStream(int nConnectTimeoutMS, int nReadTimeoutMS, @Nullable Consumer<? super URLConnection> aConnectionModifier, @Nullable com.helger.base.wrapper.IMutableWrapper<IOException> aExceptionHolder) Get an input stream for this URL resource with full control over timeouts, connection modification and exception handling.@Nullable InputStreamgetInputStream(@Nullable com.helger.base.wrapper.IMutableWrapper<IOException> aExceptionHolder) Get an input stream for this URL resource with an exception holder.static @Nullable InputStreamgetInputStream(@NonNull URL aURL) Get an input stream for the passed URL using default connect and read timeouts.@NonNull StringgetPath()Get the path of this URL resource.@NonNull URLResourcegetReadableCloneForPath(@NonNull String sPath) Create a newURLResourcefor a different path.@NonNull URLResourcegetReadableCloneForPath(@NonNull URL aURL) Create a newURLResourcefor a different URL.@NonNull StringGet the unique resource ID of this URL resource.inthashCode()static booleanisExplicitURLResource(@Nullable String sName) Check if the passed resource name is an explicit URL resource.final booleanCheck if this resource can be read multiple times.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.base.io.iface.IHasInputStream
getBufferedInputStream, withBufferedInputStreamDo, withInputStreamDoMethods inherited from interface com.helger.base.io.iface.IHasInputStreamAndReader
getBufferedReader, getReader
-
Field Details
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT- See Also:
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT- See Also:
-
-
Constructor Details
-
URLResource
Create a new URL resource from a URL string.- Parameters:
sURL- The URL string. May not benull.- Throws:
MalformedURLException- If the URL string is not a valid URL.
-
URLResource
Create a new URL resource from a URI.- Parameters:
aURI- The URI. May not benull.- Throws:
MalformedURLException- If the URI cannot be converted to a URL.
-
URLResource
Create a new URL resource from a URL.- Parameters:
aURL- The URL to use. May not benull.
-
-
Method Details
-
isExplicitURLResource
Check if the passed resource name is an explicit URL resource.- Parameters:
sName- The name to check. May benull.- Returns:
trueif the passed name is an explicit URL resource.
-
getResourceID
Get the unique resource ID of this URL resource.- Specified by:
getResourceIDin interfaceIResourceBase- Returns:
- The external form of the URL. Never
null.
-
getPath
Get the path of this URL resource.- Specified by:
getPathin interfaceIResourceBase- Returns:
- The external form of the URL. Never
null.
-
getInputStream
Get an input stream for the passed URL using default connect and read timeouts.- Parameters:
aURL- The URL to open. May not benull.- Returns:
nullif the URL could not be opened.
-
getInputStream
Get an input stream for this URL resource using default timeouts.- Specified by:
getInputStreamin interfacecom.helger.base.io.iface.IHasInputStream- Returns:
nullif the URL could not be opened.
-
getInputStream
public @Nullable InputStream getInputStream(@CheckForSigned int nConnectTimeoutMS, @CheckForSigned int nReadTimeoutMS) Get an input stream for this URL resource with specified timeouts.- Parameters:
nConnectTimeoutMS- The connect timeout in milliseconds. Use -1 for no timeout.nReadTimeoutMS- The read timeout in milliseconds. Use -1 for no timeout.- Returns:
nullif the URL could not be opened.
-
getInputStream
public @Nullable InputStream getInputStream(@Nullable com.helger.base.wrapper.IMutableWrapper<IOException> aExceptionHolder) Get an input stream for this URL resource with an exception holder.- Parameters:
aExceptionHolder- An optional holder for the exception. May benull.- Returns:
nullif the URL could not be opened.
-
getInputStream
public @Nullable InputStream getInputStream(@CheckForSigned int nConnectTimeoutMS, @CheckForSigned int nReadTimeoutMS, @Nullable com.helger.base.wrapper.IMutableWrapper<IOException> aExceptionHolder) Get an input stream for this URL resource with specified timeouts and exception holder.- Parameters:
nConnectTimeoutMS- The connect timeout in milliseconds. Use -1 for no timeout.nReadTimeoutMS- The read timeout in milliseconds. Use -1 for no timeout.aExceptionHolder- An optional holder for the exception. May benull.- Returns:
nullif the URL could not be opened.
-
getInputStream
public @Nullable InputStream getInputStream(@CheckForSigned int nConnectTimeoutMS, @CheckForSigned int nReadTimeoutMS, @Nullable Consumer<? super URLConnection> aConnectionModifier, @Nullable com.helger.base.wrapper.IMutableWrapper<IOException> aExceptionHolder) Get an input stream for this URL resource with full control over timeouts, connection modification and exception handling.- Parameters:
nConnectTimeoutMS- The connect timeout in milliseconds. Use -1 for no timeout.nReadTimeoutMS- The read timeout in milliseconds. Use -1 for no timeout.aConnectionModifier- An optional consumer to modify the URL connection before connecting. May benull.aExceptionHolder- An optional holder for the exception. May benull.- Returns:
nullif the URL could not be opened.
-
isReadMultiple
public final boolean isReadMultiple()Check if this resource can be read multiple times. URL resources always returntrue.- Specified by:
isReadMultiplein interfacecom.helger.base.io.iface.IHasInputStream- Returns:
- Always
true.
-
exists
public boolean exists()Check if this URL resource exists. For file URLs, the file existence is checked. For other protocols, an input stream is attempted to be opened.- Specified by:
existsin interfaceIResourceBase- Returns:
trueif the resource exists,falseotherwise.
-
getAsURL
Get this URL resource as a URL.- Specified by:
getAsURLin interfaceIResourceBase- Returns:
- The URL. Never
null.
-
getAsURI
Get this URL resource as a URI.- Returns:
- The URI or
nullif the conversion failed.
-
getAsFile
Get this URL resource as a file. Only works for file-protocol URLs.- Specified by:
getAsFilein interfaceIResourceBase- Returns:
- The file or
nullif conversion is not possible.
-
getReadableCloneForPath
Create a newURLResourcefor a different URL.- Parameters:
aURL- The new URL to use. May not benull.- Returns:
- A new
URLResourceinstance. Nevernull.
-
getReadableCloneForPath
Create a newURLResourcefor a different path.- Specified by:
getReadableCloneForPathin interfaceIReadableResource- Parameters:
sPath- The new URL string to use. May not benull.- Returns:
- A new
URLResourceinstance. Nevernull. - Throws:
IllegalArgumentException- If the path cannot be converted to a URL.
-
equals
-
hashCode
public int hashCode() -
toString
-