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 and Description |
|---|---|
static class |
Resource.Source
Source of a
Resource. |
| Modifier and Type | Method and Description |
|---|---|
void |
cacheBytes()
Caches the resource bytes in memory, so they can be repeatedly
accessed.
|
static java.util.Optional<Resource> |
from(Config config,
java.lang.String keyPrefix)
Loads the resource from appropriate location based
on configuration and a key prefix.
|
static Resource |
from(java.io.InputStream inputStream,
java.lang.String description)
Load resource from binary content from an input stream, using
Resource.Source.UNKNOWN type. |
static Resource |
from(java.nio.file.Path fsPath)
Load resource from file system.
|
static Resource |
from(java.lang.String resourcePath)
Load resource from classpath.
|
static Resource |
from(java.net.URI uri)
Load resource from URI provided.
|
static Resource |
from(java.net.URI uri,
java.net.Proxy proxy)
Load resource from URI provided with an explicit proxy server.
|
static Resource |
fromContent(java.lang.String description,
byte[] bytes)
Load resource from binary content.
|
static Resource |
fromContent(java.lang.String description,
java.lang.String string)
Load resource from text content (e.g.
|
static Resource |
fromPath(java.lang.String fsPath)
Helper method for
from(Path) so you do not have to create the path yourself. |
byte[] |
getBytes()
Get bytes of this resource.
|
java.lang.String |
getLocation()
Location (or description) of this resource, depends on original source.
|
Resource.Source |
getSourceType()
Type of this resource, depends on the original source.
|
java.io.InputStream |
getStream()
Get an input stream to this resource.
|
java.lang.String |
getString()
Get string content of this resource.
|
java.lang.String |
getString(java.nio.charset.Charset charset)
Get string content of this resource.
|
static Resource from(java.net.URI uri)
getBytes() and other
content retrieval-methods.uri - Resource locationstatic Resource from(java.net.URI uri, java.net.Proxy proxy)
getBytes() and other
content retrieval-methods.uri - Resource locationproxy - HTTP proxy to use when accessing the URIstatic Resource from(java.lang.String resourcePath)
getBytes() and other
content retrieval-methods.resourcePath - classpath pathstatic Resource from(java.nio.file.Path fsPath)
getBytes() and other
content retrieval-methods.fsPath - path of file systemstatic Resource fromPath(java.lang.String fsPath)
from(Path) so you do not have to create the path yourself.fsPath - String path to file systemstatic Resource fromContent(java.lang.String description, byte[] bytes)
description - description of this resource (e.g. "keystore")bytes - raw bytes of this resourcestatic Resource fromContent(java.lang.String description, java.lang.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(java.io.InputStream inputStream, java.lang.String description)
Resource.Source.UNKNOWN type.inputStream - input stream to raw bytes of this resourcedescription - description of this resource (e.g. "keystore")static java.util.Optional<Resource> from(Config config, java.lang.String keyPrefix)
config - configurationkeyPrefix - prefix of keys that may contain the location of resourcejava.io.InputStream 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.java.lang.IllegalStateException - in case the stream was already provided in previous call and was not bufferedbyte[] getBytes()
java.lang.IllegalStateException - in case the stream was already provided in previous call and was not bufferedjava.lang.String getString()
java.lang.IllegalStateException - in case the stream was already provided in previous call and was not bufferedjava.lang.String getString(java.nio.charset.Charset charset)
charset - Character set (encoding) to use to decode bytesjava.lang.IllegalStateException - in case the stream was already provided in previous call and was not bufferedResource.Source getSourceType()
java.lang.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.