Top Description Fields Constructors Methods
io.github.classgraph

public abstract Class Resource

extends Object
implements Closeable, Comparable<Resource>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable, java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.Closeable, .File, .IOException, .InputStream, java.net.MalformedURLException, .URI, .URISyntaxException, .URL, java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.nio.file.attribute.PosixFilePermission, java.util.Set, java.util.zip.ZipEntry, nonapi.io.github.classgraph.fileslice.reader.ClassfileReader, nonapi.io.github.classgraph.utils.LogNode, .URLPathEncoder

A classpath or module path resource (i.e. file) that was found in an accepted/non-rejected package inside a classpath element or module.

Field Summary

Modifier and TypeField and Description
protected ByteBuffer
byteBuffer

The byte buffer, or null.

private final ClasspathElement
classpathElement

The classpath element this resource was obtained from.

protected InputStream
inputStream

The input stream, or null.

protected long
length

The length, or -1L for unknown.

pack-priv LogNode
scanLog

The LogNode used to log that the resource was found when classpath element paths are scanned.

private String
toString

The cached result of toString().

Constructor Summary

AccessConstructor and Description
public
Resource(final ClasspathElement
the classpath element this resource was obtained from.
classpathElement
,
final long
the length the length of the resource.
length
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.io.Closeable.close.

Close the underlying InputStream, or release/unmap the underlying ByteBuffer.

public int

Returns:

the int
compareTo
(final Resource
the o
o
)

Implements java.lang.Comparable.compareTo.

Compare to.

public boolean

Returns:

true, if successful
equals
(final Object
the obj
obj
)

Overrides java.lang.Object.equals.

Equals.

public File

Returns:

The 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).
getClasspathElementFile
()

Get the classpath element File.

public URI

Returns:

The URL of the classpath element or module that this resource was found within.
getClasspathElementURI
()

Get the URI of the classpath element or module that this resource was obtained from.

public URL

Returns:

The URL of the classpath element or module that this resource was found within.
getClasspathElementURL
()

Get the URL of the classpath element or module that this resource was obtained from.

public String

Returns:

the content of this Resource as a String.
getContentAsString
()

Convenience method to get the content of this Resource as a String.

public abstract long

Returns:

The millis since the epoch indicating the date / time that this file resource was last modified. Returns 0L if the last modified date is unknown.
getLastModified
()

Get the last modified time for the resource, in milliseconds since the epoch.

public long

Returns:

The length of the resource. This only reliably returns a valid value after calling 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.
getLength
()

Get the length of the resource.

public ModuleRef

Returns:

The ModuleRef for the module that this Resource was found within, as a ModuleRef, or null if this Resource was found in a directory or jar in the classpath.
getModuleRef
()

Get the The ModuleRef for the module that this Resource was found within.

public abstract String

Returns:

the path of this classpath resource relative to the package root. For example, for a resource path of "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".
getPath
()

Get the path of this classpath resource relative to the package root.

public String

Returns:

the full path of this classpath resource within the classpath element. For example, will return the full path of "BOOT-INF/classes/com/xyz/resource.xml" or "META-INF/versions/11/com/xyz/resource.xml", not "com/xyz/resource.xml".
getPathRelativeToClasspathElement
()

Get the full path of this classpath resource relative to the root of the classpath element.

public abstract Set<PosixFilePermission>

Returns:

The set of PosixFilePermission permission flags for the resource, or null if unknown.
getPosixFilePermissions
()

Get the POSIX file permissions for the resource.

public URI

Returns:

A URI representing the resource's location.
getURI
()

Get the URI representing the resource's location.

public URL

Returns:

A URL representing the resource's location.
getURL
()

Get the URL representing the resource's location.

public int

Returns:

the int
hashCode
()

Overrides java.lang.Object.hashCode.

Hash code.

public abstract byte[]

Returns:

The contents of the resource file.
load
()

Load a classpath resource and return its content as a byte array.

public abstract InputStream

Returns:

The opened InputStream.
open
()

