-
-
Notifications
You must be signed in to change notification settings - Fork 294
Resource API
See also the ClassGraph API overview.
💡 If you just need the content of the resource as a String
, call Resource#getContentAsString()
.
A Resource
can be opened as a ByteBuffer
, InputStream
or loaded as a byte[]
array. For ByteBuffer
and InputStream
, be sure to call .close()
on the Resource
once you have finished with it:
ResourceList matchingResources = scanResult.getResourcesWithPath("config/cfg.xml");
for (Resource resource : matchingResources) {
try {
InputStream inputStream = resource.open();
// ... Use inputStream ...
} finally {
resource.close();
}
}
The above is equivalent to the following pattern (putting Resource resToClose = resource;
as the first entry in the try
header will cause resource
to be closed after inputStream
is closed):
ResourceList matchingResources = scanResult.getResourcesWithPath("config/cfg.xml");
for (Resource resource : matchingResources) {
try (Resource resToClose = resource; InputStream inputStream = resource.open()) {
// ... Use inputStream ...
}
}
Alternatively you can use the ResourceList#forEach*()
methods, which call Resource#close()
for you:
ResourceList matchingResources = scanResult.getResourcesWithPath("config/cfg.xml");
matchingResources.forEachInputStreamIgnoringIOException((resource, inputStream) -> {
// ... Use inputStream ...
});
A reference to a file found within a classath directory or jarfile, or within a module. Obtained by calling ScanResult#getAllResources()
and related methods.
In addition to optionally scanning classes on the classpath, ClassGraph always records the names of all resources (files) encountered in accepted packages during the scan (including classfiles and non-class files). The content of any resource file, or resource files with paths matching a certain pattern, can be read after the scan.
Behind the scenes, ClassGraph tries to open resources as memory-mapped file channel wherever possible for speed, while transparently translating from the fastest available API for accessing any given resource to whichever API mechanism you want to use to read the resource, allowing you to access the resource as either an InputStream
, ByteBuffer
or byte[]
array, in the fastest way possible.
-
Reading resource content:
💡 The best way to read from resources is by filtering a
ResourceList
for resources with paths of interest, then calling.forEachByteArrayIgnoringIOException(ByteArrayConsumer)
,.forEachByteArrayThrowingIOException(ByteArrayConsumerThrowsIOException)
,.forEachInputStreamIgnoringIOException(InputStreamConsumer)
,.forEachInputStreamThrowingIOException(InputStreamConsumerThrowsIOException)
,.forEachByteBufferIgnoringIOException(InputStreamConsumer)
or.forEachByteBufferThrowingIOException(InputStreamConsumerTrowsIOException)
, since these all properly close theResource
after the consumer has read from the resource.💡 However, if you want to read the contents of a
Resource
manually, using one of the following methods, don't forget to callResource#close()
when you have finished reading from the resource.💡 ClassGraph will transparently translate the mechanism for accessing the underlying resource type (whether the resource is a file in a directory, or an entry in a jarfile, or a resource in a module) to
ByteBuffer
,InputStream
orbyte[]
array, in the most efficient manner possible.🛑 Important:
Resource
is not threadsafe -- you should only have any givenResource
open in one thread at a time.-
.open()
opens the resource as anInputStream
. -
.read()
opens the resource as aByteBuffer
. Note that you must call.close()
on theResource
when you are finished with thisByteBuffer
to release theByteBuffer
. Alternatively, callResource#readCloseable()
instead (see below). -
.readCloseable()
returns aCloseableByteBuffer
that implements theCloseable
interface, so that when itsclose()
method is called, it automatically callsResource#close()
to release the wrappedByteBuffer
. CallCloseableByteBuffer#getByteBuffer()
to get the wrappedByteBuffer
. -
.load()
loads the entire content of the resource as abyte[]
array. (This calls.close()
on theResource
once all content is loaded.) - **
.getContentAsString()**
is a convenience method that calls.load()
to get the resource content, then defines and returnsString
. (Assumes UTF8 encoding.) -
.close()
closes the underlyingInputStream
or releases/unmaps the underlyingByteBuffer
for theResource
.
-
-
Reading resource metadata:
-
.getLength()
returns the length of the resource in bytes, or-1
if unknown. The value generally remains unknown until.read()
or.load()
is called. If you call.open()
, the length of the resultingInputStream
may or may not be known. -
getPosixPermissions()
returns the POSIX file permissions for theResource
as aSet<PosixFilePermission>
, obtained from either the directory entry for the containing directory (for dir-based resources), or the zipfile central directory (for jarfile resources). If not available or unknown (e.g. for system module resources or jlink'd resources), returns null. -
getLastModified()
returns the last modified timestamp for the resource (in milliseconds since the epoch), if known. If not available or unknown (e.g. for system module resources or jlink'd resources), returns0L
. -
.getPath()
returns the path of the resource, relative to the package root, as aString
. For example, for a resource path ofBOOT-INF/classes/com/xyz/resource.xml
and a package root ofBOOT-INF/classes/
, returnscom/xyz/resource.xml
. -
.getPathRelativeToClasspathElement()
returns the path of the resource relative to the classpath element (instead of relative to the package root) as aString
. For example, for a resource path ofBOOT-INF/classes/com/xyz/resource.xml
, returnsBOOT-INF/classes/com/xyz/resource.xml
, even if the package root isBOOT-INF/classes/
. -
.getURI()
returns aURI
for the resource. -
.getURL()
returns aURL
for the resource. Note that this will throwIllegalArgumentException
if the resource was obtained from a system module or a jlink'd runtime image, since the URI scheme ofjrt:
is only supported byURI
, not byURL
. -
.getClasspathElementURI()
returns aURI
for the classpath element containing the resource. -
.getClasspathElementURL()
returns aURL
for the classpath element containing the resource. Note that this will throwIllegalArgumentException
if the resource was obtained from a system module or a jlink'd runtime image, since the URI scheme ofjrt:
is only supported byURI
, not byURL
. -
.getClasspathElementFile()
returns aFile
for the classpath element containing the resource, or null if the resource was contained in a system module or jlinkd runtime image (since the
.getClasspathElementURI()will return a
URIwith
jrt:` scheme, which cannot be resolved to a file). -
.getModuleRef()
returns aModuleRef
for the module containing the resource, or null if the resource was contained in a directory or jarfile on the traditional classpath.
-
Extends ArrayList<Resource>
with the following convenience methods:
-
Converting to
Map
:-
.asMap()
returns theResourceList
as aMap<String, ResourceList>
that maps paths to aResourceList
of resources that have each path.💡 If the resource path is duplicated across different classpath elements, the
ResourceList
in the map values will contain more than one element. See also.findDuplicatePaths()
.
-
-
Reading resource metadata for each
Resource
in the list:-
.getPaths()
returns a list of the paths of resources in this list relative to the package root, by calling.getPath()
for eachResource
in the list. Returns aList<String>
. -
.getPathsRelativeToClasspathElement()
returns a list of the paths of resources in this list relative to the classpath element (instead of relative to the package root) by calling.getPathRelativeToClasspathElement()
for eachResource
in the list. Returns aList<String>
. -
.getURLs()
returns a list of the URLs of resources in this list, as aList<URL>
. You should probably use.getURIs()
instead, since resource URIs can containjrt:/
schemes if the resource is in a jlink'd image or a system module, andURL
does not support this scheme. -
.getURIs()
returns a list of the URIs of resources in this list, as aList<URI>
. -
.getAsStrings()
returns a list of the result of calling.toString()
on eachResource
object in this list, producing aList<string>
ofString
representations of eachResource
location.
-
-
Filtering
Resource
elements:-
.filter(ResourceFilter filter)
returns aResourceList
that is a subset of the original list, obtained by applying the given filter predicate to eachResource
in the list.-
ResourceFilter
is aFunctionalInterface
with the single abstract methodboolean accept(Resource resource)
.
-
-
.classFilesOnly()
returns aResourceList
that is the subset of the original list whoseResource
paths end with ".class". -
.nonClassFilesOnly()
returns aResourceList
that is the subset of the original list whoseResource
paths do not end with ".class".
-
- Finding resources with duplicate paths:
-
Reading content of each
Resource
in the list: (these methods all have unique names to avoid running into problems of ambiguous overloaded method resolution when lambdas are used rather than classes for theconsumer
parameter):💡 ClassGraph will transparently translate the mechanism for accessing the underlying resource type (whether the resource is a file in a directory, or an entry in a jarfile, or a resource in a module) to
ByteBuffer
,InputStream
orbyte[]
array, in the most efficient manner possible.-
.forEachByteArrayIgnoringIOException(ByteArrayConsumer consumer)
loads the complete content of each resource in the list, calls theByteArrayConsumer
with the content, then callsResource#close()
to clean up resources. AnyIOException
thrown while trying to open or read a resource causes the resource to be skipped.-
ByteArrayConsumer
is aFunctionalInterface
with the single abstract methodvoid accept(Resource resource, byte[] byteArray)
-
-
.forEachByteArrayThrowingIOException(ByteArrayConsumerThrowsIOException consumer)
loads the complete content of each resource in the list, calls theByteArrayConsumer
with the content, then callsResource#close()
to clean up resources. AnyIOException
thrown while trying to open or read a resource causes the exception to be thrown to the caller.-
ByteArrayConsumerThrowsIOException
is aFunctionalInterface
with the single abstract methodvoid accept(Resource resource, byte[] byteArray) throws IOException
-
-
.forEachInputStreamIgnoringIOException(InputStreamConsumer consumer)
opens each resource in the list as anInputStream
, calls theInputStreamConsumer
with the content, then callsResource#close()
to close theInputStream
. AnyIOException
thrown while trying to open or read a resource causes the resource to be skipped.-
InputStreamConsumer
is aFunctionalInterface
with the single abstract methodvoid accept(Resource resource, InputStream inputStream)
-
-
.forEachInputStreamThrowingingIOException(InputStreamConsumerThrowsIOException consumer)
opens each resource in the list as anInputStream
, calls theInputStreamConsumer
with the content, then callsResource#close()
to close theInputStream
. AnyIOException
thrown while trying to open or read a resource causes the resource to be thrown to the caller.-
InputStreamConsumerThrowsIOException
is aFunctionalInterface
with the single abstract methodvoid accept(Resource resource, InputStream inputStream) throws IOException
-
-
.forEachByteBufferIgnoringIOException(InputStreamConsumer consumer)
opens each resource in the list as aByteBuffer
(memory-mapped if possible), calls theByteBufferConsumer
with the content, then callsResource#close()
to release theByteBuffer
. AnyIOException
thrown while trying to open or read a resource causes the resource to be skipped.-
ByteBufferConsumer
is aFunctionalInterface
with the single abstract methodvoid accept(Resource resource, ByteBuffer byteBuffer)
-
-
.forEachByteBufferThrowingIOException(InputStreamConsumerThrowsIOException consumer)
opens each resource in the list as aByteBuffer
(memory-mapped if possible), calls theByteBufferConsumer
with the content, then callsResource#close()
to release theByteBuffer
. AnyIOException
thrown while trying to open or read a resource causes the resource to be thrown to the caller.-
ByteBufferConsumerThrowsIOException
is aFunctionalInterface
with the single abstract methodvoid accept(Resource resource, ByteBuffer byteBuffer) throws IOException
-
-