UploadDataProvider

public abstract class UploadDataProvider 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.

Summary

Public constructors

Public methods

void

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

abstract long

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 to the beginning.

Public constructors

UploadDataProvider

public UploadDataProvider()

Public methods

close

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
java.io.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.

getLength

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
long

the length of the upload for non-chunked uploads, -1 otherwise.

Throws
java.io.IOException

if any IOException occurred during the process.

read

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 on success or onReadError 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.

Note: For non-chunked uploads, onReadSucceeded should be called only when the read has succeeded and at least one byte has been read into byteBuffer. For chunked uploads (The size of the data is unknown), then it is allowed to call onReadSucceeded for the last chunk with an empty byte buffer.

Parameters
UploadDataSink uploadDataSink

The object to notify when the read has completed.

ByteBuffer byteBuffer

The buffer to copy the read bytes into. Do not change byteBuffer's limit.

Throws
java.io.IOException

if any IOException occurred during the process. onFailed will be called with the thrown exception set as the cause of the CallbackException.

rewind

public abstract void rewind(UploadDataSink uploadDataSink)

Rewinds upload data to the beginning. Invoked when Cronet requires the upload data provider to be in an equivalent state to as if read was never called yet.

To signal that the operation has finished, implementations of this function must call onRewindSucceeded to indicate success, or onRewindError on failure. Even if the associated UrlRequest is canceled, one or the other must still be called before resources can be safely freed. Throwing an exception from the method is equivalent to calling onRewindError. If rewinding is not supported (for instance, if reading from a one-off stream), this should call onRewindError immediately.

The implementer can safely assume that neither read nor a concurrent rewind call will be issued until they notify the sink that rewinding has finished, as described in the previous paragraph.

This method is used internally by Cronet if the body needs to be uploaded multiple times. This can occur in many different situations, for instance when following redirects, or when retrying requests after a timeout or a network disconnect. Note that while implementing rewinding is generally optional, requests which end up requiring it will fail unless rewinding is implemented.

Parameters
UploadDataSink uploadDataSink

The object to notify when the rewind operation has completed, successfully or otherwise.

Throws
java.io.IOException

if any IOException occurred during the process. onFailed will be called with the thrown exception set as the cause of the CallbackException.