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

@Immutable public class URLResource extends Object implements IReadableResource
Implementation of the IReadableResource interface for URL objects.
Author:
Philip Helger
  • Field Details

  • Constructor Details

    • URLResource

      public URLResource(@NonNull String sURL) throws MalformedURLException
      Create a new URL resource from a URL string.
      Parameters:
      sURL - The URL string. May not be null.
      Throws:
      MalformedURLException - If the URL string is not a valid URL.
    • URLResource

      public URLResource(@NonNull URI aURI) throws MalformedURLException
      Create a new URL resource from a URI.
      Parameters:
      aURI - The URI. May not be null.
      Throws:
      MalformedURLException - If the URI cannot be converted to a URL.
    • URLResource

      public URLResource(@NonNull URL aURL)
      Create a new URL resource from a URL.
      Parameters:
      aURL - The URL to use. May not be null.
  • Method Details

    • isExplicitURLResource

      public static boolean isExplicitURLResource(@Nullable String sName)
      Check if the passed resource name is an explicit URL resource.
      Parameters:
      sName - The name to check. May be null.
      Returns:
      true if the passed name is an explicit URL resource.
    • getResourceID

      public @NonNull String getResourceID()
      Get the unique resource ID of this URL resource.
      Specified by:
      getResourceID in interface IResourceBase
      Returns:
      The external form of the URL. Never null.
    • getPath

      public @NonNull String getPath()
      Get the path of this URL resource.
      Specified by:
      getPath in interface IResourceBase
      Returns:
      The external form of the URL. Never null.
    • getInputStream

      public static @Nullable InputStream getInputStream(@NonNull URL aURL)
      Get an input stream for the passed URL using default connect and read timeouts.
      Parameters:
      aURL - The URL to open. May not be null.
      Returns:
      null if the URL could not be opened.
    • getInputStream

      public @Nullable InputStream getInputStream()
      Get an input stream for this URL resource using default timeouts.
      Specified by:
      getInputStream in interface com.helger.base.io.iface.IHasInputStream
      Returns:
      null if 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:
      null if 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 be null.
      Returns:
      null if 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 be null.
      Returns:
      null if 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 be null.
      aExceptionHolder - An optional holder for the exception. May be null.
      Returns:
      null if the URL could not be opened.
    • isReadMultiple

      public final boolean isReadMultiple()
      Check if this resource can be read multiple times. URL resources always return true.
      Specified by:
      isReadMultiple in interface com.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:
      exists in interface IResourceBase
      Returns:
      true if the resource exists, false otherwise.
    • getAsURL

      public @NonNull URL getAsURL()
      Get this URL resource as a URL.
      Specified by:
      getAsURL in interface IResourceBase
      Returns:
      The URL. Never null.
    • getAsURI

      public @Nullable URI getAsURI()
      Get this URL resource as a URI.
      Returns:
      The URI or null if the conversion failed.
    • getAsFile

      public @Nullable File getAsFile()
      Get this URL resource as a file. Only works for file-protocol URLs.
      Specified by:
      getAsFile in interface IResourceBase
      Returns:
      The file or null if conversion is not possible.
    • getReadableCloneForPath

      public @NonNull URLResource getReadableCloneForPath(@NonNull URL aURL)
      Create a new URLResource for a different URL.
      Parameters:
      aURL - The new URL to use. May not be null.
      Returns:
      A new URLResource instance. Never null.
    • getReadableCloneForPath

      public @NonNull URLResource getReadableCloneForPath(@NonNull String sPath)
      Create a new URLResource for a different path.
      Specified by:
      getReadableCloneForPath in interface IReadableResource
      Parameters:
      sPath - The new URL string to use. May not be null.
      Returns:
      A new URLResource instance. Never null.
      Throws:
      IllegalArgumentException - If the path cannot be converted to a URL.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object