Package io.github.classgraph
Class Resource
- java.lang.Object
-
- io.github.classgraph.Resource
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Comparable<Resource>
public abstract class Resource extends Object implements Closeable, 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 classResource.InputStreamResourceCloserClass for closing the parentResourcewhen anInputStreamopened on the resource is closed.
-
Field Summary
Fields Modifier and Type Field Description protected ByteBufferbyteBufferprotected InputStreaminputStreamprotected longlength
-
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 InputStreambyteBufferToInputStream()abstract voidclose()Close the underlying InputStream, or release/unmap the underlying ByteBuffer.intcompareTo(Resource o)booleanequals(Object obj)abstract FilegetClasspathElementFile()abstract URLgetClasspathElementURL()longgetLength()abstract ModuleRefgetModuleRef()abstract StringgetPath()abstract StringgetPathRelativeToClasspathElement()abstract URLgetURL()inthashCode()protected byte[]inputStreamToByteArray()protected ByteBufferinputStreamToByteBuffer()abstract byte[]load()Load a classpath resource and return its content as a byte array.abstract InputStreamopen()Open anInputStreamfor a classpath resource.abstract ByteBufferread()Open aByteBufferfor a classpath resource.StringtoString()Return a string representation of the resource's location.protected abstract StringtoStringImpl()
-
-
-
Field Detail
-
inputStream
protected InputStream inputStream
-
byteBuffer
protected ByteBuffer byteBuffer
-
length
protected long length
-
-
Method Detail
-
byteBufferToInputStream
protected InputStream byteBufferToInputStream()
-
inputStreamToByteBuffer
protected ByteBuffer inputStreamToByteBuffer() throws IOException
- Throws:
IOException
-
inputStreamToByteArray
protected byte[] inputStreamToByteArray() throws IOException- Throws:
IOException
-
byteBufferToByteArray
protected byte[] byteBufferToByteArray()
-
getPath
public abstract 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".
-
getPathRelativeToClasspathElement
public abstract String getPathRelativeToClasspathElement()
- Returns:
- the path of this classpath resource within the classpath element. For example, for a resource path of "BOOT-INF/classes/com/xyz/resource.xml", returns "BOOT-INF/classes/com/xyz/resource.xml", even if the package root is "BOOT-INF/classes/".
-
getURL
public abstract 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:
IllegalArgumentException- if aMalformedURLExceptionoccurred while trying to construct the URL.
-
getClasspathElementURL
public abstract URL getClasspathElementURL()
- Returns:
- The URL of the classpath element that this class was found within.
-
getClasspathElementFile
public abstract File getClasspathElementFile()
- Returns:
- The
Filefor the classpath element package root dir or jar that this was found within, or null if thisResourcewas found in a module. (See alsogetModuleRef().)
-
getModuleRef
public abstract ModuleRef getModuleRef()
- Returns:
- The module in the module path that this
Resourcewas found within, as aModuleRef, or null if thisResourcewas found in a directory or jar in the classpath. (See alsogetClasspathElementFile().)
-
open
public abstract InputStream open() throws IOException
Open anInputStreamfor a classpath resource. Make sure you callclose()when you are finished with theInputStream, so that theInputStreamis closed.- Returns:
- The opened
InputStream. - Throws:
IOException- If theInputStreamcould not be opened.
-
read
public abstract ByteBuffer read() throws IOException
Open aByteBufferfor a classpath resource. Make sure you callclose()when you are finished with theByteBuffer, so that theByteBufferis released or unmapped.- Returns:
- The allocated or mapped
ByteBufferfor the resource file content. - Throws:
IOException- If the resource could not be opened.
-
load
public abstract byte[] load() throws IOExceptionLoad 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:
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.
-
toStringImpl
protected abstract String toStringImpl()
-
toString
public String toString()
Return a string representation of the resource's location.
-
compareTo
public int compareTo(Resource o)
- Specified by:
compareToin interfaceComparable<Resource>
-
close
public abstract void close()
Close the underlying InputStream, or release/unmap the underlying ByteBuffer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
-