Class ResourceUtilities


  • public class ResourceUtilities
    extends java.lang.Object
    Utilities for handling Java resources.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyResourceToFile​(java.lang.String resource, java.io.RandomAccessFile randomAccessFile)
      Copies the resource with the given name to a file on the file system..
      static java.lang.String copyResourceToTempFile​(java.lang.String resource, java.lang.String prefix, java.lang.String postfix)
      Copies the resource with the given name to a temporary file.
      static java.lang.String copyResourceToTempFile​(java.lang.String resource, java.nio.file.Path destinationOrNull, java.lang.String prefix, java.lang.String postfix, boolean cleanUpOldResources)
      Copies the resource with the given name to a temporary file.
      static boolean tryCopyResourceToFile​(java.lang.String resource, java.nio.file.Path filename, java.io.RandomAccessFile randomAccessFile, boolean verbose, java.lang.String logPrefixOrNull)
      Tries to copy the resource with the given name to a file on the file system.
      static java.lang.String tryCopyResourceToTempFile​(java.lang.String resource, java.lang.String prefix, java.lang.String postfix)
      Tries to copy the resource with the given name to a temporary file.
      static java.lang.String tryCopyResourceToTempFile​(java.lang.String resource, java.lang.String prefix, java.lang.String postfix, boolean cleanUpOldResources)
      Tries to copy the resource with the given name to a temporary file.
      static java.lang.String tryCopyResourceToTempFile​(java.lang.String resource, java.lang.String prefix, java.lang.String postfix, boolean cleanUpOldResources, boolean verbose, java.lang.String logPrefixOrNull)
      Tries to copy the resource with the given name to a temporary file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ResourceUtilities

        public ResourceUtilities()
    • Method Detail

      • tryCopyResourceToTempFile

        public static java.lang.String tryCopyResourceToTempFile​(java.lang.String resource,
                                                                 java.lang.String prefix,
                                                                 java.lang.String postfix)
        Tries to copy the resource with the given name to a temporary file.
        Parameters:
        resource - The name of the resource to copy.
        prefix - The prefix to use for the temporary name.
        postfix - The postfix to use for the temporary name.
        Returns:
        The name of the temporary file, or null, if the resource could not be copied.
      • tryCopyResourceToTempFile

        public static java.lang.String tryCopyResourceToTempFile​(java.lang.String resource,
                                                                 java.lang.String prefix,
                                                                 java.lang.String postfix,
                                                                 boolean cleanUpOldResources)
        Tries to copy the resource with the given name to a temporary file. The file will be deleted on program exit.
        Parameters:
        resource - The name of the resource to copy.
        prefix - The prefix to use for the temporary name.
        postfix - The postfix to use for the temporary name.
        cleanUpOldResources - If true, remove old leftover temporary files for this prefix and postfix.
        Returns:
        The name of the temporary file, or null, if the resource could not be copied.
      • tryCopyResourceToTempFile

        public static java.lang.String tryCopyResourceToTempFile​(java.lang.String resource,
                                                                 java.lang.String prefix,
                                                                 java.lang.String postfix,
                                                                 boolean cleanUpOldResources,
                                                                 boolean verbose,
                                                                 java.lang.String logPrefixOrNull)
        Tries to copy the resource with the given name to a temporary file. The file will be deleted on program exit.
        Parameters:
        resource - The name of the resource to copy.
        prefix - The prefix to use for the temporary name.
        postfix - The postfix to use for the temporary name.
        cleanUpOldResources - If true, remove old leftover temporary files for this prefix and postfix.
        verbose - If true, print error to stderr if copying fails.
        logPrefixOrNull - If verbose == true, a prefix for logging failure conditions.
        Returns:
        The name of the temporary file, or null, if the resource could not be copied.
      • copyResourceToTempFile

        public static java.lang.String copyResourceToTempFile​(java.lang.String resource,
                                                              java.lang.String prefix,
                                                              java.lang.String postfix)
                                                       throws IOExceptionUnchecked
        Copies the resource with the given name to a temporary file. The file will be deleted on program exit.
        Parameters:
        resource - The name of the resource to copy.
        prefix - The prefix to use for the temporary name.
        postfix - The postfix to use for the temporary name.
        Returns:
        The name of the temporary file.
        Throws:
        java.lang.IllegalArgumentException - If the resource cannot be found in the class path.
        IOExceptionUnchecked - If an IOException occurs.
      • copyResourceToTempFile

        public static java.lang.String copyResourceToTempFile​(java.lang.String resource,
                                                              java.nio.file.Path destinationOrNull,
                                                              java.lang.String prefix,
                                                              java.lang.String postfix,
                                                              boolean cleanUpOldResources)
                                                       throws IOExceptionUnchecked
        Copies the resource with the given name to a temporary file. The file will be deleted on program exit.
        Parameters:
        resource - The name of the resource to copy.
        prefix - The prefix to use for the temporary name.
        postfix - The postfix to use for the temporary name.
        cleanUpOldResources - If true, remove old leftover temporary files for this prefix and postfix.
        Returns:
        The name of the temporary file.
        Throws:
        java.lang.IllegalArgumentException - If the resource cannot be found in the class path.
        IOExceptionUnchecked - If an IOException occurs.
      • tryCopyResourceToFile

        public static boolean tryCopyResourceToFile​(java.lang.String resource,
                                                    java.nio.file.Path filename,
                                                    java.io.RandomAccessFile randomAccessFile,
                                                    boolean verbose,
                                                    java.lang.String logPrefixOrNull)
                                             throws IOExceptionUnchecked
        Tries to copy the resource with the given name to a file on the file system.

        This method catches all exceptions and returns a status flag.

        Parameters:
        resource - The name of the resource to copy.
        filename - The name of the file to copy the resource content to.
        randomAccessFile - The destination file to copy the resource content to.
        verbose - If true, print error information to stderr if the copying fails.
        logPrefixOrNull - If verbose == true, a prefix for logging failure conditions.
        Returns:
        true if the copying was successfull and false otherwise.
        Throws:
        IOExceptionUnchecked
      • copyResourceToFile

        public static void copyResourceToFile​(java.lang.String resource,
                                              java.io.RandomAccessFile randomAccessFile)
                                       throws IOExceptionUnchecked
        Copies the resource with the given name to a file on the file system..
        Parameters:
        resource - The name of the resource to copy.
        randomAccessFile - The destination file to copy the resource content to.
        Throws:
        java.lang.IllegalArgumentException - If the resource cannot be found in the class path.
        IOExceptionUnchecked - If an IOException occurs.