Class ResourceUtil

java.lang.Object
org.zeroturnaround.javarebel.integration.util.ResourceUtil

public class ResourceUtil extends Object
Helper methods for finding resources.
Author:
Rein Raudjärv
See Also:
  • Constructor Details

    • ResourceUtil

      public ResourceUtil()
  • Method Details

    • findResource

      public static URL findResource(ClassLoader classloader, String name)
      Invokes the findResource method of the given class loader.

      In case of URLClassLoader the underlying public method is invoked directly. Otherwise the protected method of the ClassLoader is invoked through the reflection.

      Parameters:
      classloader - the class loader object.
      name - the name of the resource (parameter passed to the underlying findResource method)
      Returns:
      a URL for the resource, or null if the resource could not be found.
      See Also:
    • findResources

      public static URL[] findResources(ClassLoader classloader, String name)
      Invokes the findResources method of the given class loader.

      In case of URLClassLoader the underlying public method is invoked directly. Otherwise the protected method of the ClassLoader is invoked through the reflection.

      The underlying Enumeration is converted to an array of URLs.

      Parameters:
      classloader - the class loader object.
      name - the resource name. (parameter passed to the underlying findResources method)
      Returns:
      URLs for the resources, or null if no resource could be found.
      See Also:
    • findResources

      public static URL[] findResources(URLClassLoader classloader, String name)
      Invokes the findResources method of the given URL class loader.

      The underlying public method is invoked directly and the Enumeration is converted to an array of URLs.

      Parameters:
      classloader - the class loader object.
      name - the resource name. (parameter passed to the underlying findResources method)
      Returns:
      URLs for the resources, or null if no resource could be found.
      See Also:
    • toURLs

      public static URL[] toURLs(Enumeration<URL> en)
      Converts the Enumeration to an array of URLs.
      Parameters:
      en - an Enumeration of URLs (not null).
      Returns:
      an array of URLs.
      See Also:
    • toEnumeration

      public static Enumeration<URL> toEnumeration(URL[] urls)
      Converts the array of URLs to an Enumeration.
      Parameters:
      urls - an array of URLs (not null).
      Returns:
      an Enumeration of URLs
      See Also:
    • toEnumeration

      public static Enumeration<URL> toEnumeration(File f)
    • concat

      public static URL[] concat(URL[] first, URL[] second)
      Concatenates two arrays of URLs into one.
      Parameters:
      first - first array of URLs (maybe null)
      second - second array of URLs (maybe null)
      Returns:
      array of URLs containing all elements (maybe null).
    • concatClassPath

      public static String concatClassPath(String first, String second)
      Concatenates two class paths.
      Parameters:
      first - first class path.
      second - second class path.
      Returns:
      result containing both class path entries.
    • toClassPath

      public static String toClassPath(URL[] urls)
      Converts an array of URLs into a class path.

      URL will be separated by File.pathSeparators.

      Parameters:
      urls - array of URLs (maybe null)
      Returns:
      class path.
    • getClassResource

      public static org.zeroturnaround.javarebel.Resource getClassResource(org.zeroturnaround.javarebel.ResourceSource source, String className)
      Finds a class resource using a resource finder.

      This is used in BaseClassResourceSource which is the base implementation of ClassResourceSource.

      The class name is converted to a resource name by replacing all dots with slashes and adding .class to the end.

      Parameters:
      source - the resource finder.
      className - the name of the class.
      Returns:
      Resource that corresponds to the given class.
      See Also:
    • asArray

      public static org.zeroturnaround.javarebel.Resource[] asArray(org.zeroturnaround.javarebel.Resource resource)
      Creates a one-element array of Resources.
      Parameters:
      resource - the Resource element.
      Returns:
      a one-element array with the given element or null if the element was null.
    • asResource

      public static org.zeroturnaround.javarebel.Resource asResource(URL url)
      Creates a new Resource object based on the given URL.

      If a file or vfsfile protocol is used a FileResource will be created. Otherwise a new URLResource instance will be returned.

      Parameters:
      url - the resource URL.
      Returns:
      new Resource object based on the given URL.
    • asResources

      public static org.zeroturnaround.javarebel.Resource[] asResources(URL[] urls)
      Creates an array of Resource objects based on the given URL objects.
      Parameters:
      urls - the resource URL objects.
      Returns:
      new Resource objects.
    • asResources

      public static org.zeroturnaround.javarebel.Resource[] asResources(Enumeration<URL> urls)
      Creates an array of Resource objects based on the given URL objects.
      Parameters:
      urls - the resource URL objects.
      Returns:
      new Resource objects.
    • asResources

      public static org.zeroturnaround.javarebel.Resource[] asResources(List<URL> urls)
      Creates an array of Resource objects based on the given URL objects.
      Parameters:
      urls - the resource URL objects.
      Returns:
      new Resource objects.
    • asRawInputStream

      public static InputStream asRawInputStream(URL url)
      Creates a raw input stream from the given URL.
      Parameters:
      url - URL object.
      Returns:
      a raw input stream.
      See Also:
    • asInputStream

      public static InputStream asInputStream(URL url)
      Creates a byte input stream from the given URL.
      Parameters:
      url - URL object.
      Returns:
      a byte input stream.
      See Also:
    • makeURL

      public static URL makeURL(File file) throws MalformedURLException
      Converts a File into a URL.
      Parameters:
      file - File object.
      Returns:
      URL object.
      Throws:
      MalformedURLException
    • makeURL

      public static URL makeURL(File jar, String entry) throws MalformedURLException
      Converts a JAR entry into a URL.
      Parameters:
      jar - a JAR file.
      entry - a JAR entry name.
      Returns:
      URL object.
      Throws:
      MalformedURLException
    • getFileFromPathOrURL

      public static File getFileFromPathOrURL(String path) throws MalformedURLException
      Converts a file path or URL into a File.
      Parameters:
      path - file path or URL.
      Returns:
      File object.
      Throws:
      MalformedURLException
    • getFileFromURL

      public static File getFileFromURL(String url) throws MalformedURLException
      Converts a URL into a File.
      Parameters:
      url - URL.
      Returns:
      File object.
      Throws:
      MalformedURLException
    • getFileFromURL

      public static File getFileFromURL(URL url)
      Converts a URL into a File.
      Parameters:
      url - URL.
      Returns:
      File object.