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:
Resource,ResourceSource,ClassLoader,URLClassLoader,URL,InputStream
-
-
Constructor Summary
Constructors Constructor Description ResourceUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.zeroturnaround.javarebel.Resource[]asArray(org.zeroturnaround.javarebel.Resource resource)Creates a one-element array ofResources.static InputStreamasInputStream(URL url)Creates a byte input stream from the given URL.static InputStreamasRawInputStream(URL url)Creates a raw input stream from the given URL.static org.zeroturnaround.javarebel.ResourceasResource(URL url)Creates a newResourceobject based on the givenURL.static org.zeroturnaround.javarebel.Resource[]asResources(URL[] urls)Creates an array ofResourceobjects based on the givenURLobjects.static org.zeroturnaround.javarebel.Resource[]asResources(Enumeration<URL> urls)Creates an array ofResourceobjects based on the givenURLobjects.static org.zeroturnaround.javarebel.Resource[]asResources(List<URL> urls)Creates an array ofResourceobjects based on the givenURLobjects.static URL[]concat(URL[] first, URL[] second)Concatenates two arrays ofURLs into one.static StringconcatClassPath(String first, String second)Concatenates two class paths.static URLfindResource(ClassLoader classloader, String name)Invokes thefindResourcemethod of the given class loader.static URL[]findResources(ClassLoader classloader, String name)Invokes thefindResourcesmethod of the given class loader.static URL[]findResources(URLClassLoader classloader, String name)Invokes thefindResourcesmethod of the given URL class loader.static org.zeroturnaround.javarebel.ResourcegetClassResource(org.zeroturnaround.javarebel.ResourceSource source, String className)Finds a class resource using a resource finder.static FilegetFileFromPathOrURL(String path)Converts a file path or URL into aFile.static FilegetFileFromURL(String url)Converts a URL into aFile.static FilegetFileFromURL(URL url)Converts a URL into aFile.static URLmakeURL(File file)static URLmakeURL(File jar, String entry)Converts a JAR entry into aURL.static StringtoClassPath(URL[] urls)Converts an array ofURLs into a class path.static Enumeration<URL>toEnumeration(File f)static Enumeration<URL>toEnumeration(URL[] urls)Converts the array ofURLs to anEnumeration.static URL[]toURLs(Enumeration<URL> en)Converts theEnumerationto an array ofURLs.
-
-
-
Method Detail
-
findResource
public static URL findResource(ClassLoader classloader, String name)
Invokes thefindResourcemethod of the given class loader.In case of
URLClassLoaderthe underlying public method is invoked directly. Otherwise the protected method of theClassLoaderis invoked through the reflection.- Parameters:
classloader- the class loader object.name- the name of the resource (parameter passed to the underlyingfindResourcemethod)- Returns:
- a
URLfor the resource, ornullif the resource could not be found. - See Also:
URLClassLoader.findResource(String)
-
findResources
public static URL[] findResources(ClassLoader classloader, String name)
Invokes thefindResourcesmethod of the given class loader.In case of
URLClassLoaderthe underlying public method is invoked directly. Otherwise the protected method of theClassLoaderis invoked through the reflection.The underlying
Enumerationis converted to an array ofURLs.- Parameters:
classloader- the class loader object.name- the resource name. (parameter passed to the underlyingfindResourcesmethod)- Returns:
URLs for the resources, ornullif no resource could be found.- See Also:
URLClassLoader.findResources(String),findResources(URLClassLoader, String),findResource(ClassLoader, String),toURLs(Enumeration)
-
findResources
public static URL[] findResources(URLClassLoader classloader, String name)
Invokes thefindResourcesmethod of the given URL class loader.The underlying public method is invoked directly and the
Enumerationis converted to an array ofURLs.- Parameters:
classloader- the class loader object.name- the resource name. (parameter passed to the underlyingfindResourcesmethod)- Returns:
URLs for the resources, ornullif no resource could be found.- See Also:
URLClassLoader.findResources(String),findResources(ClassLoader, String),findResource(ClassLoader, String),toURLs(Enumeration)
-
toURLs
public static URL[] toURLs(Enumeration<URL> en)
Converts theEnumerationto an array ofURLs.- Parameters:
en- anEnumerationofURLs (notnull).- Returns:
- an array of
URLs. - See Also:
toEnumeration(URL[])
-
toEnumeration
public static Enumeration<URL> toEnumeration(URL[] urls)
Converts the array ofURLs to anEnumeration.- Parameters:
urls- an array ofURLs (notnull).- Returns:
- an
EnumerationofURLs - See Also:
toURLs(Enumeration)
-
toEnumeration
public static Enumeration<URL> toEnumeration(File f)
-
concat
public static URL[] concat(URL[] first, URL[] second)
Concatenates two arrays ofURLs into one.- Parameters:
first- first array ofURLs (maybenull)second- second array ofURLs (maybenull)- Returns:
- array of
URLs containing all elements (maybenull).
-
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 ofURLs into a class path.URLwill be separated byFile.pathSeparators.- Parameters:
urls- array ofURLs (maybenull)- 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
BaseClassResourceSourcewhich is the base implementation ofClassResourceSource.The class name is converted to a resource name by replacing all dots with slashes and adding
.classto the end.- Parameters:
source- the resource finder.className- the name of the class.- Returns:
Resourcethat corresponds to the given class.- See Also:
ResourceSource,ClassResourceSource,BaseClassResourceSource
-
asArray
public static org.zeroturnaround.javarebel.Resource[] asArray(org.zeroturnaround.javarebel.Resource resource)
Creates a one-element array ofResources.- Parameters:
resource- theResourceelement.- Returns:
- a one-element array with the given element
or
nullif the element wasnull.
-
asResource
public static org.zeroturnaround.javarebel.Resource asResource(URL url)
Creates a newResourceobject based on the givenURL.If a
fileorvfsfileprotocol is used aFileResourcewill be created. Otherwise a newURLResourceinstance will be returned.- Parameters:
url- the resourceURL.- Returns:
- new
Resourceobject based on the givenURL.
-
asResources
public static org.zeroturnaround.javarebel.Resource[] asResources(URL[] urls)
Creates an array ofResourceobjects based on the givenURLobjects.- Parameters:
urls- the resourceURLobjects.- Returns:
- new
Resourceobjects.
-
asResources
public static org.zeroturnaround.javarebel.Resource[] asResources(Enumeration<URL> urls)
Creates an array ofResourceobjects based on the givenURLobjects.- Parameters:
urls- the resourceURLobjects.- Returns:
- new
Resourceobjects.
-
asResources
public static org.zeroturnaround.javarebel.Resource[] asResources(List<URL> urls)
Creates an array ofResourceobjects based on the givenURLobjects.- Parameters:
urls- the resourceURLobjects.- Returns:
- new
Resourceobjects.
-
asRawInputStream
public static InputStream asRawInputStream(URL url)
Creates a raw input stream from the given URL.- Parameters:
url-URLobject.- Returns:
- a raw input stream.
- See Also:
asInputStream(URL)
-
asInputStream
public static InputStream asInputStream(URL url)
Creates a byte input stream from the given URL.- Parameters:
url-URLobject.- Returns:
- a byte input stream.
- See Also:
asRawInputStream(URL)
-
makeURL
public static URL makeURL(File file) throws MalformedURLException
- Parameters:
file-Fileobject.- Returns:
URLobject.- Throws:
MalformedURLException
-
makeURL
public static URL makeURL(File jar, String entry) throws MalformedURLException
Converts a JAR entry into aURL.- Parameters:
jar- a JAR file.entry- a JAR entry name.- Returns:
URLobject.- Throws:
MalformedURLException
-
getFileFromPathOrURL
public static File getFileFromPathOrURL(String path) throws MalformedURLException
Converts a file path or URL into aFile.- Parameters:
path- file path or URL.- Returns:
Fileobject.- Throws:
MalformedURLException
-
getFileFromURL
public static File getFileFromURL(String url) throws MalformedURLException
Converts a URL into aFile.- Parameters:
url- URL.- Returns:
Fileobject.- Throws:
MalformedURLException
-
-