org.jscience.net
Class Downloader

java.lang.Object
  extended by org.jscience.net.Downloader
All Implemented Interfaces:
java.lang.Runnable

public class Downloader
extends java.lang.Object
implements java.lang.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.

See Also:
JDownloader

Nested Class Summary
static interface Downloader.Listener
          DOCUMENT ME!
 
Field Summary
static int LENGTH_NOT_OBTAINED_YET
          DOCUMENT ME!
static int THREAD_NOT_STARTED_YET
          DOCUMENT ME!
static int UNKNOWN_LENGTH
          DOCUMENT ME!
 
Constructor Summary
Downloader(URLCache uc, java.io.File file)
          Creates a new Downloader object.
Downloader(URLCache uc, java.io.OutputStream out)
          closes the stream on completion
Downloader(URLCache uc, java.io.OutputStream out, boolean closeOnCompletion)
          Creates a new Downloader object.
Downloader(java.net.URL url, java.io.File file)
          Creates a new Downloader object.
Downloader(java.net.URL url, java.io.OutputStream out)
          closes the stream on completion
 
Method Summary
 void addListener(Downloader.Listener listener)
          DOCUMENT ME!
 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, java.io.File file)
          synchronously downloads the given uc to the given file
static void download(URLCache uc, java.io.OutputStream out)
          synchronously downloads the given url to the given output stream
static void download(java.net.URL url, java.io.File file)
          synchronously downloads the given url to the given file
static void download(java.net.URL url, java.io.OutputStream out)
          synchronously downloads the given url to the given output stream
 int getBufferSize()
          DOCUMENT ME!
 boolean getCloseOnCompletion()
          DOCUMENT ME!
 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
 java.io.OutputStream getOutputStream()
          DOCUMENT ME!
 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
 java.net.URL getURL()
          DOCUMENT ME!
 void run()
          downloads the URL to the file and calls any listeners upon completion
 void setBufferSize(int size)
          DOCUMENT ME!
 void setCloseOnCompletion(boolean flag)
          DOCUMENT ME!
 
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
DOCUMENT ME!

See Also:
Constant Field Values

LENGTH_NOT_OBTAINED_YET

public static final int LENGTH_NOT_OBTAINED_YET
DOCUMENT ME!

See Also:
Constant Field Values

THREAD_NOT_STARTED_YET

public static final int THREAD_NOT_STARTED_YET
DOCUMENT ME!

See Also:
Constant Field Values
Constructor Detail

Downloader

public Downloader(java.net.URL url,
                  java.io.File file)
           throws java.io.FileNotFoundException
Creates a new Downloader object.

Parameters:
url - DOCUMENT ME!
file - DOCUMENT ME!
Throws:
java.io.FileNotFoundException - DOCUMENT ME!

Downloader

public Downloader(URLCache uc,
                  java.io.File file)
           throws java.io.FileNotFoundException
Creates a new Downloader object.

Parameters:
uc - DOCUMENT ME!
file - DOCUMENT ME!
Throws:
java.io.FileNotFoundException - DOCUMENT ME!

Downloader

public Downloader(java.net.URL url,
                  java.io.OutputStream out)
closes the stream on completion


Downloader

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


Downloader

public Downloader(URLCache uc,
                  java.io.OutputStream out,
                  boolean closeOnCompletion)
Creates a new Downloader object.

Parameters:
uc - DOCUMENT ME!
out - DOCUMENT ME!
closeOnCompletion - DOCUMENT ME!
Method Detail

setCloseOnCompletion

public void setCloseOnCompletion(boolean flag)
                          throws java.lang.IllegalStateException
DOCUMENT ME!

Parameters:
flag - DOCUMENT ME!
Throws:
java.lang.IllegalStateException - DOCUMENT ME!

getCloseOnCompletion

public boolean getCloseOnCompletion()
DOCUMENT ME!

Returns:
DOCUMENT ME!

download

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

Parameters:
url - DOCUMENT ME!
file - DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

download

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

Parameters:
uc - DOCUMENT ME!
file - DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

download

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

Parameters:
url - DOCUMENT ME!
out - DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

download

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

Parameters:
uc - DOCUMENT ME!
out - DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

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 java.lang.IllegalStateException
DOCUMENT ME!

Parameters:
size - DOCUMENT ME!
Throws:
java.lang.IllegalStateException - DOCUMENT ME!

getBufferSize

public int getBufferSize()
DOCUMENT ME!

Returns:
DOCUMENT ME!

run

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

Specified by:
run in interface java.lang.Runnable
Throws:
java.lang.IllegalStateException - DOCUMENT ME!
See Also:
Downloader.Listener, addListener(Downloader.Listener)

getOutputStream

public java.io.OutputStream getOutputStream()
DOCUMENT ME!

Returns:
DOCUMENT ME!

getURL

public java.net.URL getURL()
DOCUMENT ME!

Returns:
DOCUMENT ME!

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

Returns:
DOCUMENT ME!

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

Returns:
DOCUMENT ME!

addListener

public void addListener(Downloader.Listener listener)
DOCUMENT ME!

Parameters:
listener - DOCUMENT ME!