public interface Resource
from(URI)), classpath (from(String)), filesystem (from(Path),
fromPath(String)), content in config (from(Config, String), input stream(from(InputStream, String),
or direct value (fromContent(String, byte[]), fromContent(String, String).
The resource bytes can then be accessed by various methods, depending on the type required - either you can access bytes
(getBytes(), getStream()) or String (getString(), getString(Charset)).
This class is not thread safe. If you want to use it across multiple threads,
there is an option: call cacheBytes() before accessing it by other threads.
Note that this stores all the bytes in memory, so use with care!!!| Modifier and Type | Interface | Description |
|---|---|---|
static class |
Resource.Source |
Source of a
Resource. |
| Modifier and Type | Method | Description |
|---|---|---|
void |
cacheBytes() |
Caches the resource bytes in memory, so they can be repeatedly
accessed.
|
static Optional<Resource> |
from(Config config,
String keyPrefix) |
Loads the resource from appropriate location based
on configuration and a key prefix.
|
static Resource |
from(InputStream inputStream,
String description) |
Load resource from binary content from an input stream, using
Resource.Source.UNKNOWN type. |
static Resource |
from(String resourcePath) |
Load resource from classpath.
|
static Resource |
from(URI uri) |
Load resource from URI provided.
|
static Resource |
from(URI uri,
Proxy proxy) |
Load resource from URI provided with an explicit proxy server.
|
static Resource |
from(Path fsPath) |
Load resource from file system.
|
static Resource |
fromContent(String description,
byte[] bytes) |
Load resource from binary content.
|
static Resource |
fromContent(String description,
String string) |
Load resource from text content (e.g.
|
static Resource |
fromPath(String fsPath) |
Helper method for
from(Path) so you do not have to create the path yourself. |
byte[] |
getBytes() |
Get bytes of this resource.
|
String |
getLocation() |
Location (or description) of this resource, depends on original source.
|
Resource.Source |
getSourceType() |
Type of this resource, depends on the original source.
|
InputStream |
getStream() |
Get an input stream to this resource.
|
String |
getString() |
Get string content of this resource.
|
String |
getString(Charset charset) |
Get string content of this resource.
|
static Resource from(URI uri)
getBytes() and other
content retrieval-methods.uri - Resource locationstatic Resource from(URI uri, Proxy proxy)
getBytes() and other
content retrieval-methods.uri - Resource locationproxy - HTTP proxy to use when accessing the URIstatic Resource from(String resourcePath)
getBytes() and other
content retrieval-methods.resourcePath - classpath pathstatic Resource from(Path fsPath)
getBytes() and other
content retrieval-methods.fsPath - path of file systemstatic Resource fromPath(String fsPath)
from(Path) so you do not have to create the path yourself.fsPath - String path to file systemstatic Resource fromContent(String description, byte[] bytes)
description - description of this resource (e.g. "keystore")bytes - raw bytes of this resourcestatic Resource fromContent(String description, String string)
fromContent(String, byte[]) for binary).description - description of this resource (e.g. "JWK-private")string - string content of this resource, will be transformed to bytes using UTF-8 encodingstatic Resource from(InputStream inputStream, String description)
Resource.Source.UNKNOWN type.inputStream - input stream to raw bytes of this resourcedescription - description of this resource (e.g. "keystore")static Optional<Resource> from(Config config, String keyPrefix)
config - configurationkeyPrefix - prefix of keys that may contain the location of resourceInputStream getStream()
getBytes(), or explicitly buffer
this resource cacheBytes(), you will get a new input stream to the
buffered bytes and may call this method multiple times.IllegalStateException - in case the stream was already provided in previous call and was not bufferedbyte[] getBytes()
IllegalStateException - in case the stream was already provided in previous call and was not bufferedString getString()
IllegalStateException - in case the stream was already provided in previous call and was not bufferedString getString(Charset charset)
charset - Character set (encoding) to use to decode bytesIllegalStateException - in case the stream was already provided in previous call and was not bufferedResource.Source getSourceType()
String getLocation()
fromContent(String, String)fromContent(String, byte[])from(InputStream, String)void cacheBytes()
Copyright © 2018, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.