Class InputStreamDownloadHandler

All Implemented Interfaces:
DownloadHandler, ElementRequestHandler, Serializable

public class InputStreamDownloadHandler extends AbstractDownloadHandler<InputStreamDownloadHandler>
Download handler for serving an input stream for client download.

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 Details

    • InputStreamDownloadHandler

      public InputStreamDownloadHandler(InputStreamDownloadCallback callback)
      Create an input stream download handler for given event -> response function.
      Parameters:
      callback - serializable function for handling download
    • InputStreamDownloadHandler

      public InputStreamDownloadHandler(InputStreamDownloadCallback callback, String fileNameOverride)
      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 download
      fileNameOverride - 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

      public void handleDownloadRequest(DownloadEvent downloadEvent) throws IOException
      Description copied from interface: DownloadHandler
      Method 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

      public InputStreamDownloadHandler 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 a DownloadResponse with a known content length. When the callback reports the length as -1, the whole content is sent.

      Returns:
      true if the handler is configured to answer byte range requests
    • getUrlPostfix

      public String getUrlPostfix()
      Description copied from interface: ElementRequestHandler
      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. 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 null for "".