Class ResourceUtils

java.lang.Object
imagingbook.core.resource.ResourceUtils

public abstract class ResourceUtils extends Object

This class defines static methods for accessing resources. What makes things somewhat complex is the requirement that we want to retrieve resources located in the file system or contained inside a JAR file.

Here is a typical URI for a JAR-embedded file: "jar:file:/C:/PROJEC~2/parent/IM1D84~1/ImageJ/jars/jarWithResources.jar!/jarWithResouces/resources/clown.jpg"

Version:
2022/11/01
  • Method Details

    • isInsideJar

      public static boolean isInsideJar(Class<?> clazz)
      Determines if the specified class was loaded from a JAR file or a .class file in the file system.
      Parameters:
      clazz - the class
      Returns:
      true if contained in a JAR file, false otherwise
    • getResourceUri

      public static URI getResourceUri(Class<?> clazz, String relPath)
      Finds the URI for a resource relative to a specified class. The resource may be located in the file system or inside a JAR file.
      Parameters:
      clazz - the anchor class
      relPath - the resource path relative to the anchor class
      Returns:
      the URI or null if the resource was not found
    • getResourcePath

      public static Path getResourcePath(Class<?> clazz, String relPath)

      Finds the path to a resource relative to the location of some class. Example: Assume class C was loaded from file someLocation/C.class and there is a subfolder someLocation/resources/ that contains an image file lenna.jpg. Then the complete path to this image is obtained by

       Path path = getResourcePath(C.class, "resources/lenna.jpg");
       
      Parameters:
      clazz - anchor class
      relPath - the path of the resource to be found (relative to the location of the anchor class)
      Returns:
      the path to the specified resource
    • uriToPath

      public static Path uriToPath(URI uri)
      Converts an URI to a Path for locations that are either in the file system or inside a JAR file.
      Parameters:
      uri - the specified location
      Returns:
      the associated path
    • getResourcePaths

      public static Path[] getResourcePaths(URI uri)
    • getResourcePaths

      public static Path[] getResourcePaths(Path path)
      Method to obtain the paths to all files in a directory specified by a Path (non-recursively). This should work in an ordinary file system as well as a (possibly nested) JAR file.
      Parameters:
      path - Path to a directory (may be contained in a JAR file)
      Returns:
      a possibly empty sequence of paths
    • getResourcePaths

      public static Path[] getResourcePaths(Class<?> clazz, String relPath)
      Use this method to obtain the paths to all files in a directory located relative to the specified class (non-recursively). This should work in an ordinary file system as well as a (possibly nested) JAR file.
      Parameters:
      clazz - class whose source location defines the root
      relPath - path relative to the root
      Returns:
      a possibly empty array of paths
    • getResourceFileNames

      public static String[] getResourceFileNames(Class<?> clazz, String relDir)
      Use this method to obtain the names of all files in a directory located relative to the specified class (non-recursively). This should work in an ordinary file system as well as a (possibly nested) JAR file.
      Parameters:
      clazz - class whose source location specifies the root
      relDir - directory relative to the root
      Returns:
      a possibly empty array of file names