Open an InputStream for a classpath resource.

pack-priv abstract ClassfileReader

Returns:

the ClassfileReader.
openClassfile
()

Open a ClassfileReader on the resource (for reading classfiles).

public abstract ByteBuffer

Returns:

The allocated or mapped ByteBuffer for the resource file content.
read
()

Open a ByteBuffer for a classpath resource.

public CloseableByteBuffer

Returns:

The allocated or mapped ByteBuffer for the resource file content.
readCloseable
()

Open a ByteBuffer for a classpath resource, and wrap it in a CloseableByteBuffer instance, which implements the Closeable#close() method to free the underlying ByteBuffer when CloseableByteBuffer#close() is called, by automatically calling Resource#close().

public String

Returns:

the resource location as a URL String.
toString
()

Overrides java.lang.Object.toString.

Get a string representation of the resource's location (as a URL string).

private static URL

Returns:

the URL.
uriToURL
(final URI
the uri
uri
)

Convert a URI to URL, catching "jrt:" URIs as invalid.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

byteBufferback to summary
protected ByteBuffer byteBuffer

The byte buffer, or null.

classpathElementback to summary
private final ClasspathElement classpathElement

The classpath element this resource was obtained from.

inputStreamback to summary
protected InputStream inputStream

The input stream, or null.

lengthback to summary
protected long length

The length, or -1L for unknown.

scanLogback to summary
pack-priv LogNode scanLog

The LogNode used to log that the resource was found when classpath element paths are scanned. In the case of accepted classfile resources, sublog entries are added when the classfile's contents are scanned.

toStringback to summary
private String toString

The cached result of toString().

Constructor Detail

Resourceback to summary
public Resource(final ClasspathElement classpathElement, final long length)

Constructor.

Parameters
classpathElement:ClasspathElement

the classpath element this resource was obtained from.

length:long

the length the length of the resource.

Method Detail

closeback to summary
public void close()

Implements java.io.Closeable.close.

Close the underlying InputStream, or release/unmap the underlying ByteBuffer.

Annotations
@Override
compareToback to summary
public int compareTo(final Resource o)

Implements java.lang.Comparable.compareTo.

Compare to.

Parameters
o:Resource

the o

Returns:int

the int

Annotations
@Override
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Equals.

Parameters
obj:Object

the obj

Returns:boolean

true, if successful

Annotations
@Override
getClasspathElementFileback to summary
public File getClasspathElementFile()

Get the classpath element File.

Returns:File

The 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).

getClasspathElementURIback to summary
public URI getClasspathElementURI()

Get the URI of the classpath element or module that this resource was obtained from.

Returns:URI

The URL of the classpath element or module that this resource was found within.

Exceptions
IllegalArgumentException:
if the classpath element does not have a valid URI (e.g. for modules whose location URI is null).
getClasspathElementURLback to summary
public URL getClasspathElementURL()

Get the 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 supported by URL, and this will cause IllegalArgumentException to be thrown.

Returns:URL

The URL of the classpath element or module that this resource was found within.

Exceptions
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.
getContentAsStringback to summary
public String getContentAsString() throws IOException

Convenience method to get the content of this Resource as a String. Assumes UTF8 encoding. (Calls close() after completion.)

Returns:String

the content of this Resource as a String.

Exceptions
IOException:
If an I/O exception occurred.
getLastModifiedback to summary
public abstract long getLastModified()

Get the last modified time for the resource, in milliseconds since the epoch. This time is obtained from the directory entry, if this resource is a file on disk, or from the zipfile central directory, if this resource is a zipfile entry. Timestamps are not available for resources obtained from system modules or jlink'd modules.

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.

Returns:long

The millis since the epoch indicating the date / time that this file resource was last modified. Returns 0L if the last modified date is unknown.

getLengthback to summary
public long getLength()

Get the length of the resource.

Returns:long

The length of the resource. This only reliably returns a valid value after calling 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.

getModuleRefback to summary
public ModuleRef getModuleRef()

Get the The ModuleRef for the module that this Resource was found within.

