public class AutoCloseAsyncContentProvider extends Object implements AsyncContentProvider
A wrapper around another AsyncContentProviders that closes the provider instance and the
optionally specified additional Closeable objects when the reading from the provider is
completed. Completion means either AsyncContentProvider.onSuccess(Runnable) or
AsyncContentProvider.onError(java.util.function.Consumer) execution.
Please note that if there an exception during closing the provider in onSuccess, an
ClosingProviderAutomaticallyException is received by the onError listener that the programmer might
only want to log as the success and its listener ran successfully.
In case an exception is thrown during closing the provider and other resources in the onError listener, it is only logged out.
| Constructor and Description |
|---|
AutoCloseAsyncContentProvider(AsyncContentProvider wrapped,
Closeable... additionalCloseables)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closing the content provider.
|
Optional<Long> |
getContentLength()
The size of the data that is provided or
Optional.empty() if the length is unknown in
advance. |
Optional<MediaType> |
getContentType()
The media type of the content or
Optional.empty() if the media type is unknown. |
Optional<Throwable> |
getFailure()
Returns the error that has happened during processing the content of this provider or
Optional.empty() if there was no failure. |
boolean |
isClosed()
Returns whether this provider is closed or not.
|
AsyncContentProvider |
onContent(AsyncContentListener listener)
Registering a listener that will be notified if new content is available.
|
AsyncContentProvider |
onError(Consumer<Throwable> action)
Registering a listener that is called if there is an error delivering the data or processing of
the data is failed either via
AsyncCallback.failed(Throwable) call or via an error
during calling the registered listener of AsyncContentProvider.onContent(AsyncContentListener) or
AsyncContentProvider.onSuccess(Runnable) functions. |
AsyncContentProvider |
onSuccess(Runnable action)
Registering an action that will be called when all of the chunks are delivered.
|
public AutoCloseAsyncContentProvider(AsyncContentProvider wrapped, Closeable... additionalCloseables)
wrapped - The instance of this wrapper copies the functionality of the wrapped provider and in
the end of process (success or fail) closes the wrapped provider automatically.additionalCloseables - Zero or more additional objects that implement Closeable interface and that
should be closed in the end of processing the data of the wrapped provider (fail or
success). In case of calling close() on the
AutoCloseAsyncContentProvider also closes all passed additiona closeable
objects.public void close()
AsyncContentProviderclose in interface Closeableclose in interface AutoCloseableclose in interface AsyncContentProviderpublic Optional<Long> getContentLength()
AsyncContentProviderOptional.empty() if the length is unknown in
advance.getContentLength in interface AsyncContentProviderpublic Optional<MediaType> getContentType()
AsyncContentProviderOptional.empty() if the media type is unknown.getContentType in interface AsyncContentProviderpublic Optional<Throwable> getFailure()
AsyncContentProviderOptional.empty() if there was no failure.getFailure in interface AsyncContentProviderpublic boolean isClosed()
AsyncContentProviderisClosed in interface AsyncContentProviderpublic AsyncContentProvider onContent(AsyncContentListener listener)
AsyncContentProvideronContent in interface AsyncContentProviderlistener - The listener that is called when new data chunk is available.AsyncContentProvider.public AsyncContentProvider onError(Consumer<Throwable> action)
AsyncContentProviderAsyncCallback.failed(Throwable) call or via an error
during calling the registered listener of AsyncContentProvider.onContent(AsyncContentListener) or
AsyncContentProvider.onSuccess(Runnable) functions.onError in interface AsyncContentProvideraction - The action that must be executed if there is an error.AsyncContentProvider.public AsyncContentProvider onSuccess(Runnable action)
AsyncContentProviderAsyncContentProvider.onError(Consumer) is called with that exception.onSuccess in interface AsyncContentProvideraction - The action that is called when all of the data chunks are delivered.AsyncContentProvider.Copyright © 2011–2019 Everit Kft.. All rights reserved.