com.limegroup.gnutella.downloader
Class HTTPDownloader

java.lang.Object
  extended bycom.limegroup.gnutella.downloader.HTTPDownloader
All Implemented Interfaces:
BandwidthTracker

public class HTTPDownloader
extends java.lang.Object
implements BandwidthTracker

Downloads a file over an HTTP connection. This class is as simple as possible. It does not deal with retries, prioritizing hosts, etc. Nor does it check whether a file already exists; it just writes over anything on disk.

It is necessary to explicitly initialize an HTTPDownloader with the connectTCP(..) followed by a connectHTTP(..) method. (Hence HTTPDownloader behaves much like Connection.) Typical use is as follows:

 HTTPDownloader dl=new HTTPDownloader(host, port);
 dl.connectTCP(timeout);
 dl.connectHTTP(startByte, stopByte);
 dl.doDownload();
 
LOCKING: _writtenGoodLocs and _goodLocs are both synchronized on _goodLocs LOCKING: _writtenBadLocs and _badLocs are both synchronized on _badLocs


Field Summary
static int BUF_LENGTH
          The length of the buffer used in downloading.
 
Constructor Summary
HTTPDownloader(RemoteFileDesc rfd, java.io.File incompleteFile)
          Creates an uninitialized client-side normal download.
HTTPDownloader(java.net.Socket socket, RemoteFileDesc rfd, java.io.File incompleteFile)
          Creates an uninitialized server-side push download.
 
Method Summary
 boolean browseEnabled()
           
 boolean chatEnabled()
           
 void connectHTTP(int start, int stop, boolean supportQueueing)
          Sends a GET request using an already open socket, and reads all headers.
 void connectTCP(int timeout)
          Initializes this by connecting to the remote host (in the case of a normal client-side download).
 void doDownload(VerifyingFile commonOutFile)
           
 int getAmountRead()
           
 int getAmountToRead()
           
 float getAverageBandwidth()
          Returns the overall averaged bandwidth between all calls of measureBandwidth
 java.lang.String getFileName()
           
 byte[] getGUID()
           
 long getIndex()
           
 java.net.InetAddress getInetAddress()
          Forces this to not write past the given byte of the file, if it has not already done so.
 int getInitialReadingPoint()
           
 float getMeasuredBandwidth()
          Returns the throughput of this in kilobytes/sec (KB/s) between the last two calls to measureBandwidth, or 0.0 if unknown.
 int getPort()
           
 RemoteFileDesc getRemoteFileDesc()
          Returns the RemoteFileDesc passed to this' constructor.
 int getTotalAmountRead()
           
 java.lang.String getVendor()
           
 boolean isHTTP11()
          returns true if we have think that the server supports HTTP1.1
 boolean isPush()
          Returns true iff this is a push download.
 void measureBandwidth()
          Measures the data throughput since the last call to measureBandwidth.
static java.lang.String readServer(java.lang.String serverHeader)
          Reads the Server header.
 void stop()
          Stops this immediately.
 void stopAt(int stop)
          Instructs this stop just before reading the given byte.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BUF_LENGTH

public static final int BUF_LENGTH
The length of the buffer used in downloading.

See Also:
Constant Field Values
Constructor Detail

HTTPDownloader

public HTTPDownloader(RemoteFileDesc rfd,
                      java.io.File incompleteFile)
Creates an uninitialized client-side normal download. Call connectTCP and connectHTTP() on this before any other methods. Non-blocking.

Parameters:
rfd - complete information for the file to download, including host address and port
incompleteFile - the temp file to use while downloading, which need not exist.

HTTPDownloader

public HTTPDownloader(java.net.Socket socket,
                      RemoteFileDesc rfd,
                      java.io.File incompleteFile)
Creates an uninitialized server-side push download. connectTCP() and connectHTTP() on this before any other methods. Non-blocking.

Parameters:
socket - the socket to download from. The "GIV..." line must have been read from socket. HTTP headers may not have been read or buffered -- this can be null
rfd - complete information for the file to download, including host address and port
incompleteFile - the temp file to use while downloading, which need not exist.
Method Detail