Returns:ModuleRef

The ModuleRef for the module that this Resource was found within, as a ModuleRef, or null if this Resource was found in a directory or jar in the classpath.

getPathback to summary
public abstract String getPath()

Get the path of this classpath resource relative to the package root.

Returns:String

the path of this classpath resource relative to the package root. For example, for a resource path of "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".

getPathRelativeToClasspathElementback to summary
public String getPathRelativeToClasspathElement()

Get the full path of this classpath resource relative to the root of the classpath element.

Returns:String

the full path of this classpath resource within the classpath element. For example, will return the full path of "BOOT-INF/classes/com/xyz/resource.xml" or "META-INF/versions/11/com/xyz/resource.xml", not "com/xyz/resource.xml".

getPosixFilePermissionsback to summary
public abstract Set<PosixFilePermission> getPosixFilePermissions()

Get the POSIX file permissions for the resource. POSIX file permissions are obtained from the directory entry, if this resource is a file on disk, or from the zipfile central directory, if this resource is a zipfile entry. POSIX file permissions are not available for resources obtained from system modules or jlink'd modules, and may not be available on non-POSIX-compliant operating systems or non-POSIX filesystems.

Returns:Set<PosixFilePermission>

The set of PosixFilePermission permission flags for the resource, or null if unknown.

getURIback to summary
public URI getURI()

Get the URI representing the resource's location.

Returns:URI

A URI representing the resource's location.

Exceptions
IllegalArgumentException:
the resource was obtained from a module and the module's location URI is null.
getURLback to summary
public URL getURL()

Get the 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 supported by URL, and this will cause IllegalArgumentException to be thrown.

Returns:URL

A URL representing the resource's location.

Exceptions
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
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Hash code.

Returns:int

the int

Annotations
@Override
loadback to summary
public abstract byte[] load() throws IOException

Load a classpath resource and return its content as a byte array. Automatically calls Resource#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.

Returns:byte[]

The contents of the resource file.

Exceptions
IOException:
If the file contents could not be loaded in their entirety.
openback to summary
public abstract InputStream open() throws IOException

Open an InputStream for a classpath resource. Make sure you call Resource#close() when you are finished with the InputStream, so that the InputStream is closed.

Returns:InputStream

The opened InputStream.

Exceptions
IOException:
If the InputStream could not be opened.
openClassfileback to summary
pack-priv abstract ClassfileReader openClassfile() throws IOException

Open a ClassfileReader on the resource (for reading classfiles).

Returns:ClassfileReader

the ClassfileReader.

Exceptions
IOException:
if an I/O exception occurs.
readback to summary
public abstract ByteBuffer read() throws IOException

Open a ByteBuffer for a classpath resource. Make sure you call Resource#close() when you are finished with the ByteBuffer, so that the ByteBuffer is released or unmapped. See also readCloseable().

Returns:ByteBuffer

The allocated or mapped ByteBuffer for the resource file content.

Exceptions
IOException:
If the resource could not be read.
readCloseableback to summary
public CloseableByteBuffer readCloseable() throws IOException

Open a ByteBuffer for a classpath resource, and wrap it in a CloseableByteBuffer instance, which implements the Closeable#close() method to free the underlying ByteBuffer when CloseableByteBuffer#close() is called, by automatically calling Resource#close().

Call CloseableByteBuffer#getByteBuffer() on the returned instance to access the underlying ByteBuffer.

Returns:CloseableByteBuffer

The allocated or mapped ByteBuffer for the resource file content.

Exceptions
IOException:
If the resource could not be read.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Get a string representation of the resource's location (as a URL string).

Returns:String

the resource location as a URL String.

Annotations
@Override
uriToURLback to summary
private static URL uriToURL(final URI uri)

Convert a URI to URL, catching "jrt:" URIs as invalid.

Parameters
uri:URI

the uri

Returns:URL

the URL.

Exceptions
IllegalArgumentException:
if the URI could not be converted to a URL, or the URI had "jrt:" scheme.