public final class TLcdFailureCachingInputStreamFactory extends Object implements ILcdInputStreamFactory
ILcdInputStreamFactory
(the delegate), and
remembers for which sources the delegate failed to create an InputStream
.
When asked to createInputStream(String)
, it checks whether a previous attempt "recently" failed.
If there is a recent failure, the exception from that previous attempt is thrown.
If there is no such recent failure, the delegate is asked to create an InputStream
for the source.
The amount of time considered "recent" is configured via the constructor arguments.
Recent failures do not affect canCreateInputStream(String)
: it always asks the delegate.
Wrapping an ILcdInputStreamFactory
is useful when you expect that many input streams will be created
for the same source(s), and it is likely that some of them are not available. As an example, this happens when
lazily decoding a model consisting of many files, and you do not know beforehand which files actually exist.
This class is thread-safe if the delegate is.
Constructor and Description |
---|
TLcdFailureCachingInputStreamFactory(ILcdInputStreamFactory aDelegate,
long aRetryPeriod,
TimeUnit aTimeUnit)
Creates a new instance that decorates the delegate.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canCreateInputStream(String aSource)
Checks whether this input stream factory can provide access to
aSource . |
InputStream |
createInputStream(String aSource)
Creates an
InputStream from aSource . |
public TLcdFailureCachingInputStreamFactory(ILcdInputStreamFactory aDelegate, long aRetryPeriod, TimeUnit aTimeUnit)
The retry period controls how long this input stream factory will wait before attempting to connect to a source that previously failed. If attempts are made before the retry period has passed, the previous exception will be rethrown without trying to connect.
aDelegate
- the delegateaRetryPeriod
- the retry period, strictly positiveaTimeUnit
- unit of aRetryPeriod
public boolean canCreateInputStream(String aSource)
ILcdInputStreamFactory
aSource
. It is acceptable for this method to
return true
for a source while createInputStream
throws an exception for that same source.
For performance reasons, we strongly recommend that implementations perform only a simple test.
For example: check the structure of aSource
to see if it is a URI with a specific scheme, but do not verify
that aSource
really is accessible.
The default implementation of this method returns true
.
Make sure to override this method if you can easily reject sources. This can help avoid pointless invocations of
createInputStream
which would have to throw exceptions for unsupported sources.
canCreateInputStream
in interface ILcdInputStreamFactory
aSource
- the data source to be verified; typically a file name or a URL.true
if this input stream factory can likely access the source, false
otherwise.ILcdInputStreamFactory.createInputStream(String)
public InputStream createInputStream(String aSource) throws IOException
ILcdInputStreamFactory
InputStream
from aSource
.createInputStream
in interface ILcdInputStreamFactory
aSource
- an accessible data source.InputStream
from the data source.IOException
- when aSource
is not accepted by canCreateInputStream
or is not accessible.ILcdInputStreamFactory.canCreateInputStream(String)