Class FileUtility



  • public final class FileUtility
    extends Object
    The FileUtility provides Various file related utility functionality.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static URL createNestedJarFileUrl​(URL aJarUrl, File aToDir)
      Copies a file residing in a nested JAR to the given destination folder.
      static URL getNestedJarFileUrl​(URL aJarUrl, File aToDir)
      Determines whether an according destination file already exists for the file residing in a nested JAR.
      static InputStream getResourceAsStream​(Class<?> aClass, String aPath)
      Provides an InputStream for a resource found at the given path relative to the given class file (which might be inside a Java archive such as a JAR file or a WAR file).
      static String[] toJarHierarchy​(URL aJarUrl)
      Takes an URL pointing into a (nested) JAR resources and returns a list of JAR archive names (including the ".jar" suffix) in the order of their nesting, the first JAR archive being the outermost (parent) archive and the last JAR archive being the innermost archive.
      static URL toNestedJarFileUrl​(URL aJarUrl, File aToDir)
      Convenience method testing whether the given JAR file resource already exists in the expected folder layout .Returns its URL in case it already exists else it is being created and then the URL is returned.
      static URL toParentJarUrl​()
      Determines the parent JAR file's URL for your running application (not including the "!"
      static String toTempFileName​()
      Generates a file name for a temporary file consisting if the current time in milliseconds and a portion of random character to avoid name clashes: "temp-012345678901234567890123456789-abcdefgh".
    • Method Detail

      • getResourceAsStream

        public static InputStream getResourceAsStream​(Class<?> aClass,
                                                      String aPath)
        Provides an InputStream for a resource found at the given path relative to the given class file (which might be inside a Java archive such as a JAR file or a WAR file).
        Parameters:
        aClass - The class relative to which to look for the resource.
        aPath - The path which to use relative to the given class.
        Returns:
        The InputStream for the requested resource.
      • toTempFileName

        public static String toTempFileName​()
        Generates a file name for a temporary file consisting if the current time in milliseconds and a portion of random character to avoid name clashes: "temp-012345678901234567890123456789-abcdefgh".
        Returns:
        A temp file name.
      • createNestedJarFileUrl

        public static URL createNestedJarFileUrl​(URL aJarUrl,
                                                 File aToDir)
                                          throws IOException
        Copies a file residing in a nested JAR to the given destination folder. The provided folder represents the base folder, actually an unambiguous folder layout is created within that base folder to prevent side effects with files of the same name residing in different nested JAR archives.
        Parameters:
        aJarUrl - The URL which points into a (nested) JAR's resource.
        aToDir - The base directory into which the (nested) JAR's resource will be extracted.
        Returns:
        The file URL (protocol "file:") for the extracted resource addressed in the (nested) JAR or null in case we do not have a JAR URL. In case we already have a file URL then the URL is returned untouched ignoring any passed destination folder (the to-dir argument).
        Throws:
        IOException - in case processing the extracted file caused problems, e.g. the target folder is not a directory, it is not writable, the JAR archive caused problems (currpted) and so on.
        See Also:
        "https://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/util/file/JarUtils.java.html"
      • getNestedJarFileUrl

        public static URL getNestedJarFileUrl​(URL aJarUrl,
                                              File aToDir)
                                       throws IOException
        Determines whether an according destination file already exists for the file residing in a nested JAR. The provided folder represents the base folder; actually an unambiguous folder layout as of toJarHierarchy(URL) is assumed within that base folder (as created by the createNestedJarFileUrl(URL, File)) for preventing side effects with files of the same name residing in different nested JAR archives.
        Parameters:
        aJarUrl - The URL which points into a (nested) JAR's resource.
        aToDir - The base directory in which the (nested) JAR's resource is being expected.
        Returns:
        The file URL (protocol "file:") for the identified (existing) resource addressed in the (nested) JAR or null in case there is no such file in the expected folder layout. In case we already have a file URL then the URL is returned untouched ignoring any passed destination folder (the to-dir argument).
        Throws:
        IOException - Thrown in case no details on the referenced entry can be retrieved from the addressed JAR archive.
        See Also:
        "https://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/util/file/JarUtils.java.html"
      • toNestedJarFileUrl

        public static URL toNestedJarFileUrl​(URL aJarUrl,
                                             File aToDir)
                                      throws IOException
        Convenience method testing whether the given JAR file resource already exists in the expected folder layout .Returns its URL in case it already exists else it is being created and then the URL is returned.
        Parameters:
        aJarUrl - The URL which points into a (nested) JAR's resource.
        aToDir - The base directory in which the (nested) JAR's resource is being expected (created).
        Returns:
        The parrent's JAR file URL or null if the application does not seem to reside in a JAR. The file URL (protocol "file:") for the existing (created) resource addressed in the (nested) JAR.
        Throws:
        IOException - Thrown in case no details on the referenced entry can be retrieved from the addressed JAR archive.
        See Also:
        getNestedJarFileUrl(URL, File), createNestedJarFileUrl(URL, File)
      • toParentJarUrl

        public static URL toParentJarUrl​()
        Determines the parent JAR file's URL for your running application (not including the "!" which is required to address a file within that JAR).
        Returns:
        The parrent's JAR file URL or null if the application does not seem to reside in a JAR.
      • toJarHierarchy

        public static String[] toJarHierarchy​(URL aJarUrl)
        Takes an URL pointing into a (nested) JAR resources and returns a list of JAR archive names (including the ".jar" suffix) in the order of their nesting, the first JAR archive being the outermost (parent) archive and the last JAR archive being the innermost archive. A JAR "path" of an URL might look as follows: "jar:file:/home/steiner/Workspaces/com.fightclub/fightclub-app/target/fightclub-app-0.0.1-SNAPSHOT.jar!/webapp/home.xhtml" "jar:file:/home/steiner/Workspaces/com.fightclub/fightclub-app/target/fightclub-app-0.0.1-SNAPSHOT.jar!/lib/fightclub-adapter-web-0.0.1-SNAPSHOT.jar!/webapp/home.xhtml"
        Parameters:
        aJarUrl - The URL for which to get the JAR file hierarchy array.
        Returns:
        The array with the JAR archive hierarchy or null if not being a JAR URL.