public abstract class Resource extends Object implements Closeable, Comparable<Resource>
| Constructor and Description |
|---|
Resource(io.github.classgraph.ClasspathElement classpathElement,
long length)
Constructor.
|
Resource(io.github.classgraph.ClasspathElement classpathElement,
long length,
long lastModified,
Set<PosixFilePermission> posixFilePermissions)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close the underlying InputStream, or release/unmap the underlying ByteBuffer.
|
int |
compareTo(Resource o) |
boolean |
equals(Object obj) |
File |
getClasspathElementFile()
Get the classpath element
File. |
URI |
getClasspathElementURI()
Get the
URI of the classpath element or module that this resource was obtained from. |
URL |
getClasspathElementURL()
Get the
URL of the classpath element or module that this resource was obtained from. |
String |
getContentAsString()
Convenience method to get the content of this
Resource as a String. |
long |
getLastModified()
Get the last modified time for the resource, in milliseconds since the epoch.
|
long |
getLength()
Get the length of the resource.
|
ModuleRef |
getModuleRef()
|
abstract String |
getPath()
Get the path of this classpath resource relative to the package root.
|
abstract String |
getPathRelativeToClasspathElement()
Get the full path of this classpath resource relative to the root of the classpath element.
|
Set<PosixFilePermission> |
getPosixFilePermissions()
Get the POSIX file permissions for the resource.
|
URI |
getURI()
Get the
URI representing the resource's location. |
URL |
getURL()
Get the
URL representing the resource's location. |
int |
hashCode() |
abstract byte[] |
load()
Load a classpath resource and return its content as a byte array.
|
abstract InputStream |
open()
Open an
InputStream for a classpath resource. |
abstract ByteBuffer |
read()
Open a
ByteBuffer for a classpath resource. |
String |
toString()
Get a string representation of the resource's location (as a URL string).
|
public Resource(io.github.classgraph.ClasspathElement classpathElement,
long length)
classpathElement - the classpath element this resource was obtained from.length - the length the length of the resource.public Resource(io.github.classgraph.ClasspathElement classpathElement,
long length,
long lastModified,
Set<PosixFilePermission> posixFilePermissions)
classpathElement - the classpath element this resource was obtained from.length - the length the length of the resource.lastModified - The last modified date, in milliseconds since the epoch, or 0 if unknown.posixFilePermissions - The POSIX file permissions, or null if unknown.public URI getURI()
URI representing the resource's location.URI representing the resource's location.IllegalArgumentException - the resource was obtained from a module and the module's location URI is null.public URL getURL()
URL representing the resource's location. Use getURI() instead if the resource may
have come from a system module, or if this is a jlink'd runtime image, since "jrt:" URI schemes used by
system modules and jlink'd runtime images are not suppored by URL, and this will cause
IllegalArgumentException to be thrown.URL representing the resource's location.IllegalArgumentException - if the resource was obtained from a system module or jlink'd runtime image with a "jrt:" location
URI, or the resource was obtained from a module and the module's location URI is nullpublic URI getClasspathElementURI()
URI of the classpath element or module that this resource was obtained from.URL of the classpath element or module that this resource was found within.IllegalArgumentException - if the classpath element does not have a valid URI (e.g. for modules whose location URI is null).public URL getClasspathElementURL()
URL of the classpath element or module that this resource was obtained from. Use
getClasspathElementURI() instead if the resource may have come from a system module, or if this is a
jlink'd runtime image, since "jrt:" URI schemes used by system modules and jlink'd runtime images are not
suppored by URL, and this will cause IllegalArgumentException to be thrown.URL of the classpath element or module that this resource was found within.IllegalArgumentException - if the resource was obtained from a system module or jlink'd runtime image with a "jrt:" location
URI, or the resource was obtained from a module and the module's location URI is null.public File getClasspathElementFile()
File.File for the classpath element package root dir or jar that this Resource was
found within, or null if this Resource was found in a module backed by a "jrt:" URI, or a
module with an unknown location. May also return null if the classpath element was an http/https URL,
and the jar was downloaded directly to RAM, rather than to a temp file on disk (e.g. if the temp dir
is not writeable).public ModuleRef getModuleRef()
public String getContentAsString() throws IOException
Resource as a String. Assumes UTF8 encoding. (Calls
close() after completion.)Resource as a String.IOException - If an I/O exception occurred.public abstract String getPath()
"BOOT-INF/classes/com/xyz/resource.xml" and a package root of "BOOT-INF/classes/",
returns "com/xyz/resource.xml". Also drops version prefixes for multi-version jars, for
example for a resource path of "META-INF/versions/11/com/xyz/resource.xml" while running on
JDK 9+, returns "com/xyz/resource.xml".public abstract String getPathRelativeToClasspathElement()
"BOOT-INF/classes/com/xyz/resource.xml" or
"META-INF/versions/11/com/xyz/resource.xml", not "com/xyz/resource.xml".public abstract InputStream open() throws IOException
InputStream for a classpath resource. Make sure you call close() when you
are finished with the InputStream, so that the InputStream is closed.InputStream.IOException - If the InputStream could not be opened.public abstract ByteBuffer read() throws IOException
ByteBuffer for a classpath resource. Make sure you call close() when you are
finished with the ByteBuffer, so that the ByteBuffer is released or unmapped.ByteBuffer for the resource file content.IOException - If the resource could not be opened.public abstract byte[] load()
throws IOException
close() after loading the byte array and before returning it, so that the underlying
InputStream is closed or the underlying ByteBuffer is released or unmapped.IOException - If the file contents could not be loaded in their entirety.public long getLength()
open(),
read(), or load() (and for open(), only if the underlying jarfile has
length information for corresponding ZipEntry -- some jarfiles may not have length
information in their zip entries). Returns -1L if the length is unknown.public long getLastModified()
Note: The ZIP format has no notion of timezone, so timestamps are only meaningful if it is known what timezone they were created in. We arbitrarily assume that zipfile timestamps are in the UTC timezone. This may be a wrong assumption, so you may need to apply a timezone correction if you know the timezone used by the zipfile creator.
public Set<PosixFilePermission> getPosixFilePermissions()
PosixFilePermission permission flags for the resource, or null if unknown.public String toString()
public int compareTo(Resource o)
compareTo in interface Comparable<Resource>public void close()
close in interface Closeableclose in interface AutoCloseableCopyright © 2019. All rights reserved.