Package com.dropbox.core
Class DbxDownloader<R>
java.lang.Object
com.dropbox.core.DbxDownloader<R>
- All Implemented Interfaces:
Closeable,AutoCloseable
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
ConstructorsConstructorDescriptionDbxDownloader(R result, InputStream body) DbxDownloader(R result, InputStream body, String contentType) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this downloader and releases its underlying resources.download(OutputStream out) Downloads the response body to the givenOutputStreamand returns the server response.download(OutputStream out, IOUtil.ProgressListener progressListener) This method is the same asdownload(OutputStream)except for allowing to track download progress.Returns the value of the content-type header field.Returns theInputStreamcontaining the response body bytes.Returns the server response.
-
Constructor Details
-
DbxDownloader
-
DbxDownloader
-
-
Method Details
-
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
Returns the value of the content-type header field.- Returns:
- the content type, or null if not known.
-
getInputStream
Returns theInputStreamcontaining the response body bytes. Remember to callclose()after reading the stream to properly free up resources.- Returns:
- Response body input stream.
- Throws:
IllegalStateException- if this downloader has already been closed (seeclose())- See Also:
-
download
Downloads the response body to the givenOutputStreamand returns the server response. This method manages closing this downloader's resources, so no further calls toclose()are necessary. The underlyingInputStreamreturned 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-OutputStreamto write response body to- Returns:
- Response from server
- Throws:
DbxException- if an error occurs reading the response or response bodyIOException- if an error occurs writing the response body to the output stream.IllegalStateException- if this downloader has already been closed (seeclose())
-
download
public R download(OutputStream out, IOUtil.ProgressListener progressListener) throws DbxException, IOException This method is the same asdownload(OutputStream)except for allowing to track download progress.- Parameters:
out-OutputStreamto write response body toprogressListener-IOUtil.ProgressListenerto 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 togetInputStream()will fail.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-