Join us on the livestream at Android Dev Summit on 7-8 November 2018, starting at 10AM PDT!

UploadDataProvider

public abstract class UploadDataProvider extends Object
implements Closeable

Abstract class allowing the embedder to provide an upload body to UrlRequest. It supports both non-chunked (size known in advanced) and chunked (size not known in advance) uploads. Be aware that not all servers support chunked uploads.

An upload is either always chunked, across multiple uploads if the data ends up being sent more than once, or never chunked.

Public Constructor Summary

Public Method Summary

void
close()
Called when this UploadDataProvider is no longer needed by a request, so that resources (like a file) can be explicitly released.
abstract long
getLength()
If this is a non-chunked upload, returns the length of the upload.
abstract void
read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer)
Reads upload data into byteBuffer.
abstract void
rewind(UploadDataSink uploadDataSink)
Rewinds upload data.

Inherited Method Summary

Public Constructors

public UploadDataProvider ()

Public Methods

public void close ()

Called when this UploadDataProvider is no longer needed by a request, so that resources (like a file) can be explicitly released.

Throws
IOException if any IOException occurred during the process. This will cause the request to fail if it is not yet complete; otherwise it will be logged.

public abstract long getLength ()

If this is a non-chunked upload, returns the length of the upload. Must always return -1 if this is a chunked upload.

Returns
  • the length of the upload for non-chunked uploads, -1 otherwise.
Throws
IOException if any IOException occurred during the process.

public abstract void read (UploadDataSink uploadDataSink, ByteBuffer byteBuffer)

Reads upload data into byteBuffer. Upon completion, the buffer's position is updated to the end of the bytes that were read. The buffer's limit is not changed. Each call of this method must be followed be a single call, either synchronous or asynchronous, to uploadDataSink: onReadSucceeded(boolean) on success or onReadError(Exception) on failure. Neither read nor rewind will be called until one of those methods or the other is called. Even if the associated UrlRequest is canceled, one or the other must still be called before resources can be safely freed. Throwing an exception will also result in resources being freed and the request being errored out.

Parameters
uploadDataSink The object to notify when the read has completed, successfully or otherwise.
byteBuffer The buffer to copy the read bytes into. Do not change byteBuffer's limit.
Throws
IOException if any IOException occurred during the process. onFailed(UrlRequest, UrlResponseInfo, CronetException) will be called with the thrown exception set as the cause of the CallbackException.

public abstract void rewind (UploadDataSink uploadDataSink)

Rewinds upload data. Each call must be followed be a single call, either synchronous or asynchronous, to uploadDataSink: onRewindSucceeded() on success or onRewindError(Exception) on failure. Neither read nor rewind will be called until one of those methods or the other is called. Even if the associated UrlRequest is canceled, one or the other must still be called before resources can be safely freed. Throwing an exception will also result in resources being freed and the request being errored out.

If rewinding is not supported, this should call onRewindError(Exception). Note that rewinding is required to follow redirects that preserve the upload body, and for retrying when the server times out stale sockets.

Parameters
uploadDataSink The object to notify when the rewind operation has completed, successfully or otherwise.
Throws
IOException if any IOException occurred during the process. onFailed(UrlRequest, UrlResponseInfo, CronetException) will be called with the thrown exception set as the cause of the CallbackException.