connectTCP

public void connectTCP(int timeout)
                throws java.io.IOException
Initializes this by connecting to the remote host (in the case of a normal client-side download). Blocks for up to timeout milliseconds trying to connect, unless timeout is zero, in which case there is no timeout. This MUST be uninitialized, i.e., connectTCP may not be called more than once.

Parameters:
timeout - the timeout to use for connecting, in milliseconds, or zero if no timeout
Throws:
CantConnectException - could not establish a TCP connection
java.io.IOException

connectHTTP

public void connectHTTP(int start,
                        int stop,
                        boolean supportQueueing)
                 throws java.io.IOException,
                        TryAgainLaterException,
                        FileNotFoundException,
                        NotSharingException,
                        QueuedException,
                        RangeNotAvailableException,
                        ProblemReadingHeaderException,
                        UnknownCodeException
Sends a GET request using an already open socket, and reads all headers.

Parameters:
start - The byte at which the HTTPDownloader should begin
stop - the index just past the last byte to read; stop-1 is the last byte the HTTPDownloader should download

Throws:
TryAgainLaterException - the host is busy
FileNotFoundException - the host doesn't recognize the file
NotSharingException - the host isn't sharing files (BearShare)
java.io.IOException - miscellaneous error
QueuedException - uploader has queued us
RangeNotAvailableException - uploader has ranges other than requested
ProblemReadingHeaderException - could not parse headers
UnknownCodeException - unknown response code

readServer

public static java.lang.String readServer(java.lang.String serverHeader)
Reads the Server header. All information after the ':' is considered to be the Server.


doDownload

public void doDownload(VerifyingFile commonOutFile)
                throws java.io.IOException
Throws:
java.io.IOException

stop

public void stop()
Stops this immediately. This method is always safe to call.


stopAt

public void stopAt(int stop)
Instructs this stop just before reading the given byte.

Parameters:
stop - the index just past the last byte to read; stop-1 is the index of the last byte to be downloaded

getInitialReadingPoint

public int getInitialReadingPoint()

getAmountRead

public int getAmountRead()

getTotalAmountRead

public int getTotalAmountRead()

getAmountToRead

public int getAmountToRead()

getInetAddress

public java.net.InetAddress getInetAddress()
Forces this to not write past the given byte of the file, if it has not already done so. Typically this is called to reduce the download window; doing otherwise will typically result in incomplete downloads.


chatEnabled

public boolean chatEnabled()

browseEnabled

public boolean browseEnabled()

getVendor

public java.lang.String getVendor()

getIndex

public long getIndex()

getFileName

public java.lang.String getFileName()

getGUID

public byte[] getGUID()

getPort

public int getPort()

getRemoteFileDesc

public RemoteFileDesc getRemoteFileDesc()
Returns the RemoteFileDesc passed to this' constructor.


isPush

public boolean isPush()
Returns true iff this is a push download.


isHTTP11

public boolean isHTTP11()
returns true if we have think that the server supports HTTP1.1


measureBandwidth

public void measureBandwidth()
Description copied from interface: BandwidthTracker
Measures the data throughput since the last call to measureBandwidth. This value can be read by calling getMeasuredBandwidth.

Specified by:
measureBandwidth in interface BandwidthTracker

getMeasuredBandwidth

public float getMeasuredBandwidth()
                           throws InsufficientDataException
Description copied from interface: BandwidthTracker
Returns the throughput of this in kilobytes/sec (KB/s) between the last two calls to measureBandwidth, or 0.0 if unknown.

Specified by:
getMeasuredBandwidth in interface BandwidthTracker
Throws:
InsufficientDataException

getAverageBandwidth

public float getAverageBandwidth()
Description copied from interface: BandwidthTracker
Returns the overall averaged bandwidth between all calls of measureBandwidth

Specified by:
getAverageBandwidth in interface BandwidthTracker

toString

public java.lang.String toString()