Class DownloadStream

  • All Implemented Interfaces:
    Serializable

    public class DownloadStream
    extends Object
    implements Serializable
    Downloadable stream.

    Note that the methods in a DownloadStream are called without locking the session to prevent locking the session during long file downloads. If your DownloadStream uses anything from the session, you must handle the locking.

    Since:
    3.0
    Author:
    Vaadin Ltd.
    See Also:
    Serialized Form
    • Constructor Detail

      • DownloadStream

        public DownloadStream​(InputStream stream,
                              String contentType,
                              String fileName)
        Creates a new instance of DownloadStream.
    • Method Detail

      • getStream

        public InputStream getStream()
        Gets downloadable stream.
        Returns:
        output stream.
      • setStream

        public void setStream​(InputStream stream)
        Sets the stream.
        Parameters:
        stream - The stream to set
      • getContentType

        public String getContentType()
        Gets stream content type.
        Returns:
        type of the stream content.
      • setContentType

        public void setContentType​(String contentType)
        Sets stream content type.
        Parameters:
        contentType - the contentType to set
      • getFileName

        public String getFileName()
        Returns the file name.
        Returns:
        the name of the file.
      • setFileName

        public void setFileName​(String fileName)
        Sets the file name.
        Parameters:
        fileName - the file name to set.
      • setParameter

        public void setParameter​(String name,
                                 String value)
        Sets a paramater for download stream. Parameters are optional information about the downloadable stream and their meaning depends on the used adapter. For example in WebAdapter they are interpreted as HTTP response headers. If the parameters by this name exists, the old value is replaced.
        Parameters:
        name - the Name of the parameter to set.
        value - the Value of the parameter to set.
      • getParameter

        public String getParameter​(String name)
        Gets a paramater for download stream. Parameters are optional information about the downloadable stream and their meaning depends on the used adapter. For example in WebAdapter they are interpreted as HTTP response headers.
        Parameters:
        name - the Name of the parameter to set.
        Returns:
        Value of the parameter or null if the parameter does not exist.
      • getParameterNames

        public Iterator<String> getParameterNames()
        Gets the names of the parameters.
        Returns:
        Iterator of names or null if no parameters are set.
      • getCacheTime

        public long getCacheTime()
        Gets length of cache expiration time. This gives the adapter the possibility cache streams sent to the client. The caching may be made in adapter or at the client if the client supports caching. Default is DEFAULT_CACHETIME.
        Returns:
        Cache time in milliseconds
      • setCacheTime

        public void setCacheTime​(long cacheTime)
        Sets length of cache expiration time. This gives the adapter the possibility cache streams sent to the client. The caching may be made in adapter or at the client if the client supports caching. Zero or negavive value disbales the caching of this stream.
        Parameters:
        cacheTime - the cache time in milliseconds.
      • getBufferSize

        public int getBufferSize()
        Gets the size of the download buffer.
        Returns:
        int The size of the buffer in bytes.
      • setBufferSize

        public void setBufferSize​(int bufferSize)
        Sets the size of the download buffer.
        Parameters:
        bufferSize - the size of the buffer in bytes.
        Since:
        7.0
      • writeResponse

        public void writeResponse​(VaadinRequest request,
                                  VaadinResponse response)
                           throws IOException
        Writes this download stream to a Vaadin response. This takes care of setting response headers according to what is defined in this download stream (getContentType(), getCacheTime(), getFileName()) and transferring the data from the stream ( getStream()) to the response. Defined parameters ( getParameterNames()) are also included as headers in the response. If there's is a parameter named Location, a redirect (302 Moved temporarily) is sent instead of the contents of this stream.
        Parameters:
        request - the request for which the response should be written
        response - the Vaadin response to write this download stream to
        Throws:
        IOException - passed through from the Vaadin response
        Since:
        7.0
      • getContentDispositionFilename

        public static String getContentDispositionFilename​(String filename)
        Returns the filename formatted for inclusion in a Content-Disposition header. Includes both a plain version of the name and a UTF-8 version
        Parameters:
        filename - The filename to include
        Returns:
        A value for inclusion in a Content-Disposition header
        Since:
        7.4.8