Class Resources


  • public final class Resources
    extends Object
    Convenience methods for constructing Resources
    Author:
    David B. Bracewell
    • Constructor Detail

      • Resources

        public Resources()
    • Method Detail

      • findAllClasspathResources

        public static Iterator<Resource> findAllClasspathResources​(String pattern)
        Finds all resources with the given pattern across loaded ClassLoaders
        Parameters:
        pattern - the file pattern
        Returns:
        Iterator of resources
      • from

        public static Resource from​(String resource)
        Constructs a resource from a string representation. Defaults to a file based resource if no schema is present.
        Parameters:
        resource - The string representation of the resource
        Returns:
        A resource representing the string representation
      • fromClasspath

        public static Resource fromClasspath​(String resource)

        Creases a new ClasspathResource.

        Parameters:
        resource - The classpath making up the resource
        Returns:
        A new Resource associated with the classpath
      • fromFile

        public static Resource fromFile​(File resource)

        Creases a new FileResource.

        Parameters:
        resource - The file making up the resource
        Returns:
        A new Resource associated with the file
      • fromFile

        public static Resource fromFile​(String resource)

        Creases a new FileResource.

        Parameters:
        resource - The file making up the resource
        Returns:
        A new Resource associated with the file
      • fromInputStream

        public static Resource fromInputStream​(InputStream inputStream)
        From input stream resource.
        Parameters:
        inputStream - The input stream to wrap
        Returns:
        Resource that can read from given input stream
      • fromOutputStream

        public static Resource fromOutputStream​(OutputStream outputStream)
        From output stream resource.
        Parameters:
        outputStream - The output stream to wrap
        Returns:
        Resource that can write to given output stream
      • fromReader

        public static Resource fromReader​(Reader reader)
        Creates a new Resource that wraps the given reader
        Parameters:
        reader - The reader to wrap
        Returns:
        Resource that can read from given reader
      • fromStdin

        public static Resource fromStdin()
        From stdin resource.
        Returns:
        Resource that can read from standard in
      • fromStdout

        public static Resource fromStdout()
        From stdout resource.
        Returns:
        Resource that can output to standard out
      • fromString

        public static Resource fromString​(String stringResource)
        Creates a StringResource from the given string.
        Parameters:
        stringResource - the string resource
        Returns:
        the resource
      • fromString

        public static Resource fromString()
        Creates a StringResource that is empty.
        Returns:
        the resource
      • fromURI

        public static URIResource fromURI​(URI resource)

        Creates a new URIResource.

        Parameters:
        resource - The uri to wrap.
        Returns:
        A new Resource wrapping a uri.
      • fromURI

        public static URIResource fromURI​(String resource)

        Creates a new URIResource.

        Parameters:
        resource - The uri to wrap.
        Returns:
        A new Resource wrapping a uri.
      • fromUrl

        public static URLResource fromUrl​(URL resource)

        Creates a new URLResource.

        Parameters:
        resource - The url to wrap.
        Returns:
        A new Resource wrapping a url.
      • fromWriter

        public static Resource fromWriter​(Writer writer)
        Creates a new Resource that wraps the given writer
        Parameters:
        writer - the writer wrap
        Returns:
        the resource
      • getJar

        public static Resource getJar​(@NonNull
                                      @NonNull Class<?> clazz)
        Gets the jar file that a class is stored in.
        Parameters:
        clazz - The class whose associated jar file is descried.
        Returns:
        The Resource (jar file) for the class
      • temporaryDirectory

        public static Resource temporaryDirectory()
        Creates a new Resource that points to a temporary directory.
        Returns:
        A resource which is a temporary directory on disk
      • temporaryFile

        public static Resource temporaryFile()
        Creates a new Resource that points to a temporary file.
        Returns:
        A resource which is a temporary file on disk
      • temporaryFile

        public static Resource temporaryFile​(String name,
                                             String extension)
                                      throws IOException
        Creates a resource wrapping a temporary file
        Parameters:
        name - The file name
        extension - The file extension
        Returns:
        The resource representing the temporary file
        Throws:
        IOException - the io exception