Class TLcdFailureCachingInputStreamFactory
- All Implemented Interfaces:
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.
- Since:
- 2022.1
-
Constructor Summary
ConstructorDescriptionTLcdFailureCachingInputStreamFactory
(ILcdInputStreamFactory aDelegate, long aRetryPeriod, TimeUnit aTimeUnit) Creates a new instance that decorates the delegate. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canCreateInputStream
(String aSource) Checks whether this input stream factory can provide access toaSource
.createInputStream
(String aSource) Creates anInputStream
fromaSource
.
-
Constructor Details
-
TLcdFailureCachingInputStreamFactory
public TLcdFailureCachingInputStreamFactory(ILcdInputStreamFactory aDelegate, long aRetryPeriod, TimeUnit aTimeUnit) Creates a new instance that decorates the delegate.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.
- Parameters:
aDelegate
- the delegateaRetryPeriod
- the retry period, strictly positiveaTimeUnit
- unit ofaRetryPeriod
-
-
Method Details
-
canCreateInputStream
Description copied from interface:ILcdInputStreamFactory
Checks whether this input stream factory can provide access toaSource
. It is acceptable for this method to returntrue
for a source whilecreateInputStream
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 thataSource
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 ofcreateInputStream
which would have to throw exceptions for unsupported sources.- Specified by:
canCreateInputStream
in interfaceILcdInputStreamFactory
- Parameters:
aSource
- the data source to be verified; typically a file name or a URL.- Returns:
true
if this input stream factory can likely access the source,false
otherwise.- See Also:
-
createInputStream
Description copied from interface:ILcdInputStreamFactory
Creates anInputStream
fromaSource
.- Specified by:
createInputStream
in interfaceILcdInputStreamFactory
- Parameters:
aSource
- an accessible data source.- Returns:
- an
InputStream
from the data source. - Throws:
IOException
- whenaSource
is not accepted bycanCreateInputStream
or is not accessible.- See Also:
-