2.9.3 (latest) 2.9.2 2.8.1 2.7.2 2.6.0 2.5.1 2.4.1 2.1.2 2.0.1 1.35.2 1.34.1 1.33.4

```java
public final class MediaHttpDownloader
```
Media HTTP Downloader, with support for both direct and resumable media downloads. Documentation
is available [here](https://developers.google.com/api-client-library/java/google-api-java-client/media-download).

Implementation is not thread-safe.

Back-off is disabled by default. To enable it for an abnormal HTTP response and an I/O
exception you should call HttpRequest#setUnsuccessfulResponseHandler with a new HttpBackOffUnsuccessfulResponseHandler instance and HttpRequest#setIOExceptionHandler
with HttpBackOffIOExceptionHandler.

Upgrade warning: in prior version 1.14 exponential back-off was enabled by default for an
abnormal HTTP response. Starting with version 1.15 it's disabled by default.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> MediaHttpDownloader

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Fields

### MAXIMUM_CHUNK_SIZE

```java
public static final int MAXIMUM_CHUNK_SIZE
```
Default maximum number of bytes that will be downloaded from the server in any single HTTP
request. Set to 32MB because that is the maximum App Engine request size.

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `int` |   |

## Constructors

### MediaHttpDownloader(HttpTransport transport, HttpRequestInitializer httpRequestInitializer)

```java
public MediaHttpDownloader(HttpTransport transport, HttpRequestInitializer httpRequestInitializer)
```
Construct the [MediaHttpDownloader](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `transport` | `com.google.api.client.http.HttpTransport` The transport to use for requests |
| `httpRequestInitializer` | `com.google.api.client.http.HttpRequestInitializer` The initializer to use when creating an HttpRequest or `null` for none |

## Methods

### download(GenericUrl requestUrl, HttpHeaders requestHeaders, OutputStream outputStream)

```java
public void download(GenericUrl requestUrl, HttpHeaders requestHeaders, OutputStream outputStream)
```
Executes a direct media download or a resumable media download.

This method does not close the given output stream.

This method is not reentrant. A new instance of [MediaHttpDownloader](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt) must be
instantiated before download called be called again.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `requestUrl` | `com.google.api.client.http.GenericUrl` request URL where the download requests will be sent |
| `requestHeaders` | `com.google.api.client.http.HttpHeaders` request headers or `null` to ignore |
| `outputStream` | `OutputStream` destination output stream |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `IOException` |   |

### download(GenericUrl requestUrl, OutputStream outputStream)

```java
public void download(GenericUrl requestUrl, OutputStream outputStream)
```
Executes a direct media download or a resumable media download.

This method does not close the given output stream.

This method is not reentrant. A new instance of [MediaHttpDownloader](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt) must be
instantiated before download called be called again.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `requestUrl` | `com.google.api.client.http.GenericUrl` The request URL where the download requests will be sent |
| `outputStream` | `OutputStream` destination output stream |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `IOException` |   |

### getChunkSize()

```java
public int getChunkSize()
```
Returns the maximum size of individual chunks that will get downloaded by single HTTP requests.
The default value is [#MAXIMUM_CHUNK_SIZE](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt#com_google_api_client_googleapis_media_MediaHttpDownloader_MAXIMUM_CHUNK_SIZE).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `int` |   |

### getDownloadState()

```java
public MediaHttpDownloader.DownloadState getDownloadState()
```
Gets the current download state of the downloader.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader.DownloadState` | the download state |

### getLastBytePosition()

```java
public long getLastBytePosition()
```
Gets the last byte position of the media file we want to download or `-1` if there is no
upper limit on the byte position.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `long` | the last byte position |

### getNumBytesDownloaded()

```java
public long getNumBytesDownloaded()
```
Gets the total number of bytes downloaded by this downloader.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `long` | the number of bytes downloaded |

### getProgress()

```java
public double getProgress()
```
Gets the download progress denoting the percentage of bytes that have been downloaded,
represented between 0.0 (0%) and 1.0 (100%).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `double` | the download progress |

### getProgressListener()

```java
public MediaHttpDownloaderProgressListener getProgressListener()
```
Returns the progress listener to send progress notifications to or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloaderProgressListener` |   |

### getTransport()

```java
public HttpTransport getTransport()
```
Returns the transport to use for requests.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpTransport` |   |

### isDirectDownloadEnabled()

```java
public boolean isDirectDownloadEnabled()
```
Returns whether direct media download is enabled or disabled. If value is set to `true`
then a direct download will be done where the whole media content is downloaded in a single
request. If value is set to `false` then the download uses the resumable media download
protocol to download in data chunks. Defaults to `false`.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `boolean` |   |

### setBytesDownloaded(long bytesDownloaded)

```java
public MediaHttpDownloader setBytesDownloaded(long bytesDownloaded)
```
Sets the total number of bytes that have been downloaded of the media resource.

If a download was aborted mid-way due to a connection failure then users can resume the
download from the point where it left off.

Use [#setContentRange](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt#com_google_api_client_googleapis_media_MediaHttpDownloader_setContentRange_) if you need to specify both the bytes downloaded and the last
byte position.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `bytesDownloaded` | `long` The total number of bytes downloaded |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader` |   |

### setChunkSize(int chunkSize)

```java
public MediaHttpDownloader setChunkSize(int chunkSize)
```
Sets the maximum size of individual chunks that will get downloaded by single HTTP requests.
The default value is [#MAXIMUM_CHUNK_SIZE](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt#com_google_api_client_googleapis_media_MediaHttpDownloader_MAXIMUM_CHUNK_SIZE).

The maximum allowable value is [#MAXIMUM_CHUNK_SIZE](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt#com_google_api_client_googleapis_media_MediaHttpDownloader_MAXIMUM_CHUNK_SIZE).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `chunkSize` | `int` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader` |   |

### setContentRange(long firstBytePos, int lastBytePos) (deprecated)

```java
public MediaHttpDownloader setContentRange(long firstBytePos, int lastBytePos)
```
**Deprecated.** *Use [#setContentRange(long, long)](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt#com_google_api_client_googleapis_media_MediaHttpDownloader_setContentRange_long_long_) instead.*

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `firstBytePos` | `long` |
| `lastBytePos` | `int` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader` |   |

### setContentRange(long firstBytePos, long lastBytePos)

```java
public MediaHttpDownloader setContentRange(long firstBytePos, long lastBytePos)
```
Sets the content range of the next download request. Eg: bytes=firstBytePos-lastBytePos.

If a download was aborted mid-way due to a connection failure then users can resume the
download from the point where it left off.

Use [#setBytesDownloaded](https://docs.cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.media.MediaHttpDownloader.md.txt#com_google_api_client_googleapis_media_MediaHttpDownloader_setBytesDownloaded_) if you only need to specify the first byte position.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `firstBytePos` | `long` The first byte position in the content range string |
| `lastBytePos` | `long` The last byte position in the content range string. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader` |   |

### setDirectDownloadEnabled(boolean directDownloadEnabled)

```java
public MediaHttpDownloader setDirectDownloadEnabled(boolean directDownloadEnabled)
```
Returns whether direct media download is enabled or disabled. If value is set to `true`
then a direct download will be done where the whole media content is downloaded in a single
request. If value is set to `false` then the download uses the resumable media download
protocol to download in data chunks. Defaults to `false`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `directDownloadEnabled` | `boolean` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader` |   |

### setProgressListener(MediaHttpDownloaderProgressListener progressListener)

```java
public MediaHttpDownloader setProgressListener(MediaHttpDownloaderProgressListener progressListener)
```
Sets the progress listener to send progress notifications to or `null` for none.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `progressListener` | `MediaHttpDownloaderProgressListener` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MediaHttpDownloader` |   |