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
ConstructorsConstructorDescriptionTLcdFailureCachingInputStreamFactory(ILcdInputStreamFactory aDelegate, long aRetryPeriod, TimeUnit aTimeUnit) Creates a new instance that decorates the delegate. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanCreateInputStream(String aSource) Checks whether this input stream factory can provide access toaSource.createInputStream(String aSource) Creates anInputStreamfromaSource.
-
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:ILcdInputStreamFactoryChecks whether this input stream factory can provide access toaSource. It is acceptable for this method to returntruefor a source whilecreateInputStreamthrows 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
aSourceto see if it is a URI with a specific scheme, but do not verify thataSourcereally 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 ofcreateInputStreamwhich would have to throw exceptions for unsupported sources.- Specified by:
canCreateInputStreamin interfaceILcdInputStreamFactory- Parameters:
aSource- the data source to be verified; typically a file name or a URL.- Returns:
trueif this input stream factory can likely access the source,falseotherwise.- See Also:
-
createInputStream
Description copied from interface:ILcdInputStreamFactoryCreates anInputStreamfromaSource.- Specified by:
createInputStreamin interfaceILcdInputStreamFactory- Parameters:
aSource- an accessible data source.- Returns:
- an
InputStreamfrom the data source. - Throws:
IOException- whenaSourceis not accepted bycanCreateInputStreamor is not accessible.- See Also:
-