Package com.dropbox.core
Class DbxDownloader<R>
- java.lang.Object
-
- com.dropbox.core.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:
Example usingFileOutputStream out = new FileOutputStream("test.txt"); try { response = downloader.download(out); } finally { out.close(); }
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 givenOutputStream
and returns the server response.R
download(java.io.OutputStream out, IOUtil.ProgressListener progressListener)
This method is the same asdownload(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 theInputStream
containing the response body bytes.R
getResult()
Returns the server response.
-
-
-
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 theInputStream
containing the response body bytes. Remember to callclose()
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 (seeclose()
)- See Also:
download(OutputStream)
-
download
public R download(java.io.OutputStream out) throws DbxException, java.io.IOException
Downloads the response body to the givenOutputStream
and returns the server response. This method manages closing this downloader's resources, so no further calls toclose()
are necessary. The underlyingInputStream
returned bygetInputStream()
will be closed by this method. This method is the equivalent oftry { 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 bodyjava.io.IOException
- if an error occurs writing the response body to the output stream.java.lang.IllegalStateException
- if this downloader has already been closed (seeclose()
)
-
download
public R download(java.io.OutputStream out, IOUtil.ProgressListener progressListener) throws DbxException, java.io.IOException
This method is the same asdownload(OutputStream)
except for allowing to track download progress.- Parameters:
out
-OutputStream
to write response body toprogressListener
-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 togetInputStream()
will fail.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
-