Holger's
Java API

com.antelmann.net
Class Downloader

java.lang.Object
  extended by com.antelmann.net.Downloader
All Implemented Interfaces:
Runnable

public class Downloader
extends Object
implements Runnable

Downloads a URL or URLCache object into a file. You can either download the URL synchronously using the provided static method or asynchronously in a separate thread. When doing the latter, you can first instanciate this Runnable, register a listener (in case you want to be notified upon completion) and then start the thread. If in a separate thread, you can also monitor the progress of the download using getReadSoFar() or cancel the download.

Author:
Holger Antelmann
See Also:
JDownloader

Nested Class Summary
static interface Downloader.Listener
           
 
Field Summary
static int LENGTH_NOT_OBTAINED_YET
           
static int THREAD_NOT_STARTED_YET
           
static int UNKNOWN_LENGTH
           
 
Constructor Summary
Downloader(URLCache uc, File file)
           
Downloader(URLCache uc, OutputStream out)
          closes the stream on completion
Downloader(URLCache uc, OutputStream out, boolean closeOnCompletion)
           
Downloader(URLCache uc, OutputStream out, boolean closeOnCompletion, Monitor monitor)
           
Downloader(URL url, File file)
           
Downloader(URL url, OutputStream out)
          closes the stream on completion
 
Method Summary
 void addListener(Downloader.Listener listener)
           
 void cancel()
          cancels the current download; the listeners will be called with an IOException containing the message that canced() was called or that the input stream was closed
static void download(URLCache uc, File file)
          synchronously downloads the given uc to the given file
static void download(URLCache uc, OutputStream out)
          synchronously downloads the given url to the given output stream
static void download(URLCache uc, OutputStream out, String user, char[] password)
          synchronously downloads the given url to the given output stream using basic authorization mechanism
static void download(URL url, File file)
          synchronously downloads the given url to the given file
static void download(URL url, OutputStream out)
          synchronously downloads the given url to the given output stream
 int getBufferSize()
           
 boolean getCloseOnCompletion()
           
 int getContentLength()
          returns the content length or LENGTH_NOT_OBTAINED_YET if the download hasn't been started, yet; UNKNOWN_LENGTH is returned if the length is unknown
 OutputStream getOutputStream()
           
 int getReadSoFar()
          returns the bytes that have been read by the thread; returns THREAD_NOT_STARTED_YET if the thread hasn't been started, yet
 URL getURL()
           
 void run()
          downloads the URL to the file and calls any listeners upon completion
 void setBasicAuthorization(String user, char[] password)
          if the user is non-null, the InputStream will be obtained via basic authorization
 void setBufferSize(int size)
           
 void setCloseOnCompletion(boolean flag)
           
 void setMonitor(Monitor monitor)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNKNOWN_LENGTH

public static final int UNKNOWN_LENGTH
See Also:
Constant Field Values

LENGTH_NOT_OBTAINED_YET

public static final int LENGTH_NOT_OBTAINED_YET
See Also:
Constant Field Values

THREAD_NOT_STARTED_YET

public static final int THREAD_NOT_STARTED_YET
See Also:
Constant Field Values
Constructor Detail

Downloader

public Downloader(URL url,
                  File file)
           throws FileNotFoundException
Throws:
FileNotFoundException

Downloader

public Downloader(URLCache uc,
                  File file)
           throws FileNotFoundException
Throws:
FileNotFoundException

Downloader

public Downloader(URL url,
                  OutputStream out)
closes the stream on completion


Downloader

public Downloader(URLCache uc,
                  OutputStream out)
closes the stream on completion


Downloader

public Downloader(URLCache uc,
                  OutputStream out,
                  boolean closeOnCompletion)

Downloader

public Downloader(URLCache uc,
                  OutputStream out,
                  boolean closeOnCompletion,
                  Monitor monitor)
Method Detail

setMonitor

public void setMonitor(Monitor monitor)

setBasicAuthorization

public void setBasicAuthorization(String user,
                                  char[] password)
if the user is non-null, the InputStream will be obtained via basic authorization


setCloseOnCompletion

public void setCloseOnCompletion(boolean flag)
                          throws IllegalStateException
Throws:
IllegalStateException

getCloseOnCompletion

public boolean getCloseOnCompletion()

download

public static void download(URL url,
                            File file)
                     throws IOException
synchronously downloads the given url to the given file

Throws:
IOException

download

public static void download(URLCache uc,
                            File file)
                     throws IOException
synchronously downloads the given uc to the given file

Throws:
IOException

download

public static void download(URL url,
                            OutputStream out)
                     throws IOException
synchronously downloads the given url to the given output stream

Throws:
IOException

download

public static void download(URLCache uc,
                            OutputStream out)
                     throws IOException
synchronously downloads the given url to the given output stream

Throws:
IOException

download

public static void download(URLCache uc,
                            OutputStream out,
                            String user,
                            char[] password)
                     throws IOException
synchronously downloads the given url to the given output stream using basic authorization mechanism

Throws:
IOException

cancel

public void cancel()
cancels the current download; the listeners will be called with an IOException containing the message that canced() was called or that the input stream was closed


setBufferSize

public void setBufferSize(int size)
                   throws IllegalStateException
Throws:
IllegalStateException

getBufferSize

public int getBufferSize()

run

public void run()
downloads the URL to the file and calls any listeners upon completion

Specified by:
run in interface Runnable
See Also:
Downloader.Listener, addListener(Downloader.Listener)

getOutputStream

public OutputStream getOutputStream()

getURL

public URL getURL()

getReadSoFar

public int getReadSoFar()
returns the bytes that have been read by the thread; returns THREAD_NOT_STARTED_YET if the thread hasn't been started, yet


getContentLength

public int getContentLength()
returns the content length or LENGTH_NOT_OBTAINED_YET if the download hasn't been started, yet; UNKNOWN_LENGTH is returned if the length is unknown


addListener

public void addListener(Downloader.Listener listener)


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads