Class DbxDownloader<R>

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class DbxDownloader<R>
    extends java.lang.Object
    implements java.io.Closeable
    Class for handling download requests. This class provides methods for downloading a request body and reading the server response. Example usage:
    
        FileOutputStream out = new FileOutputStream("test.txt");
        try {
            response = downloader.download(out);
        } finally {
            out.close();
        }
    
    Example using getInputStream():
    
        FileOutputStream out = new FileOutputStream("test.txt");
        response = downloader.getResult();
        try {
            InputStream in = downloader.getInputStream();
            // read from in, write to out
        } finally {
            downloader.close();
        }
    
    • Constructor Summary

      Constructors 
      Constructor Description
      DbxDownloader​(R result, java.io.InputStream body)  
      DbxDownloader​(R result, java.io.InputStream body, java.lang.String contentType)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this downloader and releases its underlying resources.
      R download​(java.io.OutputStream out)
      Downloads the response body to the given OutputStream and returns the server response.
      R download​(java.io.OutputStream out, IOUtil.ProgressListener progressListener)
      This method is the same as download(OutputStream) except for allowing to track download progress.
      java.lang.String getContentType()
      Returns the value of the content-type header field.
      java.io.InputStream getInputStream()
      Returns the InputStream containing the response body bytes.
      R getResult()
      Returns the server response.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DbxDownloader

        public DbxDownloader​(R result,
                             java.io.InputStream body,
                             java.lang.String contentType)
      • DbxDownloader

        public DbxDownloader​(R result,
                             java.io.InputStream body)
    • Method Detail

      • getResult

        public R getResult()
        Returns the server response. Returns the response from the server that is separate from the response body (data to be downloaded).
        Returns:
        Response from server
      • getContentType

        public java.lang.String getContentType()
        Returns the value of the content-type header field.
        Returns:
        the content type, or null if not known.
      • getInputStream

        public java.io.InputStream getInputStream()
        Returns the InputStream containing the response body bytes. Remember to call close() after reading the stream to properly free up resources.
        Returns:
        Response body input stream.
        Throws:
        java.lang.IllegalStateException - if this downloader has already been closed (see close())
        See Also:
        download(OutputStream)
      • download

        public R download​(java.io.OutputStream out)
                   throws DbxException,
                          java.io.IOException
        Downloads the response body to the given OutputStream and returns the server response. This method manages closing this downloader's resources, so no further calls to close() are necessary. The underlying InputStream returned by getInputStream() will be closed by this method. This method is the equivalent of
        
            try {
                InputStream in = downloader.getInputStream();
                // read from in, write to out
                return downloader.getResult();
            } finally {
                downloader.close();
            }
         
        Parameters:
        out - OutputStream to write response body to
        Returns:
        Response from server
        Throws:
        DbxException - if an error occurs reading the response or response body
        java.io.IOException - if an error occurs writing the response body to the output stream.
        java.lang.IllegalStateException - if this downloader has already been closed (see close())
      • download

        public R download​(java.io.OutputStream out,
                          IOUtil.ProgressListener progressListener)
                   throws DbxException,
                          java.io.IOException
        This method is the same as download(OutputStream) except for allowing to track download progress.
        Parameters:
        out - OutputStream to write response body to
        progressListener - IOUtil.ProgressListener to track the download progress.
        Returns:
        Response from server
        Throws:
        DbxException - if an error occurs reading the response or response body.
        java.io.IOException - if an error occurs writing the response body to the output stream.
      • close

        public void close()
        Closes this downloader and releases its underlying resources. After calling this method, calls to getInputStream() will fail.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable