The Download Class
The Download class is the workhorse of the Download Manager. Its
primary purpose is to download a file and save that file’s contents to disk. Each
time a new download is added to the Download Manager, a new Download object is instantiated to handle
the download. The Download Manager has the ability to download multiple files
at once.
The Download Variables
Download begins by declaring several static final variables that specify
the various constants used by the class. Next, four instance variables are
declared. The url variable holds the
Internet URL for the file being downloaded; the size variable holds the size of the download file in bytes; the downloaded variable holds the number of bytes
that have been downloaded thus far; and the status variable indicates the download’s current status.
The Download Constructor
Download’s constructor is passed a reference to the URL to
download in the form of a URL object, which is assigned
to the url instance variable. It
then sets the remaining instance variables
to their initial states and calls the download( ) method. Notice that size is set to –1 to indicate there is no size yet.
The download( ) Method
The download( ) method creates a new Thread object, passing it a reference to the invoking Download instance. To use threads, the Download class simply implements the Runnable interface by overriding the run( ) method. After the download( ) method has instantiated a new Thread instance, passing its constructor the Runnable Download class, it calls the thread’s start( ) method. Invoking the start( ) method causes the Runnable instance’s (the Download class’) run( ) method to be executed.
The run( ) Method
run( ) sets up variables for the network stream that the
download’s contents will
be read from and sets up the file
that the download’s contents will be written to. Next, a
connection to the download’s URL is
opened by calling url.openConnection(
).
Since we know that the Download Manager supports only HTTP downloads, the
connection is cast to the HttpURLConnection
type. After the HttpURLConnection has been created, the
connection request property is set by calling connection. setRequestProperty( ).
Class
URLConnection
The abstract class
URLConnection is the superclass of all classes that represent a communications link
between the application and a URL. Instances of this class can be used both to
read from and to write to the resource referenced by the URL. In general,
creating a connection to a URL is a multistep process:- The
connection object is created by invoking the openConnection method on a
URL.
- The
setup parameters and general request properties are manipulated.
- The
actual connection to the remote object is made, using the connect method.
- The
remote object becomes available. The header fields and the contents of the
remote object can be accessed.
Ø setRequestProperty
public void setRequestProperty(String key,String value)
Sets the general request property. If a property with the key already
exists, overwrite its value with the new value.
Ø getContentLength
public int getContentLength()
Returns the value of the
content-length header field.
RandomAccessFile
public RandomAccessFile(File file,String mode)
|
"rw"
|
Open for reading and writing.
If the file does not already exist then an attempt will be made to create it.
|
The
stateChanged( ) Method
Each time the Download class needs to notify its registered Observers of a change, the stateChanged( ) method is invoked. The stateChanged( ) method first calls the Observable class’ setChanged( ) method to flag the class as having
been changed. Next, the stateChanged( ) method calls Observable’s notifyObservers( ) method, which broadcasts the change
notification to the registered Observers.
No comments:
Post a Comment