public class

MediaHttpDownloader

extends Object
java.lang.Object
   ↳ com.kinvey.java.core.MediaHttpDownloader

Class Overview

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

Implementation is not thread-safe.

Summary

Nested Classes
enum MediaHttpDownloader.DownloadState Download state associated with the Media HTTP downloader. 
class MediaHttpDownloader.DownloadUrlResponse  
Constants
int MAXIMUM_CHUNK_SIZE Default maximum number of bytes that will be downloaded from the server in any single HTTP request.
Public Constructors
MediaHttpDownloader(HttpTransport transport, HttpRequestInitializer httpRequestInitializer)
Construct the MediaHttpDownloader.
Public Methods
void download(AbstractKinveyClientRequest initiationClientRequest, OutputStream out)
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 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.
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%).
DownloaderProgressListener 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 setContentRange(long firstBytePos, int lastBytePos)
Sets the content range of the next download request.
MediaHttpDownloader setDirectDownloadEnabled(boolean directDownloadEnabled)
Returns whether direct media download is enabled or disabled.
MediaHttpDownloader setProgressListener(DownloaderProgressListener progressListener)
Sets the progress listener to send progress notifications to or null for none.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

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.

Constant Value: 33554432 (0x02000000)

Public Constructors

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

Public Methods

public void download (AbstractKinveyClientRequest initiationClientRequest, OutputStream out)

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
initiationClientRequest request object used to request unique uri from kinvey
out output stream to dump bytes as they stream off the wire
Throws
IOException

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.

public MediaHttpDownloader.DownloadState getDownloadState ()

Gets the current download state of the downloader.

Returns
  • the download state

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
  • the last byte position

public long getNumBytesDownloaded ()

Gets the total number of bytes downloaded by this downloader.

Returns
  • the number of bytes downloaded

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

public DownloaderProgressListener getProgressListener ()

Returns the progress listener to send progress notifications to or null for none.

public HttpTransport getTransport ()

Returns the transport to use for requests.

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.

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.

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.

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(long, int) if you need to specify both the bytes downloaded and the last byte position.

Parameters
bytesDownloaded The total number of bytes downloaded

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.

public MediaHttpDownloader setContentRange (long firstBytePos, int 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(long) if you only need to specify the first byte position.

Parameters
firstBytePos The first byte position in the content range string
lastBytePos The last byte position in the content range string.

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.

public MediaHttpDownloader setProgressListener (DownloaderProgressListener progressListener)

Sets the progress listener to send progress notifications to or null for none.