public class ResourceUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
FILE_URL_PREFIX
URL prefix for loading from the file system: "file:".
|
static String |
JAR_URL_SEPARATOR
The JAR URL separator.
|
static Pattern[] |
patterns
Resources are resolved against the platform specific path and must therefore obey the specific File.separator.
|
static String |
REGEX_FILE_SEPARATOR
Regex file separator.
|
static String |
URL_PROTOCOL_CODE_SOURCE
URL protocol for an entry from an OC4J jar file: "code-source".
|
static String |
URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file".
|
static String |
URL_PROTOCOL_JAR
URL protocol for an entry from a jar file: "jar".
|
static String |
URL_PROTOCOL_VFS
URL protocol for a JBoss VFS resource: "vfs".
|
static String |
URL_PROTOCOL_WSJAR
URL protocol for an entry from a WebSphere jar file: "wsjar".
|
static String |
URL_PROTOCOL_ZIP
URL protocol for an entry from a zip file: "zip".
|
Constructor and Description |
---|
ResourceUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
applyRelativePath(String path,
String relativePath)
Apply the given relative path to the given path,
assuming standard Java folder separation (i.e.
|
static String |
classPackageAsResourcePath(Class<?> clazz)
Given an input class object, return a string which consists of the
class's package name as a pathname, i.e., all dots ('.') are replaced by
slashes ('/').
|
static String |
cleanPath(String path)
Normalize the path by suppressing sequences like "path/.." and
inner simple dots.
|
static URL |
extractJarFileURL(URL jarUrl)
Extract the URL for the actual jar file from the given URL
(which may point to a resource in a jar file or to a jar file itself).
|
static String |
getClassName(Resource resource)
Gets the class name of the specified Micronaut resource.
|
static String |
getClassName(String path)
Returns the class name for a resource.
|
static File |
getFile(URI resourceUri,
String description)
Resolve the given resource URI to a
java.io.File ,
i.e. |
static File |
getFile(URL resourceUrl,
String description)
Resolve the given resource URL to a
java.io.File ,
i.e. |
static String |
getFilename(String path)
Extract the filename from the given path,
e.g.
|
static boolean |
isFileURL(URL url)
Determine whether the given URL points to a resource in the file system,
that is, has protocol "file" or "vfs".
|
static boolean |
isJarURL(URL url)
Determine whether the given URL points to a resource in a jar file,
that is, has protocol "jar", "zip", "wsjar" or "code-source".
|
static URI |
toURI(String location)
Create a URI instance for the given location String,
replacing spaces with "%20" quotes first.
|
static URI |
toURI(URL url)
Create a URI instance for the given URL,
replacing spaces with "%20" quotes first.
|
static void |
useCachesIfNecessary(URLConnection con) |
public static final String JAR_URL_SEPARATOR
public static final String FILE_URL_PREFIX
public static final String URL_PROTOCOL_FILE
public static final String URL_PROTOCOL_JAR
public static final String URL_PROTOCOL_ZIP
public static final String URL_PROTOCOL_VFS
public static final String URL_PROTOCOL_WSJAR
public static final String URL_PROTOCOL_CODE_SOURCE
public static final String REGEX_FILE_SEPARATOR
public static final Pattern[] patterns
public static String getFilename(String path)
path
- the file path (may be null
)null
if nonepublic static String classPackageAsResourcePath(Class<?> clazz)
ClassLoader.getResource()
. For it to be fed to
Class.getResource
instead, a leading slash would also have
to be prepended to the returned value.clazz
- the input class. A null
value or the default
(empty) package will result in an empty string ("") being returned.ClassLoader.getResource(java.lang.String)
,
Class.getResource(java.lang.String)
public static void useCachesIfNecessary(URLConnection con)
con
- The URL connectionpublic static String getClassName(Resource resource)
resource
- The Spring Resourcepublic static String getClassName(String path)
path
- The path to checkpublic static File getFile(URL resourceUrl, String description) throws FileNotFoundException
java.io.File
,
i.e. to a file in the file system.resourceUrl
- the resource URL to resolvedescription
- a description of the original resource that
the URL was created for (for example, a class path location)FileNotFoundException
- if the URL cannot be resolved to
a file in the file systempublic static boolean isJarURL(URL url)
"zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere, respectively, but can be treated like jar files. The same applies to "code-source" URLs on Oracle OC4J, provided that the path contains a jar separator.
url
- the URL to checkpublic static File getFile(URI resourceUri, String description) throws FileNotFoundException
java.io.File
,
i.e. to a file in the file system.resourceUri
- the resource URI to resolvedescription
- a description of the original resource that
the URI was created for (for example, a class path location)FileNotFoundException
- if the URL cannot be resolved to
a file in the file systempublic static URI toURI(URL url) throws URISyntaxException
Furthermore, this method works on JDK 1.4 as well,
in contrast to the URL.toURI()
method.
url
- the URL to convert into a URI instanceURISyntaxException
- if the URL wasn't a valid URIURL.toURI()
public static boolean isFileURL(URL url)
url
- the URL to checkpublic static String applyRelativePath(String path, String relativePath)
path
- the path to start from (usually a full file path)relativePath
- the relative path to apply
(relative to the full file path above)public static String cleanPath(String path)
The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
path
- the original pathpublic static URL extractJarFileURL(URL jarUrl) throws MalformedURLException
jarUrl
- the original URLMalformedURLException
- if no valid jar file URL could be extractedpublic static URI toURI(String location) throws URISyntaxException
location
- the location String to convert into a URI instanceURISyntaxException
- if the location wasn't a valid URI