Class ClassPathUtils


  • public class ClassPathUtils
    extends Object
    • Constructor Detail

      • ClassPathUtils

        public ClassPathUtils()
    • Method Detail

      • toResourceName

        public static String toResourceName​(Path path)
        Translates a file system-specific path to a Java classpath resource name that uses '/' as a separator.
        Parameters:
        path - file system path
        Returns:
        Java classpath resource name
      • consumeAsStreams

        public static void consumeAsStreams​(ClassLoader cl,
                                            String resource,
                                            Consumer<InputStream> consumer)
                                     throws IOException
        Locates all the occurrences of a resource on the classpath of the provided classloader and invokes the consumer providing the input streams for each located resource. The consumer does not have to close the provided input stream. This method was introduced to avoid calling URL.openStream() which in case the resource is found in an archive (such as JAR) locks the containing archive even if the caller properly closes the stream.
        Parameters:
        cl - classloader to load the resources from
        resource - resource path
        consumer - resource input stream consumer
        Throws:
        IOException - in case of an IO failure
      • consumeAsPaths

        public static void consumeAsPaths​(ClassLoader cl,
                                          String resource,
                                          Consumer<Path> consumer)
                                   throws IOException
        Locates specified resources on the classpath and attempts to represent them as local file system paths to be processed by a consumer. If a resource appears to be an actual file or a directory, it is simply passed to the consumer as-is. If a resource is an entry in a JAR, the entry will be resolved as an instance of Path in a FileSystem representing the JAR. If the protocol of the URL representing the resource is neither 'file' nor 'jar', the method will fail with an exception.
        Parameters:
        cl - classloader to load the resources from
        resource - resource path
        consumer - resource path consumer
        Throws:
        IOException - in case of an IO failure
      • consumeAsPath

        public static void consumeAsPath​(URL url,
                                         Consumer<Path> consumer)
        Attempts to represent a resource as a local file system path to be processed by a consumer. If a resource appears to be an actual file or a directory, it is simply passed to the consumer as-is. If a resource is an entry in a JAR, the entry will be resolved as an instance of Path in a FileSystem representing the JAR. If the protocol of the URL representing the resource is neither 'file' nor 'jar', the method will fail with an exception.
        Parameters:
        url - resource url
        consumer - resource path consumer
      • processAsPath

        public static <R> R processAsPath​(URL url,
                                          Function<Path,​R> function)
        Attempts to represent a resource as a local file system path to be processed by a function. If a resource appears to be an actual file or a directory, it is simply passed to the function as-is. If a resource is an entry in a JAR, the entry will be resolved as an instance of Path in a FileSystem representing the JAR. If the protocol of the URL representing the resource is neither 'file' nor 'jar', the method will fail with an exception.
        Parameters:
        url - resource url
        function - resource path function
      • consumeStream

        public static void consumeStream​(URL url,
                                         Consumer<InputStream> consumer)
                                  throws IOException
        Invokes a consumer providing the input streams to read the content of the URL. The consumer does not have to close the provided input stream. This method was introduced to avoid calling URL.openStream() directly, which in case the resource is found in an archive (such as JAR) locks the containing archive even if the caller properly closes the stream.
        Parameters:
        url - URL
        consumer - input stream consumer
        Throws:
        IOException - in case of an IO failure
      • readStream

        public static <R> R readStream​(URL url,
                                       Function<InputStream,​R> function)
                                throws IOException
        Invokes a function providing the input streams to read the content of the URL. The function does not have to close the provided input stream. This method was introduced to avoid calling URL.openStream() directly, which in case the resource is found in an archive (such as JAR) locks the containing archive even if the caller properly closes the stream.
        Parameters:
        url - URL
        function - input stream processing function
        Throws:
        IOException - in case of an IO failure
      • toLocalPath

        public static Path toLocalPath​(URL url)
        Translates a URL to local file system path. In case the URL couldn't be translated to a file system path, an instance of IllegalArgumentException will be thrown.
        Parameters:
        url - URL
        Returns:
        local file system path