Class URLInputStreamResource

java.lang.Object
org.springframework.core.io.AbstractResource
org.cruk.clarity.api.impl.URLInputStreamResource
All Implemented Interfaces:
InputStreamSource, Resource

public class URLInputStreamResource extends AbstractResource
Resource implementation based around a URL. Can be used for any source where the URL protocol is supported by the JVM.
Since:
2.23
  • Constructor Details

    • URLInputStreamResource

      public URLInputStreamResource(URL url) throws IOException
      Create a new resource around the given URL.
      Parameters:
      url - The URL of the resource.
      Throws:
      IOException - if the resource cannot be accessed or the URL does not allow connection.
    • URLInputStreamResource

      public URLInputStreamResource(URL url, int bufferSize) throws IOException
      Create a new resource around the given URL.
      Parameters:
      url - The URL of the resource.
      bufferSize - The size of the buffer around the URL's input stream. If this is zero or less, no buffer will be put in place.
      Throws:
      IOException - if the resource cannot be accessed or the URL does not allow connection.
  • Method Details

    • finalize

      protected void finalize()
      Make sure things are closed.
      Overrides:
      finalize in class Object
    • close

      public void close()
      Close the resource's input stream and disconnect the URL connection.
    • getFile

      public File getFile()
      Get a file object for the URL.
      Specified by:
      getFile in interface Resource
      Overrides:
      getFile in class AbstractResource
      Returns:
      The file object for the resource if the URL was a local file resource, null if the URL is of any other type.
    • getFilename

      public String getFilename()
      Get the file name of the resource.
      Specified by:
      getFilename in interface Resource
      Overrides:
      getFilename in class AbstractResource
      Returns:
      The resource's file name.
    • exists

      public boolean exists()
      Check whether the resource exists. For some protocols this can be done explicitly, but for others this test cannot take place and it is assumed the resource does exist.
      Specified by:
      exists in interface Resource
      Overrides:
      exists in class AbstractResource
      Returns:
      true if the resource exists, false if not.
    • getURL

      public URL getURL()
      Get the URL of the resource.
      Specified by:
      getURL in interface Resource
      Overrides:
      getURL in class AbstractResource
      Returns:
      The resource URL.
    • getURI

      public URI getURI()
      Get the URI of the resource.
      Specified by:
      getURI in interface Resource
      Overrides:
      getURI in class AbstractResource
      Returns:
      The resource URI.
    • getDescription

      public String getDescription()
      Get a description of the resource. For these types, this is the URL in string form.
      Returns:
      The URL as a string.
    • getInputStream

      public InputStream getInputStream()
      Get the input stream to read the resource content. Client code should call close() to close this stream and associated resources, but if the stream is closed explicitly it should be fine as long as this object is not used any further.
      Returns:
      An input stream.
    • contentLength

      public long contentLength()
      Get the size of the resource.
      Specified by:
      contentLength in interface Resource
      Overrides:
      contentLength in class AbstractResource
      Returns:
      The size in bytes of the resource. Will return -1 if this cannot be determined.
    • lastModified

      public long lastModified()
      Get the last modified time of the resource.
      Specified by:
      lastModified in interface Resource
      Overrides:
      lastModified in class AbstractResource
      Returns:
      The last modified time in milliseconds.
      See Also:
    • equals

      public boolean equals(Object obj)
      Test for equality. Two URLInputStreamResource objects are considered equal if they point to the same resource.
      Overrides:
      equals in class AbstractResource
      Parameters:
      obj - The object to test against.
      Returns:
      true if obj is another URLInputStreamResource and both it and this have the same URL.
    • hashCode

      public int hashCode()
      Get a hash code for this URLInputStreamResource.
      Overrides:
      hashCode in class AbstractResource
      Returns:
      The hash code.
    • toString

      public String toString()
      Print this resource in a human readable manner.
      Overrides:
      toString in class AbstractResource
      Returns:
      The class name and the URL.