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:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
AlphaUploadUploader,DocsCreateUploader,DocsUpdateUploader,PaperCreateUploader,PaperUpdateUploader,UploadSessionAppendUploader,UploadSessionAppendV2Uploader,UploadSessionFinishUploader,UploadSessionStartUploader,UploadUploader
public abstract class DbxUploader<R,E,X extends DbxApiException> extends java.lang.Object implements java.io.CloseableClass for completing upload requests. This class provides methods for uploading a request body and handling the server response. Example usage:
Example usingFileInputStream in = new FileInputStream("test.txt"); try { response = uploader.uploadAndFinish(in); } finally { in.close(); }getOutputStream():try { OutputStream out = uploader.getOutputStream(); out.write(data); response = uploader.finish(); } finally { uploader.close(); }
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedDbxUploader(HttpRequestor.Uploader httpUploader, StoneSerializer<R> responseSerializer, StoneSerializer<E> errorSerializer, java.lang.String userId)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidabort()Aborts this upload request and closes its underlying connection.voidclose()Closes this upload request and releases its underlying resources.Rfinish()Completes the request and returns response from the server.java.io.OutputStreamgetOutputStream()Returns anOutputStreamthat writes to the request body.java.io.OutputStreamgetOutputStream(IOUtil.ProgressListener progressListener)Returns anOutputStreamthat writes to the request body.protected abstract XnewException(DbxWrappedException error)RuploadAndFinish(java.io.InputStream in)Uploads all bytes read from the givenInputStreamand returns the response.RuploadAndFinish(java.io.InputStream in, long limit)Uploads up tolimitbytes read from the givenInputStreamand returns the response.RuploadAndFinish(java.io.InputStream in, long limit, IOUtil.ProgressListener progressListener)This method is the same asuploadAndFinish(InputStream, long)except for it allows tracking the upload progress.RuploadAndFinish(java.io.InputStream in, IOUtil.ProgressListener progressListener)This method is the same asuploadAndFinish(InputStream, long)except for it allow tracking the upload progress.
-
-
-
Constructor Detail
-
DbxUploader
protected DbxUploader(HttpRequestor.Uploader httpUploader, StoneSerializer<R> responseSerializer, StoneSerializer<E> errorSerializer, java.lang.String userId)
-
-
Method Detail
-
newException
protected abstract X newException(DbxWrappedException error)
-
uploadAndFinish
public R uploadAndFinish(java.io.InputStream in) throws X extends DbxApiException, DbxException, java.io.IOException
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 responsejava.io.IOException- if an error occurs reading the input stream.java.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())X extends DbxApiException
-
uploadAndFinish
public R uploadAndFinish(java.io.InputStream in, IOUtil.ProgressListener progressListener) throws X extends DbxApiException, DbxException, java.io.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 responsejava.io.IOException- if an error occurs reading the input stream.java.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())X extends DbxApiException
-
uploadAndFinish
public R uploadAndFinish(java.io.InputStream in, long limit) throws X extends DbxApiException, DbxException, java.io.IOException
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 responsejava.io.IOException- if an error occurs reading the input stream.java.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())X extends DbxApiException
-
uploadAndFinish
public R uploadAndFinish(java.io.InputStream in, long limit, IOUtil.ProgressListener progressListener) throws X extends DbxApiException, DbxException, java.io.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 responsejava.io.IOException- if an error occurs reading the input stream.java.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())X extends DbxApiException
-
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 interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
abort
public void abort()
Aborts this upload request and closes its underlying connection.
-
getOutputStream
public java.io.OutputStream 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:
java.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())- See Also:
uploadAndFinish(InputStream)
-
getOutputStream
public java.io.OutputStream getOutputStream(IOUtil.ProgressListener progressListener)
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:
java.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())- See Also:
uploadAndFinish(InputStream)
-
finish
public R finish() throws X extends DbxApiException, DbxException
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 responsejava.lang.IllegalStateException- if this uploader has already been closed (seeclose()) or finished (seefinish())X extends DbxApiException
-
-