Top Description Fields Constructors Methods
io.smallrye.common.resource

public abstract Class Resource

extends Object
Class Inheritance
Known Direct Subclasses
io.smallrye.common.resource.URLResource, io.smallrye.common.resource.JarFileResource, io.smallrye.common.resource.MemoryResource, io.smallrye.common.resource.PathResource
Imports
java.io.IOException, .InputStream, .OutputStream, .UncheckedIOException, java.net.URL, java.nio.ByteBuffer, java.nio.channels.SelectableChannel, .WritableByteChannel, java.nio.charset.Charset, java.nio.file.DirectoryStream, .Files, .Path, .StandardCopyOption, .StandardOpenOption, java.security.CodeSigner, .ProtectionDomain, java.time.Instant, java.util.List, java.util.function.Function, io.smallrye.common.constraint.Assert

A handle to a loadable resource, which usually will come from a JAR or the filesystem.

Field Summary

Modifier and TypeField and Description
private final String

Constructor Summary

AccessConstructor and Description
protected
Resource(final String
the resource path (must not be null)
path
)

Construct a new instance.

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer

Returns:

the bytes of this resource, as a read-only byte buffer
asBuffer
()

Returns the bytes of this resource, as a read-only byte buffer.

public String

Returns:

the resource content as a string
asString
(Charset
the character set to use for decoding (must not be null)
charset
)

Returns the resource content as a string.

public List<CodeSigner>

Returns:

the list of code signers for this resource
codeSigners
()

Returns the list of code signers for this resource.

public long

Returns:

the number of bytes copied
copyTo
(Path
the destination path (must not be null)
destination
)

Copy the bytes of this resource to the given destination.

public long

Returns:

the number of bytes copied
copyTo
(OutputStream
the destination stream (must not be null)
destination
)

Copy the bytes of this resource to the given destination.

public long

Returns:

the number of bytes copied
copyTo
(WritableByteChannel
the destination channel (must not be null and must not be non-blocking)
channel
)

Copy the bytes of this resource to the given destination.

public boolean

Returns:

true if this resource represents a directory, or false otherwise
isDirectory
()

Returns true if this resource represents a directory, or false otherwise.

public Instant

Returns:

the modification time of the resource, or null if the time is unknown
modifiedTime
()

Returns the modification time of the resource, or null if the time is unknown.

public DirectoryStream<Resource>

Returns:

the directory stream (not null)
openDirectoryStream
()

Open a directory stream to read the contents of this directory.

public abstract InputStream

Returns:

the input stream (not null)
openStream
()

Open an input stream to read this resource.

public final String

Returns:

the resource's relative path
pathName
()

Returns the resource's relative path.

public <
the type of the function result
R
>
R

Returns:

the result of the action function
readStream
(Function<InputStream, R>
an action to perform (must not be null)
function
)

Perform the given action on the input stream of this resource.

public abstract long

Returns:

the size of the resource, or -1 if the size is not known
size
()

Returns the size of the resource, or -1 if the size is not known.

public abstract URL

Returns:

the resource URL (not null)
url
()

Returns the resource URL (not null).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

pathback to summary
private final String path

Constructor Detail

Resourceback to summary
protected Resource(final String path)

Construct a new instance.

Parameters
path:String

the resource path (must not be null)

Method Detail

asBufferback to summary
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 Specification

Implementers must ensure that the returned buffer is read-only.

Returns:ByteBuffer

the bytes of this resource, as a read-only byte buffer

Exceptions
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
asStringback to summary
public String asString(Charset charset) throws IOException

Returns the resource content as a string.

Parameters
charset:Charset

the character set to use for decoding (must not be null)

Returns:String

the resource content as a string

Exceptions
IOException:
if the content could not be read
codeSignersback to summary
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:List<CodeSigner>

the list of code signers for this resource

copyToback to summary
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:Path

the destination path (must not be null)

Returns:long

the number of bytes copied

Exceptions
IOException:
if the copy fails
copyToback to summary
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:OutputStream

the destination stream (must not be null)

Returns:long

the number of bytes copied

Exceptions
IOException:
if the copy fails
copyToback to summary
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:WritableByteChannel

the destination channel (must not be null and must not be non-blocking)

Returns:long

the number of bytes copied

Exceptions
IOException:
if the copy fails
isDirectoryback to summary
public boolean isDirectory()

Returns true if this resource represents a directory, or false otherwise. Not every resource implementation supports directory access.

Returns:boolean

true if this resource represents a directory, or false otherwise

modifiedTimeback to summary
public Instant modifiedTime()

Returns the modification time of the resource, or null if the time is unknown.

Returns:Instant

the modification time of the resource, or null if the time is unknown

openDirectoryStreamback to summary
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:DirectoryStream<Resource>

the directory stream (not null)

Exceptions
IOException:
if the directory could not be opened or the resource is not a directory
openStreamback to summary
public abstract InputStream openStream() throws IOException

Open an input stream to read this resource.

Returns:InputStream

the input stream (not null)

Exceptions
IOException:
if the input stream could not be opened or the resource is a directory
pathNameback to summary
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:String

the resource's relative path

readStreamback to summary
public <R> R readStream(Function<InputStream, R> function) throws IOException

Perform the given action on the input stream of this resource.

Parameters
<R>
the type of the function result
function:Function<InputStream, R>

an action to perform (must not be null)

Returns:R

the result of the action function

Exceptions
IOException:
if the stream could not be opened, or the resource is a directory, or the action throws an instance of UncheckedIOException
sizeback to summary
public abstract long size()

Returns the size of the resource, or -1 if the size is not known.

Returns:long

the size of the resource, or -1 if the size is not known

urlback to summary
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:URL

the resource URL (not null)