com.google.api.client.googleapis.media
Class MediaHttpDownloader

java.lang.Object
  extended by com.google.api.client.googleapis.media.MediaHttpDownloader

public final class MediaHttpDownloader
extends Object

Media HTTP Downloader, with support for both direct and resumable media downloads. Documentation is available here.

Implementation is not thread-safe.

Since:
1.9
Author:
[email protected] (Ravi Mistry)

Nested Class Summary
static class MediaHttpDownloader.DownloadState
          Download state associated with the Media HTTP downloader.
 
Field Summary
static int MAXIMUM_CHUNK_SIZE
          Default maximum number of bytes that will be downloaded from the server in any single HTTP request.
 
Constructor Summary
MediaHttpDownloader(HttpTransport transport, HttpRequestInitializer httpRequestInitializer)
          Construct the MediaHttpDownloader.
 
Method Summary
 void download(GenericUrl requestUrl, OutputStream outputStream)
          Executes a direct media download or a resumable media download.
 int getChunkSize()
          Returns the maximum size of individual chunks that will get downloaded by single HTTP requests.
 MediaHttpDownloader.DownloadState getDownloadState()
          Gets the current download state of the downloader.
 long getNumBytesDownloaded()
          Gets the total number of bytes downloaded by this downloader.
 double getProgress()
          Gets the download progress denoting the percentage of bytes that have been downloaded, represented between 0.0 (0%) and 1.0 (100%).
 MediaHttpDownloaderProgressListener getProgressListener()
          Returns the progress listener to send progress notifications to or null for none.
 HttpTransport getTransport()
          Returns the transport to use for requests.
 boolean isBackOffPolicyEnabled()
          Returns whether the back off policy is enabled or disabled.
 boolean isDirectDownloadEnabled()
          Returns whether direct media download is enabled or disabled.
 MediaHttpDownloader setBackOffPolicyEnabled(boolean backOffPolicyEnabled)
          Sets whether the back off policy is enabled or disabled.
 MediaHttpDownloader setBytesDownloaded(long bytesDownloaded)
          Sets the total number of bytes that have been downloaded of the media resource.
 MediaHttpDownloader setChunkSize(int chunkSize)
          Sets the maximum size of individual chunks that will get downloaded by single HTTP requests.
 MediaHttpDownloader setDirectDownloadEnabled(boolean directDownloadEnabled)
          Returns whether direct media download is enabled or disabled.
 MediaHttpDownloader setProgressListener(MediaHttpDownloaderProgressListener progressListener)
          Sets the progress listener to send progress notifications to or null for none.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXIMUM_CHUNK_SIZE

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.

See Also:
Constant Field Values
Constructor Detail

MediaHttpDownloader

public MediaHttpDownloader(HttpTransport transport,
                           HttpRequestInitializer httpRequestInitializer)
Construct the MediaHttpDownloader.

Parameters:
transport - The transport to use for requests
httpRequestInitializer - The initializer to use when creating an HttpRequest or null for none
Method Detail

download

public void download(GenericUrl requestUrl,
                     OutputStream outputStream)
              throws IOException
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 must be instantiated before download called be called again.

Parameters:
requestUrl - The request URL where the download requests will be sent
outputStream - destination output stream
Throws:
IOException

setBytesDownloaded

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.

This method is only applicable for resumable media download.

Parameters:
bytesDownloaded - The total number of bytes downloaded

isDirectDownloadEnabled

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.


setDirectDownloadEnabled

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.


setProgressListener

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


getProgressListener

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


setBackOffPolicyEnabled

public MediaHttpDownloader setBackOffPolicyEnabled(boolean backOffPolicyEnabled)
Sets whether the back off policy is enabled or disabled. If value is set to false then server errors are not handled and the download process will fail if a server error is encountered. Defaults to true.


isBackOffPolicyEnabled

public boolean isBackOffPolicyEnabled()
Returns whether the back off policy is enabled or disabled. If value is set to false then server errors are not handled and the download process will fail if a server error is encountered. Defaults to true.


getTransport

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


setChunkSize

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.

The maximum allowable value is MAXIMUM_CHUNK_SIZE.


getChunkSize

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.


getNumBytesDownloaded

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

Returns:
the number of bytes downloaded

getDownloadState

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

Returns:
the download state

getProgress

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:
the download progress


Copyright © 2010-2012 Google. All Rights Reserved.