org.eclipse.ecf.protocol.bittorrent
Class TorrentFile

java.lang.Object
  extended by org.eclipse.ecf.protocol.bittorrent.TorrentFile

public class TorrentFile
extends java.lang.Object

The TorrentFile class is a representation of the information stored within a .torrent file. Files can be set with the setTargetFile(File) method and then have its integrity checked against the torrent's hash sum values validate() method.


Constructor Summary
TorrentFile(java.io.File file)
          Creates a new Torrent to analyze the provided torrent file.
 
Method Summary
 boolean equals(java.lang.Object other)
          Returns whether this TorrentFile is equal to the given object.
 java.lang.String[] getFilenames()
          Retrieves the names of all of the files' that is specified by this Torrent.
 java.lang.String getHexHash()
          Returns the hexadecimal representation of the hash returned from getInfoHash().
 java.lang.String getInfoHash()
          Returns the hash of the info dictionary specified by the torrent's metainfo.
 long[] getLengths()
          Retrieve the specified lengths of the files contained within this torrent.
 java.lang.String getName()
          Retrieves the name of this torrent file.
 int getNumPieces()
          Returns the number of pieces associated with this torrent.
 int getPieceLength()
          Returns the length of a piece.
 java.lang.String[] getPieces()
          Returns a string array that contains the SHA-1 hash of each of the pieces defined by the torrent's metainfo.
 java.io.File getTargetFile()
          Gets the file that has been set as the target file of this torrent per setTargetFile(File).
 long getTotalLength()
          Retrieves the total length of all of the files specified within this torrent.
 java.lang.String getTracker()
          Retrieves the URL of the tracker that's handling the requests for this torrent.
 int hashCode()
          Returns the hash code of this TorrentFile based on its info hash.
 boolean isMultiFile()
          Returns whether this torrent is associated with multiple files or not.
 void save(java.io.File file)
          Writes the contents of the file that was used to initialize this TorrentFile onto the provided file.
 void setTargetFile(java.io.File file)
          Sets the target file or folder that this torrent should download to or look for the corresponding files in.
 boolean validate()
          Checks the integrity of the target file or folder as set by setTargetFile(File) to determine whether its contents pass all of the hash checks.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TorrentFile

public TorrentFile(java.io.File file)
            throws java.lang.IllegalArgumentException,
                   java.io.IOException
Creates a new Torrent to analyze the provided torrent file.

Parameters:
file - the torrent file
Throws:
java.lang.IllegalArgumentException - If file is null or a directory
java.io.IOException - If an I/O error occurs whilst analyzing the torrent file
Method Detail

validate

public boolean validate()
                 throws java.lang.IllegalStateException,
                        java.io.IOException
Checks the integrity of the target file or folder as set by setTargetFile(File) to determine whether its contents pass all of the hash checks.

Returns:
true if and only if every hash check has been passed, false otherwise
Throws:
java.io.FileNotFoundException - If one of the files associated with the torrent could not be found
java.lang.IllegalStateException - If the target file has not been set yet with setTargetFile(File)
java.io.IOException - If an I/O error occurs while reading from the files

setTargetFile

public void setTargetFile(java.io.File file)
                   throws java.lang.IllegalArgumentException
Sets the target file or folder that this torrent should download to or look for the corresponding files in.

Parameters:
file - the target file or folder to use, this should be a file if the torrent is a single file torrent or a folder if it has multiple files
Throws:
java.lang.IllegalArgumentException - If file is null or if file is a directory when this torrent is only using a single file

getInfoHash

public java.lang.String getInfoHash()
Returns the hash of the info dictionary specified by the torrent's metainfo. This is primarily used for torrent identification when sending messages to the tracker.

Returns:
the hash of the info dictionary within the torrent's metainfo, this will likely contain binary data and will not be human-readable as a result

getHexHash

public java.lang.String getHexHash()
Returns the hexadecimal representation of the hash returned from getInfoHash(). This string is forty characters long.

Returns:
the hexadecimal value of getInfoHash()

getLengths

public long[] getLengths()
Retrieve the specified lengths of the files contained within this torrent. Every length contained within the returned array corresponds to a file's name specified by getFilenames().

Returns:
an array of lengths for each of the files specified within the torrent's metainfo

getPieceLength

public int getPieceLength()
Returns the length of a piece.

Returns:
a piece's length

getTracker

public java.lang.String getTracker()
Retrieves the URL of the tracker that's handling the requests for this torrent.

Returns:
the tracker's URL

getPieces

public java.lang.String[] getPieces()
Returns a string array that contains the SHA-1 hash of each of the pieces defined by the torrent's metainfo.

Returns:
an array of strings with the SHA-1 hash for each piece of data

getNumPieces

public int getNumPieces()
Returns the number of pieces associated with this torrent.

Returns:
the number of pieces

getFilenames

public java.lang.String[] getFilenames()
Retrieves the names of all of the files' that is specified by this Torrent. All of the files' lengths can be matched up against the long value stored within the returned array from getLengths().

Returns:
an array of names for all of the files specified by the metadata

getName

public java.lang.String getName()
Retrieves the name of this torrent file. This is dictated by whatever is before the ending .torrent extension. If no such extension exists, the entire file's name will be returned.

Returns:
the name of this torrent without the trailing .torrent extension, if present

getTargetFile

public java.io.File getTargetFile()
Gets the file that has been set as the target file of this torrent per setTargetFile(File).

Returns:
the target file for this torrent

isMultiFile

public boolean isMultiFile()
Returns whether this torrent is associated with multiple files or not.

Returns:
true if this torrent specifies multiple files, false otherwise

getTotalLength

public long getTotalLength()
Retrieves the total length of all of the files specified within this torrent. This is equivalent to the sum of all the lengths returned from the array in getLengths().

Returns:
the combined length of all the files specified by this torrent

save

public void save(java.io.File file)
          throws java.io.IOException
Writes the contents of the file that was used to initialize this TorrentFile onto the provided file.

Parameters:
file - the file to save to
Throws:
java.io.IOException - If an I/O error occurs while trying to write to the file

equals

public boolean equals(java.lang.Object other)
Returns whether this TorrentFile is equal to the given object. The two are the same if their info hash section of the contained metainfo is the same. If other is not an instance of a TorrentFile, false is returned.

Overrides:
equals in class java.lang.Object
Parameters:
other -
Returns:
true if other is a TorrentFile and its info hash is the same as this one, false otherwise

hashCode

public int hashCode()
Returns the hash code of this TorrentFile based on its info hash.

Overrides:
hashCode in class java.lang.Object
Returns:
the result of calling hashCode() on the returned string from getInfoHash()