public class Resource extends java.lang.Object implements java.lang.Comparable<Resource>
contextClass
and
a path
. The resource will be loaded using the same class loader that
was used to load the contextClass
. The path consists of the class'
package name plus the path
. A resource object is always guaranteed
to exist meaning that instantiation of a non-existent resource will fail.Modifier and Type | Method and Description |
---|---|
static java.util.Optional<Resource> |
asOptional(java.lang.Class<?> contextClass,
java.lang.String path)
Creates an instance of a resource.
|
int |
compareTo(Resource other) |
java.io.File |
copyTo(java.io.File file)
Copies the resource to the given file.
|
boolean |
equals(java.lang.Object o) |
static boolean |
exists(java.lang.Class<?> contextClass,
java.lang.String path)
Returns true is the resource exists.
|
java.lang.String |
getAbsolutePath()
Returns the resource's absolute path including the
contextClass '
package name. |
static java.lang.String |
getAbsolutePath(java.lang.Class<?> contextClass,
java.lang.String path)
Returns the resource's absolute path including the
contextClass '
package name. |
byte[] |
getAsByteArray()
Returns the resource as a raw byte array.
|
java.io.InputStream |
getAsStream()
Get resource as stream.
|
java.io.File |
getAsTmpFile()
Creates a temporary copy of the resource in the systems default temp file
location.
|
java.lang.String |
getBaseName()
Returns the resource's base name without extension.
|
java.lang.String |
getContent()
Get resource as string (assuming UTF-8 encoding) with normalized line
separators.
|
java.lang.String |
getContentAsRawString()
Get resource as string (assuming UTF-8 encoding) without normalized line
separators.
|
java.lang.Class<?> |
getContextClass()
Returns the resource's context class.
|
java.lang.String |
getExtension()
Returns the resource's extension.
|
java.util.List<java.lang.String> |
getLines()
Returns the content of the resource split into lines.
|
java.lang.String |
getName()
Returns the name of the resource including the extension.
|
java.lang.String |
getParentPath()
Returns the resource's parent path.
|
java.lang.String |
getPath()
Returns the resource's path relative to the
contextClass including
the resource's name. |
java.lang.String |
getPathSegmentAt(int index)
Returns only a single path segment of the resource's path.
|
java.net.URL |
getUrl()
Returns the resource's url.
|
boolean |
hasExtension(java.lang.String... extensions)
Returns true is the resource's extension matches one of the given extensions.
|
int |
hashCode() |
long |
lastModified()
Returns the timestamp in milliseconds of the last modification in case the
underlying resource url is a file.
|
static Resource |
of(java.lang.Class<?> contextClass,
java.lang.String path)
Creates an instance of a resource.
|
static Resource |
of(java.lang.Class<?> contextClass,
java.lang.String path,
java.net.URL url)
Creates an instance of a resource.
|
java.lang.String |
toString() |
void |
unzipTo(java.io.File tempDir)
Unzips the resource into into the given folder under a folder with the same
name as the resource's base name.
|
public static Resource of(java.lang.Class<?> contextClass, java.lang.String path)
exists(Class, String)
before
creating an instance in case it might be missing.public static java.util.Optional<Resource> asOptional(java.lang.Class<?> contextClass, java.lang.String path)
public static Resource of(java.lang.Class<?> contextClass, java.lang.String path, java.net.URL url)
exists(Class, String)
before
creating an instance in case it might be missing.public static java.lang.String getAbsolutePath(java.lang.Class<?> contextClass, java.lang.String path)
contextClass
'
package name.public static boolean exists(java.lang.Class<?> contextClass, java.lang.String path)
public java.lang.String getName()
public java.lang.String getBaseName()
public java.lang.String getExtension()
public boolean hasExtension(java.lang.String... extensions)
getExtension()
this method supports also extensions
that contain dots.public java.lang.Class<?> getContextClass()
public java.lang.String getPath()
contextClass
including
the resource's name.public java.net.URL getUrl()
public java.lang.String getParentPath()
getPath()
without
the resource's name.public java.lang.String getAbsolutePath()
contextClass
'
package name.public java.lang.String getPathSegmentAt(int index)
public java.io.InputStream getAsStream()
public byte[] getAsByteArray()
public java.lang.String getContentAsRawString()
public java.lang.String getContent()
public java.util.List<java.lang.String> getLines()
public java.io.File getAsTmpFile() throws java.io.IOException
java.io.IOException
public java.io.File copyTo(java.io.File file) throws java.io.IOException
java.io.IOException
public void unzipTo(java.io.File tempDir) throws java.io.IOException
java.io.IOException
public int compareTo(Resource other)
compareTo
in interface java.lang.Comparable<Resource>
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public long lastModified()
ResourceUtils.getFileBackedResource(Resource)
.