Class 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 parent Resource when an InputStream opened on the resource is closed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.nio.ByteBuffer byteBuffer
      The byte buffer, or null.
      protected java.io.InputStream inputStream
      The input stream, or null.
      protected long length
      The length, or -1L for unknown.
    • Constructor Summary

      Constructors 
      Constructor Description
      Resource​(io.github.classgraph.ClasspathElement classpathElement, long length)
      Constructor.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected byte[] byteBufferToByteArray()
      Read/copy contents of a ByteBuffer as a byte array.
      protected java.io.InputStream byteBufferToInputStream()
      Create an InputStream from a ByteBuffer.
      void close()
      Close the underlying InputStream, or release/unmap the underlying ByteBuffer.
      int compareTo​(Resource o)  
      boolean equals​(java.lang.Object obj)  
      java.io.File getClasspathElementFile()
      Get the classpath element File.
      java.net.URI getClasspathElementURI()
      Get the URI of the classpath element or module that this resource was obtained from.
      java.net.URL getClasspathElementURL()
      Get the URL of the classpath element or module that this resource was obtained from.
      java.lang.String getContentAsString()
      Convenience method to get the content of this Resource as a String.
      long getLength()
      Get the length of the resource.
      ModuleRef getModuleRef()
      Get the The ModuleRef for the module that this Resource was found within.
      abstract java.lang.String getPath()
      Get the path of this classpath resource relative to the package root.
      abstract java.lang.String getPathRelativeToClasspathElement()
      Get the full path of this classpath resource relative to the root of the classpath element.
      java.net.URI getURI()
      Get the URI representing the resource's location.
      java.net.URL getURL()
      Get the URL representing the resource's location.
      int hashCode()  
      protected byte[] inputStreamToByteArray()
      Read all bytes from an InputStream and return as a byte array.
      protected java.nio.ByteBuffer inputStreamToByteBuffer()
      Create a ByteBuffer from an InputStream.
      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 an InputStream for a classpath resource.
      abstract java.nio.ByteBuffer read()
      Open a ByteBuffer for a classpath resource.
      java.lang.String toString()
      Get a string representation of the resource's location (as a URL string).
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • inputStream

        protected java.io.InputStream inputStream
        The input stream, or null.
      • byteBuffer

        protected java.nio.ByteBuffer byteBuffer
        The byte buffer, or null.
      • length

        protected long length
        The length, or -1L for unknown.
    • Constructor Detail

      • Resource

        public Resource​(io.github.classgraph.ClasspathElement classpathElement,
                        long length)
        Constructor.
        Parameters:
        classpathElement - the classpath element this resource was obtained from.
        length - the length the length of the resource.
    • Method Detail

      • byteBufferToInputStream

        protected java.io.InputStream byteBufferToInputStream()
        Create an InputStream from a ByteBuffer.
        Returns:
        the input stream
      • inputStreamToByteBuffer

        protected java.nio.ByteBuffer inputStreamToByteBuffer()
                                                       throws java.io.IOException
        Create a ByteBuffer from an InputStream.
        Returns:
        the byte buffer
        Throws:
        java.io.IOException - if an I/O exception occurs.
      • inputStreamToByteArray

        protected byte[] inputStreamToByteArray()
                                         throws java.io.IOException
        Read all bytes from an InputStream and return as a byte array.
        Returns:
        the contents of the InputStream.
        Throws:
        java.io.IOException - if an I/O exception occurs.
      • byteBufferToByteArray

        protected byte[] byteBufferToByteArray()
        Read/copy contents of a ByteBuffer as a byte array.
        Returns:
        the contents of the ByteBuffer as a byte array.
      • 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.
      • getURI

        public java.net.URI getURI()
        Get the URI representing the resource's location.
        Returns:
        A URI representing the resource's location.
        Throws:
        java.lang.IllegalArgumentException - the resource was obtained from a module and the module's location URI is null.
      • getURL

        public java.net.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 suppored by URL, and this will cause IllegalArgumentException to be thrown.
        Returns:
        A URL representing the resource's location.
        Throws:
        java.lang.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
      • getClasspathElementURI

        public java.net.URI getClasspathElementURI()
        Get the URI of the classpath element or module that this resource was obtained from.
        Returns:
        The URL of the classpath element or module that this resource was found within.
        Throws:
        java.lang.IllegalArgumentException - if the classpath element does not have a valid URI (e.g. for modules whose location URI is null).
      • getClasspathElementURL

        public java.net.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 suppored by URL, and this will cause IllegalArgumentException to be thrown.
        Returns:
        The URL of the classpath element or module that this resource was found within.
        Throws:
        java.lang.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.
      • getClasspathElementFile

        public java.io.File getClasspathElementFile()
        Get the classpath element 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).
      • getContentAsString

        public java.lang.String getContentAsString()
                                            throws java.io.IOException
        Convenience method to get the content of this Resource as a String. Assumes UTF8 encoding. (Calls close() after completion.)
        Returns:
        the content of this Resource as a String.
        Throws:
        java.io.IOException - If an I/O exception occurred.
      • getPath

        public abstract java.lang.String getPath()
        Get the path of this classpath resource relative to the package root.
        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".
      • getPathRelativeToClasspathElement

        public abstract java.lang.String getPathRelativeToClasspathElement()
        Get the full path of this classpath resource relative to the root of the classpath element.
        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".
      • open

        public abstract java.io.InputStream open()
                                          throws java.io.IOException
        Open an InputStream for a classpath resource. Make sure you call close() when you are finished with the InputStream, so that the InputStream is closed.
        Returns:
        The opened InputStream.
        Throws:
        java.io.IOException - If the InputStream could not be opened.
      • read

        public abstract java.nio.ByteBuffer read()
                                          throws java.io.IOException
        Open a ByteBuffer for a classpath resource. Make sure you call close() when you are finished with the ByteBuffer, so that the ByteBuffer 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 calls 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:
        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()
        Get the length of the resource.
        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.
      • toString

        public java.lang.String toString()
        Get a string representation of the resource's location (as a URL string).
        Overrides:
        toString in class java.lang.Object
        Returns:
        the resource location as a URL String.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • compareTo

        public int compareTo​(Resource o)
        Specified by:
        compareTo in interface java.lang.Comparable<Resource>
      • close

        public void close()
        Close the underlying InputStream, or release/unmap the underlying ByteBuffer.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable