Package com.dropbox.core
Class DbxUploader<R,E,X extends DbxApiException>
java.lang.Object
com.dropbox.core.DbxUploader<R,E,X>
- Type Parameters:
R- response type returned by server on request successX- exception type returned by server on request failure
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
AlphaUploadUploader,DocsCreateUploader,DocsUpdateUploader,PaperCreateUploader,PaperUpdateUploader,UploadSessionAppendBatchUploader,UploadSessionAppendUploader,UploadSessionAppendV2Uploader,UploadSessionFinishUploader,UploadSessionStartUploader,UploadUploader
public abstract class DbxUploader<R,E,X extends DbxApiException>
extends Object
implements Closeable
Class for completing upload requests.
This class provides methods for uploading a request body and handling the server response.
Example usage:
FileInputStream in = new FileInputStream("test.txt");
try {
response = uploader.uploadAndFinish(in);
} finally {
in.close();
}
Example using getOutputStream():
try {
OutputStream out = uploader.getOutputStream();
out.write(data);
response = uploader.finish();
} finally {
uploader.close();
}
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDbxUploader(HttpRequestor.Uploader httpUploader, StoneSerializer<R> responseSerializer, StoneSerializer<E> errorSerializer, String userId) -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Aborts this upload request and closes its underlying connection.voidclose()Closes this upload request and releases its underlying resources.finish()Completes the request and returns response from the server.Returns anOutputStreamthat writes to the request body.getOutputStream(IOUtil.ProgressListener progressListener) Returns anOutputStreamthat writes to the request body.protected abstract XnewException(DbxWrappedException error) Uploads all bytes read from the givenInputStreamand returns the response.uploadAndFinish(InputStream in, long limit) Uploads up tolimitbytes read from the givenInputStreamand returns the response.uploadAndFinish(InputStream in, long limit, IOUtil.ProgressListener progressListener) This method is the same asuploadAndFinish(InputStream, long)except for it allows tracking the upload progress.uploadAndFinish(InputStream in, IOUtil.ProgressListener progressListener) This method is the same asuploadAndFinish(InputStream, long)except for it allow tracking the upload progress.
-
Constructor Details
-
DbxUploader
protected DbxUploader(HttpRequestor.Uploader httpUploader, StoneSerializer<R> responseSerializer, StoneSerializer<E> errorSerializer, String userId)
-
-
Method Details
-
newException
-
uploadAndFinish
Uploads all bytes read from the givenInputStreamand returns the response. This method manages closing this uploader's resources, so no further calls toclose()are necessary. The underlyingOutputStreamreturned bygetOutputStream()will be closed by this method. This method is the equivalent oftry { OutputStream out = uploader.getOutputStream(); // read from in, write to out response = uploader.finish(); } finally { uploader.close(); }- Parameters:
in-InputStreamcontaining data to upload- Returns:
- Response from server
- Throws:
X- if the server sent an error response for the requestDbxException- if an error occurs uploading the data or reading the responseIOException- if an error occurs reading the input stream.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())
-
uploadAndFinish
public R uploadAndFinish(InputStream in, IOUtil.ProgressListener progressListener) throws X, DbxException, IOException This method is the same asuploadAndFinish(InputStream, long)except for it allow tracking the upload progress.- Parameters:
in-InputStreamcontaining data to uploadprogressListener-OUtil.ProgressListenerto track the upload progress. Only support OKHttpRequester and StandardHttpRequester.- Returns:
- Response from server
- Throws:
X- if the server sent an error response for the requestDbxException- if an error occurs uploading the data or reading the responseIOException- if an error occurs reading the input stream.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())
-
uploadAndFinish
Uploads up tolimitbytes read from the givenInputStreamand returns the response. This method upload bytes from the givenInputStreamuntillimitbytes have been read or end-of-stream is detected. UseuploadAndFinish(InputStream)to upload the entire stream. This method manages closing this uploader's resources, so no further calls toclose()are necessary. The underlyingOutputStreamreturned bygetOutputStream()will be closed by this method. This method is the equivalent oftry { OutputStream out = uploader.getOutputStream(); // read at most `limit` bytes from in, write to out response = uploader.finish(); } finally { uploader.close(); }- Parameters:
in-InputStreamcontaining data to uploadlimit- Maximum number of bytes to read from the givenInputStream- Returns:
- Response from server
- Throws:
X- if the server sent an error response for the requestDbxException- if an error occurs uploading the data or reading the responseIOException- if an error occurs reading the input stream.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())
-
uploadAndFinish
public R uploadAndFinish(InputStream in, long limit, IOUtil.ProgressListener progressListener) throws X, DbxException, IOException This method is the same asuploadAndFinish(InputStream, long)except for it allows tracking the upload progress.- Parameters:
in-InputStreamcontaining data to uploadlimit- Maximum number of bytes to read from the givenInputStreamprogressListener-OUtil.ProgressListenerto track the upload progress. Only support OKHttpRequester and StandardHttpRequester.- Returns:
- Response from server
- Throws:
X- if the server sent an error response for the requestDbxException- if an error occurs uploading the data or reading the responseIOException- if an error occurs reading the input stream.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())
-
close
public void close()Closes this upload request and releases its underlying resources. This method should always be called to allow for proper resource deallocation.try { OutputStream out = uploader.getOutputStream(); out.write(data); response = uploader.finish(); } finally { uploader.close(); }- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
abort
public void abort()Aborts this upload request and closes its underlying connection. -
getOutputStream
Returns anOutputStreamthat writes to the request body. Remember to callfinish()to complete the request and retrieve the response. Data written to this stream will be uploaded. Typically you will not need this method and can use the more convenientuploadAndFinish(InputStream).- Returns:
- Request body output stream.
- Throws:
IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())- See Also:
-
getOutputStream
Returns anOutputStreamthat writes to the request body. Remember to callfinish()to complete the request and retrieve the response. Data written to this stream will be uploaded. Typically you will not need this method and can use the more convenientuploadAndFinish(InputStream).- Parameters:
progressListener-IOUtil.ProgressListenerto track the upload progress. Only support OKHttpRequester and StandardHttpRequester.- Returns:
- Request body output stream.
- Throws:
IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())- See Also:
-
finish
Completes the request and returns response from the server. This method should be called after writing data to the uploadOutputStream(seegetOutputStream()).- Returns:
- Response from server
- Throws:
X- if the server sent an error response for the requestDbxException- if an error occurs sending the upload or reading the responseIllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())
-