Class InputStreamDownloadHandler
- All Implemented Interfaces:
DownloadHandler,ElementRequestHandler,Serializable
Byte range requests, which media players use to seek and browsers use to
resume a download, are not answered by default. Every range request calls the
callback again for a new stream, and the stream has to be read from its start
up to the range: unless the stream can seek, such as a
FileInputStream, each seek or resume reads all the bytes
before it again, which adds up for large media that a player fetches in many
small ranges. Ranges are also only correct if the callback returns the same
content every time, which only the application knows. Call
enableRangeRequests() when that holds, or use
DownloadHandler.forFile(java.io.File), which answers ranges by
seeking in the file.
- Since:
- 24.8
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate an input stream download handler for given event -> response function.InputStreamDownloadHandler(InputStreamDownloadCallback callback, String fileNameOverride) Create an input stream download handler for given event -> response function. -
Method Summary
Modifier and TypeMethodDescriptionEnables answering byte range requests, which media players use to seek and browsers use to resume a download.Optional URL postfix allows appending an application-controlled string, e.g. the logical name of the target file, to the end of the otherwise random-looking download URL.voidhandleDownloadRequest(DownloadEvent downloadEvent) Method that is called when the client wants to download from the url stored for this specific handler registration.booleanReturns whether this handler answers byte range requests, which media players use to seek and browsers use to resume a download.Methods inherited from class com.vaadin.flow.server.streams.AbstractDownloadHandler
getContentType, getContentType, getTransferContext, inline, isInline, resolveContentType, withContentTypeMethods inherited from class com.vaadin.flow.server.streams.TransferProgressAwareHandler
addTransferProgressListener, getListeners, notifyError, onProgress, onProgress, onProgress, onProgress, setTransferUI, uiAccess, whenComplete, whenComplete, whenStart, whenStartMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.server.streams.DownloadHandler
allowDisabled, handleRequestMethods inherited from interface com.vaadin.flow.server.streams.ElementRequestHandler
getDisabledUpdateMode, isAllowInert
-
Constructor Details
-
InputStreamDownloadHandler
Create an input stream download handler for given event -> response function.- Parameters:
callback- serializable function for handling download
-
InputStreamDownloadHandler
Create an input stream download handler for given event -> response function.The downloaded file fileNameOverride and download URL postfix will be set to
fileNameOverride.- Parameters:
callback- serializable function for handling downloadfileNameOverride- used as a downloaded file name as a download request URL postfix, e.g./VAADIN/dynamic/resource/0/5298ee8b-9686-4a5a-ae1d-b38c62767d6a/my-file.txt- Since:
- 25.2
-
-
Method Details
-
handleDownloadRequest
Description copied from interface:DownloadHandlerMethod that is called when the client wants to download from the url stored for this specific handler registration.Once this method has been called, the download is no longer bound to the owner component's lifecycle and is not interrupted by detaching the owner or by closing the browser tab, see the
class-level documentation.- Parameters:
downloadEvent- download event containing the necessary data for writing the response- Throws:
IOException- if an IO error occurred during download
-
enableRangeRequests
Enables answering byte range requests, which media players use to seek and browsers use to resume a download. Safari does not play audio or video without them.Only enable them if the callback returns the same content on every call, with a known content length: the callback is called for every range request, and a range that a client joins with a range of other content ends up as corrupt data. The bytes before a range are skipped from the returned stream, which is a seek for a stream that supports it, such as a
FileInputStream, but reads them for other streams.- Returns:
- this instance for method chaining
-
isRangeRequestsEnabled
public boolean isRangeRequestsEnabled()Returns whether this handler answers byte range requests, which media players use to seek and browsers use to resume a download. Safari does not play audio or video from a handler that does not answer them.A handler that does not answer them sends the whole content for every request. A handler that does may still send the whole content for a particular response, for example when the length of its content is not known. The default implementation returns
false.Range requests are answered after
enableRangeRequests(), but only for aDownloadResponsewith a known content length. When the callback reports the length as-1, the whole content is sent.- Returns:
trueif the handler is configured to answer byte range requests
-
getUrlPostfix
Description copied from interface:ElementRequestHandlerOptional URL postfix allows appending an application-controlled string, e.g. the logical name of the target file, to the end of the otherwise random-looking download URL. If defined, requests that would otherwise be routable are still rejected if the postfix is missing or invalid. Postfix changes the last segment in the resource url.- Returns:
- String optional URL postfix, or
nullfor "".
-