Package io.github.classgraph
Class Resource
- java.lang.Object
-
- io.github.classgraph.Resource
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Comparable<Resource>
public abstract class Resource extends java.lang.Object implements java.io.Closeable, java.lang.Comparable<Resource>
A classpath or module path resource (i.e. file) that was found in a whitelisted/non-blacklisted package inside a classpath element or module.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
Resource.InputStreamResourceCloser
Class for closing the parentResource
when anInputStream
opened on the resource is closed.
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.ByteBuffer
byteBuffer
protected java.io.InputStream
inputStream
protected boolean
isOpen
protected long
length
-
Constructor Summary
Constructors Constructor Description Resource()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected byte[]
byteBufferToByteArray()
protected java.io.InputStream
byteBufferToInputStream()
void
close()
Close the underlying InputStream, or release/unmap the underlying ByteBuffer.int
compareTo(Resource o)
boolean
equals(java.lang.Object obj)
abstract java.io.File
getClasspathElementFile()
abstract java.net.URL
getClasspathElementURL()
long
getLength()
abstract ModuleRef
getModuleRef()
abstract java.lang.String
getPath()
abstract java.lang.String
getPathRelativeToClasspathElement()
abstract java.net.URL
getURL()
int
hashCode()
protected byte[]
inputStreamToByteArray()
protected java.nio.ByteBuffer
inputStreamToByteBuffer()
abstract byte[]
load()
Load a classpath resource and return its content as a byte array.protected void
markAsClosed()
Mark the resource as closed.protected void
markAsOpen()
Mark the resource as open.abstract java.io.InputStream
open()
Open anInputStream
for a classpath resource.abstract java.nio.ByteBuffer
read()
Open aByteBuffer
for a classpath resource.java.lang.String
toString()
Return a string representation of the resource's location (as a URL string).
-
-
-
Method Detail
-
byteBufferToInputStream
protected java.io.InputStream byteBufferToInputStream()
-
inputStreamToByteBuffer
protected java.nio.ByteBuffer inputStreamToByteBuffer() throws java.io.IOException
- Throws:
java.io.IOException
-
inputStreamToByteArray
protected byte[] inputStreamToByteArray() throws java.io.IOException
- Throws:
java.io.IOException
-
byteBufferToByteArray
protected byte[] byteBufferToByteArray()
-
markAsOpen
protected void markAsOpen() throws java.io.IOException
Mark the resource as open.- Throws:
java.io.IOException
- If the resource is already open.
-
markAsClosed
protected void markAsClosed()
Mark the resource as closed.
-
getPath
public abstract java.lang.String getPath()
- Returns:
- the path of this classpath resource relative to the package root within the classpath element. 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 11+, returns "com/xyz/resource.xml".
-
getPathRelativeToClasspathElement
public abstract java.lang.String getPathRelativeToClasspathElement()
- Returns:
- the full path of this classpath resource within the classpath element (see JarEntry::getRealPath). 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".
-
getURL
public abstract java.net.URL getURL()
- Returns:
- A URL representing the resource's location. May point to a temporary file that ClassGraph extracted an inner jar or directory to, or downloaded a remote jar to. You may or may not be able to fetch content from the URL.
- Throws:
java.lang.IllegalArgumentException
- if aMalformedURLException
occurred while trying to construct the URL.
-
getClasspathElementURL
public abstract java.net.URL getClasspathElementURL()
- Returns:
- The URL of the classpath element that this class was found within.
-
getClasspathElementFile
public abstract java.io.File getClasspathElementFile()
- Returns:
- The
File
for the classpath element package root dir or jar that this was found within, or null if thisResource
was found in a module. (See alsogetModuleRef()
.)
-
getModuleRef
public abstract ModuleRef getModuleRef()
- Returns:
- The module in the module path that this
Resource
was found within, as aModuleRef
, or null if thisResource
was found in a directory or jar in the classpath. (See alsogetClasspathElementFile()
.)
-
open
public abstract java.io.InputStream open() throws java.io.IOException
Open anInputStream
for a classpath resource. Make sure you callclose()
when you are finished with theInputStream
, so that theInputStream
is closed.- Returns:
- The opened
InputStream
. - Throws:
java.io.IOException
- If theInputStream
could not be opened.
-
read
public abstract java.nio.ByteBuffer read() throws java.io.IOException
Open aByteBuffer
for a classpath resource. Make sure you callclose()
when you are finished with theByteBuffer
, so that theByteBuffer
is released or unmapped.- Returns:
- The allocated or mapped
ByteBuffer
for the resource file content. - Throws:
java.io.IOException
- If the resource could not be opened.
-
load
public abstract byte[] load() throws java.io.IOException
Load a classpath resource and return its content as a byte array. Automatically callsclose()
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:
- The contents of the resource file.
- Throws:
java.io.IOException
- If the file contents could not be loaded in their entirety.
-
getLength
public long getLength()
- Returns:
- The length of the resource. This only reliably returns a valid value after calling
open()
,read()
, orload()
, and foropen()
, only if the underlying jarfile has length information for correspondingZipEntry
(some jarfiles may not have length information in their zip entries). Returns -1L if the length is unknown.
-
toString
public java.lang.String toString()
Return a string representation of the resource's location (as a URL string).- Overrides:
toString
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
compareTo
public int compareTo(Resource o)
- Specified by:
compareTo
in interfacejava.lang.Comparable<Resource>
-
close
public void close()
Close the underlying InputStream, or release/unmap the underlying ByteBuffer.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
-