Class PathResource

java.lang.Object
io.smallrye.common.resource.Resource
io.smallrye.common.resource.PathResource

public final class PathResource extends Resource
A resource corresponding to a Path.
  • Constructor Details

    • PathResource

      public PathResource(String pathName, Path path)
      Construct a new instance.
      Parameters:
      pathName - the relative path name (must not be null)
      path - the path (must not be null)
  • Method Details

    • path

      public Path path()
      Returns the path of this resource.
      Returns:
      the path of this resource
    • hasFile

      public boolean hasFile()
      Returns true if the resource has a physical File, or false if it does not.
      Returns:
      true if the resource has a physical File, or false if it does not
    • file

      public File file()
      Returns the physical File, if there is one. Use hasFile() to determine whether there is a physical file.
      Returns:
      the physical File, if there is one
      Throws:
      UnsupportedOperationException - if the path does not have a physical file
    • url

      public URL url()
      Description copied from class: Resource
      Returns the resource URL (not null). If the resource location information cannot be converted to a URL, an exception may be thrown.
      Specified by:
      url in class Resource
      Returns:
      the resource URL (not null)
    • openDirectoryStream

      public DirectoryStream<Resource> openDirectoryStream() throws IOException
      Description copied from class: Resource
      Open a directory stream to read the contents of this directory. Not every resource implementation supports directory access.
      Overrides:
      openDirectoryStream in class Resource
      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()
      Description copied from class: Resource
      Returns true if this resource represents a directory, or false otherwise. Not every resource implementation supports directory access.
      Overrides:
      isDirectory in class Resource
      Returns:
      true if this resource represents a directory, or false otherwise
    • openStream

      public InputStream openStream() throws IOException
      Description copied from class: Resource
      Open an input stream to read this resource.
      Specified by:
      openStream in class Resource
      Returns:
      the input stream (not null)
      Throws:
      IOException - if the input stream could not be opened or the resource is a directory
    • asBuffer

      public ByteBuffer asBuffer() throws IOException
      Description copied from class: Resource
      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 Resource.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.
      Overrides:
      asBuffer in class Resource
      Returns:
      the bytes of this resource, as a read-only byte buffer
      Throws:
      IOException - if the content could not be read
    • isMappable

      public boolean isMappable()
      Description copied from class: Resource
      Returns true if this resource can be mapped into memory, or false if it cannot be.
      Overrides:
      isMappable in class Resource
      Returns:
      true if this resource can be mapped into memory, or false if it cannot be
    • mapAsBuffer

      public MappedByteBuffer mapAsBuffer(long offset, int length) throws IOException
      Description copied from class: Resource
      Returns the mapped view of this resource as a buffer. If the resource is not mappable, an exception is thrown; use Resource.isMappable() to determine whether mapping this resource can succeed. It is unspecified whether mapping past the end of the file is supported; an exception may be thrown in this case.

      If this method succeeds, the returned buffer is a read-only memory-mapped view of the resource data. Depending on the OS implementation, updates to the backing file may or may not cause the mapped view to be updated at the same time.

      Overrides:
      mapAsBuffer in class Resource
      Parameters:
      offset - the offset into the resource to map
      length - the number of bytes to map
      Returns:
      the mapped view of this resource as a buffer
      Throws:
      IOException - if there is a mapping error, or if the resource is not mappable
    • asString

      public String asString(Charset charset) throws IOException
      Description copied from class: Resource
      Returns the resource content as a string.
      Overrides:
      asString in class Resource
      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()
      Description copied from class: Resource
      Returns the modification time of the resource, or null if the time is unknown.
      Overrides:
      modifiedTime in class Resource
      Returns:
      the modification time of the resource, or null if the time is unknown
    • createdTime

      public Instant createdTime()
      Description copied from class: Resource
      Returns the creation time of the resource, or null if the time is unknown.
      Overrides:
      createdTime in class Resource
      Returns:
      the creation time of the resource, or null if the time is unknown
    • size

      public long size()
      Description copied from class: Resource
      Returns the size of the resource, or -1 if the size is not known.
      Specified by:
      size in class Resource
      Returns:
      the size of the resource, or -1 if the size is not known