Class MemoryResources

All Implemented Interfaces:
ResourceFinder, ResourceWriter

public class MemoryResources extends AbstractResourceFinder implements ResourceWriter
This class offers ResourceFinder and ResourceWriter capabilities for resources that are stored in a memory.
Since:
1.0
See Also:
  • Constructor Details

    • MemoryResources

      public MemoryResources()
      Creates a new instance.
      Since:
      1.0
  • Method Details

    • addResource

      public void addResource(String name, String content)
      Description copied from interface: ResourceWriter
      Adds a resource with the provided name and content.
      Specified by:
      addResource in interface ResourceWriter
      Parameters:
      name - the name of the resource
      content - the content of the resource
    • updateResource

      public boolean updateResource(String name, String content)
      Description copied from interface: ResourceWriter
      Updates the content of the resource with the provided name.
      Specified by:
      updateResource in interface ResourceWriter
      Parameters:
      name - the name of the resource
      content - the content of the resource
      Returns:
      true when the resource was updated; false otherwise
    • removeResource

      public boolean removeResource(String name)
      Description copied from interface: ResourceWriter
      Removes the resource with the provided name.
      Specified by:
      removeResource in interface ResourceWriter
      Parameters:
      name - the name of the resource
      Returns:
      true when the resource was removed; false otherwise
    • getResource

      public URL getResource(String name)
      Description copied from interface: ResourceFinder
      Retrieves the resource that corresponds to the provided name.

      This method never throws an exception, but returns null in case of an exception.

      Specified by:
      getResource in interface ResourceFinder
      Parameters:
      name - the name of the resource to retrieve
      Returns:
      the URL object that corresponds to the provided name; or

      null if the resource couldn't be found or if an error occurred.

    • useStream

      public <ResultType> ResultType useStream(URL resource, InputStreamUser<ResultType,?> user) throws ResourceFinderErrorException, InnerClassException
      Description copied from interface: ResourceFinder
      Returns a stream that can be used to read the contents of the provided resource.
      Specified by:
      useStream in interface ResourceFinder
      Parameters:
      resource - the resource to retrieve
      user - an instance of InputStreamUser that contains the logic that will be executed with this stream
      Returns:
      the return value from the useInputStream method of the provided InputStreamUser instance
      Throws:
      ResourceFinderErrorException - when an error occurred during the creation or opening of the stream.
      InnerClassException - when errors occurs inside the InputStreamUser
      See Also:
    • getContent

      public String getContent(URL resource, String encoding)
      Description copied from interface: ResourceFinder
      Retrieves the complete content of the provided resource.
      Specified by:
      getContent in interface ResourceFinder
      Parameters:
      resource - the resource to retrieve the content from
      encoding - the encoding that should be used to read the content
      Returns:
      a String object that contains the complete content of the resource with the provided name; or

      null if the resource couldn't be found.

      See Also:
    • getModificationTime

      public long getModificationTime(URL resource)
      Description copied from interface: ResourceFinder
      Retrieves the modification time of the provided resource.
      Specified by:
      getModificationTime in interface ResourceFinder
      Parameters:
      resource - the resource to retrieve the modification time from
      Returns:
      a positive long with the modification time in milliseconds; or

      -1 if the resource couldn't be found.

      See Also: