| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
See also the ClassGraph API overview.
๐ก If you just need the content of the resource as a String, call Resource#loadAsString(), which loads the content and closes the resource for you.
A Resource can be opened as an InputStream or a ByteBuffer, or loaded as a byte[] array. After calling open() or read(), you must close either the Resource or the InputStream / CloseableByteBuffer you were handed, so that the stream is closed or the buffer is released. All three implement AutoCloseable, so try-with-resources does this for you, and closing both the Resource and what it handed you is safe:
ResourceList matchingResources = scanResult.getResourcesWithPath("config/cfg.xml");
for (Resource resource : matchingResources) {
try (resource; InputStream inputStream = resource.open()) {
// ... Use inputStream ...
}
}ResourceList also implements AutoCloseable, and closing the list closes every Resource in it, so a single try-with-resources block can cover the whole list:
try (ResourceList matchingResources = scanResult.getResourcesWithPath("config/cfg.xml")) {
for (Resource resource : matchingResources) {
InputStream inputStream = resource.open();
// ... Use inputStream ...
}
}Alternatively you can use the ResourceList#forEach*() methods, which open the resource, hand its content to your consumer, and then close the resource for you:
scanResult.getResourcesWithPath("config/cfg.xml").forEachInputStream((resource, inputStream) -> {
// ... Use inputStream ...
});A reference to a file found within a classpath 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 of resource files with paths matching a certain pattern, can be read after the scan.
Behind the scenes, ClassGraph reads resources through the virtual filesystem, which reads a file through an NIO FileChannel, memory-mapping it on Windows and using positioned channel reads everywhere else. It translates transparently from the fastest available API for accessing any given resource to whichever API you want to use to read it, so that you can access the resource as an InputStream, a ByteBuffer or a byte[] array, in the fastest way possible.
Reading resource content:
๐ก The simplest way to read from resources is by filtering a ResourceList down to the resources with paths of interest, then calling one of the .forEach*() methods on the list, since those all close each Resource after the consumer has read from it.
๐ก If you read the content of a Resource yourself, using one of the following methods, don't forget to call Resource#close() when you have finished reading from it (or open the Resource in a try-with-resources block).
๐ Important: Resource is not threadsafe -- any given Resource should only be open in one thread at a time. Opening a Resource that is already open, or opening any Resource after the ScanResult has been closed, throws IllegalStateException.
Reading resource metadata:
๐ก The ZIP format has no notion of timezone, so ClassGraph assumes that zipfile timestamps are UTC. If you know the timezone the zipfile was created in, and it was not UTC, you will need to apply the corresponding correction yourself.
Reaching the virtual filesystem:
๐ The returned VfsEntry stops working once the ScanResult that the Resource came from is closed, since closing the ScanResult closes the Vfs that the entry is read through.
Returned by Resource#read() and by VfsEntry#read(). Wraps a ByteBuffer in an AutoCloseable, so that whatever storage the buffer holds is handed back when the CloseableByteBuffer is closed. Some of these buffers own nothing of their own -- an entry read from inside a jarfile is a slice of the jarfile, which is released when the ScanResult or Vfs is closed -- so closing one can do nothing at all, but the wrapper is the same either way, so the calling code does not have to know which kind it has.
try (CloseableByteBuffer buf = resource.read()) {
ByteBuffer byteBuffer = buf.getByteBuffer();
// ... Use byteBuffer ...
}Extends ArrayList<Resource>, and implements AutoCloseable, with the following convenience methods.
๐ก ResourceList instances returned by the ClassGraph API are unmodifiable: any attempt to add, remove, replace or sort their elements throws UnsupportedOperationException. Copy the list if you need a modifiable version of it, e.g. new ArrayList<>(resourceList).
Closing:
Converting to Map:
๐ก If a resource path is duplicated across different classpath elements, the ResourceList in the map value will contain more than one element. See also .findDuplicatePaths().
Reading resource metadata for each Resource in the list:
Selecting Resource elements:
Reading the content of each Resource in the list:
๐ก These methods all have distinct names, rather than being overloads of a single name, so that a lambda passed as the consumer is never ambiguous.
๐ก Each of the three content types comes in two forms: the plain form propagates any IOException thrown while opening or reading a resource (or thrown by the consumer itself) to the caller, and the IgnoringIOException form silently skips that resource and continues with the rest of the list.
๐ก All six methods call Resource#close() after the consumer returns, and return this, for method chaining.
| Back | FazBrowse Home | New Git URL |