com.torrentMech.codec
Class Torrent

java.lang.Object
  extended by com.torrentMech.codec.Torrent

public class Torrent
extends java.lang.Object

Torrent represents a decoded Torrent file. Dictionaries are stored internally as Hashtable objects while lists are stored as LinkedList objects. Values from the decoded torrent can be called from various class methods. Malformed torrent files will throw a com.torrentMech.codec.CorruptTorrentException.

Version:
.01 alpha
Author:
Will Bailey

Field Summary
private  int pos
          Used internally to keep the current position withing the bencoded torrent string during decoding.
private  java.lang.String torrent
          Internally represents the original bencoded torrent.
private static int TORRENT_E
          Used within the decoder methods to keep track of current stage of decoding.
private static int TORRENT_KEY
          Used within the decoder methods to keep track of current stage of decoding.
private static int TORRENT_LIST_ELEMENT
          Used within the decoder methods to keep track of current stage of decoding.
private static int TORRENT_VALUE
          Used within the decoder methods to keep track of current stage of decoding.
private  java.util.Hashtable torrentTree
          Internally represents the top level dictionary of the torrent.
 
Constructor Summary
Torrent(java.io.BufferedReader br)
          Populates the Torrent instance with torrent details read from a java.io.BufferedReader.
Torrent(java.lang.String torrent)
          Populates the Torrent instance with torrent details read from a java.lang.String.
 
Method Summary
private  java.lang.String checkOptionalKey(java.lang.String key)
          Internal utility method for the Torrent class to assist in retrieving requests for optional keys within the torrent.
private  java.util.Hashtable decodeDict()
          Decode a bencoded torrent's dictionary into it's key/value pairs.
private  java.lang.String decodeInt()
          Decodes integer values of a torrent.
private  java.util.LinkedList decodeList()
          Decodes list sections of the torrent.
private  java.lang.String decodeString()
          Decodes String sections of the torrent.
 java.lang.String getComment()
           
 java.lang.String getCreatedBy()
           
 long getCreationDate()
           
 java.lang.String[][] getFiles()
          Returns a two-dimensional java.lang.String array of the files contained in the torrent.
 java.lang.String getTorrent()
           
 java.lang.String[] getTrackers()
          Returns all unique trackers for this torrent.
 boolean isMultiFile()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TORRENT_KEY

private static final int TORRENT_KEY
Used within the decoder methods to keep track of current stage of decoding. Currently on a key.

See Also:
Constant Field Values

TORRENT_VALUE

private static final int TORRENT_VALUE
Used within the decoder methods to keep track of current stage of decoding. Currently on a value.

See Also:
Constant Field Values

TORRENT_E

private static final int TORRENT_E
Used within the decoder methods to keep track of current stage of decoding. End of list or dictionary.

See Also:
Constant Field Values

TORRENT_LIST_ELEMENT

private static final int TORRENT_LIST_ELEMENT
Used within the decoder methods to keep track of current stage of decoding. List element.

See Also:
Constant Field Values

pos

private int pos
Used internally to keep the current position withing the bencoded torrent string during decoding.


torrentTree

private java.util.Hashtable torrentTree
Internally represents the top level dictionary of the torrent.


torrent

private java.lang.String torrent
Internally represents the original bencoded torrent.

Constructor Detail

Torrent

public Torrent(java.io.BufferedReader br)
        throws CorruptTorrentException
Populates the Torrent instance with torrent details read from a java.io.BufferedReader.

Throws:
CorruptTorrentException

Torrent

public Torrent(java.lang.String torrent)
        throws CorruptTorrentException
Populates the Torrent instance with torrent details read from a java.lang.String. This String is not a path or URL to a torrent, but the actual Bencoded torrent.

Throws:
CorruptTorrentException
Method Detail

getTorrent

public java.lang.String getTorrent()
Returns:
The torrent in Bencoded form that this Torrent instance represents.

getTrackers

public java.lang.String[] getTrackers()
Returns all unique trackers for this torrent. The trackers are drawn from the announce key and the announce-list key. The annouce key is compared to the announce-list key to see if the tracker shows up twice (most torrents seem to repeat the announce tracker within the announce-list key).

Returns:
Returns a java.lang.String array of all the unique trackers for this torrent.

getFiles

public java.lang.String[][] getFiles()
Returns a two-dimensional java.lang.String array of the files contained in the torrent. Each of the first dimensions of the array is the file name including path and each of the second dimensions of the array is the corresponding file length in bytes. The array is initialized in the form [n][2] where n is the # of files in the torrent.
i.e. A [2][2] array would be as follows:

[MyFolder\File1.txt][MyFolder\File2.txt]
[1024][2436]

Returns:
Returns a two-dimensional java.lang.String array of the torrent files including their path and length.

isMultiFile

public boolean isMultiFile()
Returns:
Returns true if the torrent contains multiple files, false if it is a single file torrent.

getCreationDate

public long getCreationDate()
Returns:
Returns the creation date of the torrent is milliseconds since 1970 or 0 if none exists.

getCreatedBy

public java.lang.String getCreatedBy()
Returns:
Returns the value of the optional created by key or Null if none exists.

getComment

public java.lang.String getComment()
Returns:
Returns the value of the optional comment key or Null if none exists.

checkOptionalKey

private java.lang.String checkOptionalKey(java.lang.String key)
Internal utility method for the Torrent class to assist in retrieving requests for optional keys within the torrent. Simply to reduce redundant code. The argument is the key being queried.

Returns:
Returns the java.lang.String value of the requested key or null if the key doesn't exist.

decodeDict

private java.util.Hashtable decodeDict()
                                throws CorruptTorrentException
Decode a bencoded torrent's dictionary into it's key/value pairs. Recursive method.

Returns:
Hashtable representing a bencoded torrent dictionary.
Throws:
CorruptTorrentException

decodeString

private java.lang.String decodeString()
                               throws CorruptTorrentException
Decodes String sections of the torrent.

Throws:
CorruptTorrentException

decodeInt

private java.lang.String decodeInt()
                            throws CorruptTorrentException
Decodes integer values of a torrent. Returned as a String for storage in the Hashtable.

Throws:
CorruptTorrentException

decodeList

private java.util.LinkedList decodeList()
                                 throws CorruptTorrentException
Decodes list sections of the torrent.

Throws:
CorruptTorrentException