java.lang.Object
io.smallrye.common.resource.Resource
- Direct Known Subclasses:
JarFileResource,MemoryResource,PathResource,URLResource
A handle to a loadable resource, which usually will come from a JAR or the filesystem.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasBuffer()Returns the bytes of this resource, as a read-only byte buffer.Returns the resource content as a string.Returns the list of code signers for this resource.longcopyTo(OutputStream destination) Copy the bytes of this resource to the given destination.longcopyTo(WritableByteChannel channel) Copy the bytes of this resource to the given destination.longCopy the bytes of this resource to the given destination.booleanReturns true if this resource represents a directory, or false otherwise.Returns the modification time of the resource, ornullif the time is unknown.Open a directory stream to read the contents of this directory.abstract InputStreamOpen an input stream to read this resource.final StringpathName()Returns the resource's relative path.<R> RreadStream(Function<InputStream, R> function) Perform the given action on the input stream of this resource.abstract longsize()Returns the size of the resource, or-1if the size is not known.abstract URLurl()Returns the resource URL (notnull).
-
Constructor Details
-
Resource
Construct a new instance.- Parameters:
path- the resource path (must not benull)
-
-
Method Details
-
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
Returns the resource URL (notnull). If the resource location information cannot be converted to a URL, an exception may be thrown.- Returns:
- the resource URL (not
null)
-
openStream
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
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 benull)- 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 ofUncheckedIOException
-
openDirectoryStream
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
Returns the bytes of this resource, as a read-only byte buffer. The buffer is suitable for passing toClassLoader.defineClass(String, ByteBuffer, ProtectionDomain). The default implementation reads all of the resource bytes from the stream returned byopenStream(). 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 readOutOfMemoryError- if the size of the resource is greater than the maximum allowed size of a buffer
-
copyTo
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 benull)- Returns:
- the number of bytes copied
- Throws:
IOException- if the copy fails
-
copyTo
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 benull)- Returns:
- the number of bytes copied
- Throws:
IOException- if the copy fails
-
copyTo
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 benulland must not be non-blocking)- Returns:
- the number of bytes copied
- Throws:
IOException- if the copy fails
-
asString
Returns the resource content as a string.- Parameters:
charset- the character set to use for decoding (must not benull)- Returns:
- the resource content as a string
- Throws:
IOException- if the content could not be read
-
modifiedTime
Returns the modification time of the resource, ornullif the time is unknown.- Returns:
- the modification time of the resource, or
nullif the time is unknown
-
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-1if the size is not known.- Returns:
- the size of the resource, or
-1if the size is not known
-