java.lang.Object
io.smallrye.common.resource.Resource
Direct Known Subclasses:
JarFileResource, MemoryResource, PathResource, URLResource

public abstract class Resource extends Object
A handle to a loadable resource, which usually will come from a JAR or the filesystem.
  • Constructor Details

    • Resource

      protected Resource(String path)
      Construct a new instance.
      Parameters:
      path - the resource path (must not be null)
  • Method Details

    • pathName

      public final String pathName()
      Returns the resource's relative path. The returned path is relative (that is, it does not start with /) and canonical (that is, contains no sequences of more than one consecutive /, contains no . or .. segments, and does not end with a /).
      Returns:
      the resource's relative path
    • url

      public abstract URL url()
      Returns the resource URL (not null). If the resource location information cannot be converted to a URL, an exception may be thrown.
      Returns:
      the resource URL (not null)
    • openStream

      public abstract InputStream openStream() throws IOException
      Open an input stream to read this resource.
      Returns:
      the input stream (not null)
      Throws:
      IOException - if the input stream could not be opened or the resource is a directory
    • readStream

      public <R> R readStream(Function<InputStream,R> function) throws IOException
      Perform the given action on the input stream of this resource.
      Type Parameters:
      R - the type of the function result
      Parameters:
      function - an action to perform (must not be null)
      Returns:
      the result of the action function
      Throws:
      IOException - if the stream could not be opened, or the resource is a directory, or the action throws an instance of UncheckedIOException
    • openDirectoryStream

      public DirectoryStream<Resource> openDirectoryStream() throws IOException
      Open a directory stream to read the contents of this directory. Not every resource implementation supports directory access.
      Returns:
      the directory stream (not null)
      Throws:
      IOException - if the directory could not be opened or the resource is not a directory
    • isDirectory

      public boolean isDirectory()
      Returns true if this resource represents a directory, or false otherwise. Not every resource implementation supports directory access.
      Returns:
      true if this resource represents a directory, or false otherwise
    • asBuffer

      public ByteBuffer asBuffer() throws IOException
      Returns the bytes of this resource, as a read-only byte buffer. The buffer is suitable for passing to ClassLoader.defineClass(String, ByteBuffer, ProtectionDomain). The default implementation reads all of the resource bytes from the stream returned by openStream(). Other implementations might return a buffer for data already contained in memory, or might return a memory-mapped buffer if the resource is very large. The buffer might or might not be cached on the resource. Because of this, care should be taken to avoid calling this method repeatedly for a single resource.
      Implementation Requirements:
      Implementers must ensure that the returned buffer is read-only.
      Returns:
      the bytes of this resource, as a read-only byte buffer
      Throws:
      IOException - if the content could not be read
      OutOfMemoryError - if the size of the resource is greater than the maximum allowed size of a buffer
    • copyTo

      public long copyTo(Path destination) throws IOException
      Copy the bytes of this resource to the given destination. The copy may fail before all of the bytes have been transferred; in this case the content and state of the destination are undefined.

      The path is opened as if with the following options:

      Parameters:
      destination - the destination path (must not be null)
      Returns:
      the number of bytes copied
      Throws:
      IOException - if the copy fails
    • copyTo

      public long copyTo(OutputStream destination) throws IOException
      Copy the bytes of this resource to the given destination. The copy may fail before all of the bytes have been transferred; in this case the content and state of the destination are undefined. The destination stream is not closed.
      Parameters:
      destination - the destination stream (must not be null)
      Returns:
      the number of bytes copied
      Throws:
      IOException - if the copy fails
    • copyTo

      public long copyTo(WritableByteChannel channel) throws IOException
      Copy the bytes of this resource to the given destination. The copy may fail before all of the bytes have been transferred; in this case the content and state of the destination are undefined. The destination channel is not closed.
      Parameters:
      channel - the destination channel (must not be null and must not be non-blocking)
      Returns:
      the number of bytes copied
      Throws:
      IOException - if the copy fails
    • asString

      public String asString(Charset charset) throws IOException
      Returns the resource content as a string.
      Parameters:
      charset - the character set to use for decoding (must not be null)
      Returns:
      the resource content as a string
      Throws:
      IOException - if the content could not be read
    • modifiedTime

      public Instant modifiedTime()
      Returns the modification time of the resource, or null if the time is unknown.
      Returns:
      the modification time of the resource, or null if the time is unknown
    • codeSigners

      public List<CodeSigner> codeSigners()
      Returns the list of code signers for this resource. The resource must have been fully read, or else consumed as a buffer. By default, the base implementation returns an empty list.
      Returns:
      the list of code signers for this resource
    • size

      public abstract long size()
      Returns the size of the resource, or -1 if the size is not known.
      Returns:
      the size of the resource, or -1 if the size is not known