Class DbxDownloader<R>

java.lang.Object
com.dropbox.core.DbxDownloader<R>
All Implemented Interfaces:
Closeable, AutoCloseable

public class DbxDownloader<R> extends Object implements 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 Details

    • DbxDownloader

      public DbxDownloader(R result, InputStream body, String contentType)
    • DbxDownloader

      public DbxDownloader(R result, InputStream body)
  • Method Details

    • 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 String getContentType()
      Returns the value of the content-type header field.
      Returns:
      the content type, or null if not known.
    • getInputStream

      public 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:
      IllegalStateException - if this downloader has already been closed (see close())
      See Also:
    • download

      public R download(OutputStream out) throws DbxException, 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
      IOException - if an error occurs writing the response body to the output stream.
      IllegalStateException - if this downloader has already been closed (see close())
    • download

      public R download(OutputStream out, IOUtil.ProgressListener progressListener) throws DbxException, 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.
      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 AutoCloseable
      Specified by:
      close in interface Closeable