Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions) Summary: [Ctors](https://developer.android.com/reference/android/net/http/UploadDataProvider#pubctors) \| [Methods](https://developer.android.com/reference/android/net/http/UploadDataProvider#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/net/http/UploadDataProvider#inhmethods)

# UploadDataProvider

*** ** * ** ***


`
public


abstract
class
UploadDataProvider
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/io/Closeable


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.net.http.UploadDataProvider |

<br />

*** ** * ** ***

Abstract class allowing the embedder to provide an upload body to `https://developer.android.com/reference/android/net/http/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 ||
|---|---|
| ` https://developer.android.com/reference/android/net/http/UploadDataProvider#UploadDataProvider()() ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/net/http/UploadDataProvider#close()() ` Called when this UploadDataProvider is no longer needed by a request, so that resources (like a file) can be explicitly released. |
| ` abstract long` | ` https://developer.android.com/reference/android/net/http/UploadDataProvider#getLength()() ` If this is a non-chunked upload, returns the length of the upload. |
| ` abstract void` | ` https://developer.android.com/reference/android/net/http/UploadDataProvider#read(android.net.http.UploadDataSink,%20java.nio.ByteBuffer)(https://developer.android.com/reference/android/net/http/UploadDataSink uploadDataSink, https://developer.android.com/reference/java/nio/ByteBuffer byteBuffer) ` Reads upload data into `byteBuffer`. |
| ` abstract void` | ` https://developer.android.com/reference/android/net/http/UploadDataProvider#rewind(android.net.http.UploadDataSink)(https://developer.android.com/reference/android/net/http/UploadDataSink uploadDataSink) ` Rewinds upload data. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||
| From interface ` https://developer.android.com/reference/java/io/Closeable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/io/Closeable#close()() ` Closes this stream and releases any system resources associated with it. | ||
| From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Public constructors

### UploadDataProvider

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)

```
public UploadDataProvider ()
```

<br />

<br />

## Public methods

### close

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void close ()
```

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

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/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

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)

```
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.

<br />

| Returns ||
|---|---|
| `long` | the length of the upload for non-chunked uploads, -1 otherwise. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if any IOException occurred during the process. |

### read

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)

```
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`: `https://developer.android.com/reference/android/net/http/UploadDataSink#onReadSucceeded(boolean)` on success or `https://developer.android.com/reference/android/net/http/UploadDataSink#onReadError(java.lang.Exception)` on failure. Neither read nor rewind will be called until one of
those methods or the other is called. Even if the associated `https://developer.android.com/reference/android/net/http/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.

<br />

| Parameters ||
|---|---|
| `uploadDataSink` | `UploadDataSink`: The object to notify when the read has completed, successfully or otherwise. This value cannot be `null`. <br /> |
| `byteBuffer` | `ByteBuffer`: The buffer to copy the read bytes into. Do not change byteBuffer's limit. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if any IOException occurred during the process. `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException)` will be called with the thrown exception set as the cause of the `https://developer.android.com/reference/android/net/http/CallbackException`. |

### rewind

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)

```
public abstract void rewind (UploadDataSink uploadDataSink)
```

Rewinds upload data. Each call must be followed be a single call, either synchronous or
asynchronous, to `uploadDataSink`: `https://developer.android.com/reference/android/net/http/UploadDataSink#onRewindSucceeded()` on success
or
`https://developer.android.com/reference/android/net/http/UploadDataSink#onRewindError(java.lang.Exception)` on failure. Neither read nor rewind will be called until
one of those methods or the other is called. Even if the associated `https://developer.android.com/reference/android/net/http/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
`https://developer.android.com/reference/android/net/http/UploadDataSink#onRewindError(java.lang.Exception)`. Note that rewinding is required to follow redirects
that preserve the upload body, and for retrying when the server times out stale sockets.

<br />

| Parameters ||
|---|---|
| `uploadDataSink` | `UploadDataSink`: The object to notify when the rewind operation has completed, successfully or otherwise. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if any IOException occurred during the process. `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException)` will be called with the thrown exception set as the cause of the `https://developer.android.com/reference/android/net/http/CallbackException`. |