Class TorrentInfo

java.lang.Object
com.frostwire.jlibtorrent.TorrentInfo

public final class TorrentInfo extends Object
This class represents the information stored in a .torrent file
  • Constructor Details

    • TorrentInfo

      public TorrentInfo(torrent_info ti)
    • TorrentInfo

      public TorrentInfo(File torrent)
      Load the torrent file and decode it inside the constructor, for convenience.

      This might not be the most suitable for applications that want to be able to report detailed errors on what might go wrong.

      Parameters:
      torrent - the torrent file
    • TorrentInfo

      public TorrentInfo(byte[] data)
      Load the torrent data and decode it inside the constructor, for convenience.

      This might not be the most suitable for applications that want to be able to report detailed errors on what might go wrong.

      Parameters:
      data - the torrent data
    • TorrentInfo

      public TorrentInfo(MappedByteBuffer buffer)
  • Method Details

    • swig

      public torrent_info swig()
      Returns:
      the native object
    • files

      public FileStorage files()
      The FileStorage object contains the information on how to map the pieces to files.

      It is separated from the TorrentInfo object because when creating torrents a storage object needs to be created without having a torrent file. When renaming files in a storage, the storage needs to make its own copy of the FileStorage in order to make its mapping differ from the one in the torrent file.

      Returns:
      the files storage
    • origFiles

      public FileStorage origFiles()
      Returns the original (unmodified) file storage for this torrent. This is used by the web server connection, which needs to request files with the original names. Filename may be changed using renameFile(int, String).
      Returns:
      the original file storage
    • renameFile

      public void renameFile(int index, String newFilename)
      Renames a the file with the specified index to the new name. The new filename is reflected by the FileStorage returned by files() but not by the one returned by origFiles().

      If you want to rename the base name of the torrent (for a multifile torrent), you can copy the FileStorage (see files() and origFiles() ), change the name, and then use remapFiles(FileStorage).

      The newFilename can both be a relative path, in which case the file name is relative to the savePath of the torrent. If the newFilename is an absolute path then the file is detached from the savePath of the torrent. In this case the file is not moved when TorrentHandle.moveStorage(String, MoveFlags) is invoked.

      Parameters:
      index - the file index to rename
      newFilename - the new file name
    • remapFiles

      public void remapFiles(FileStorage f)
      Remaps the file storage to a new file layout. This can be used to, for instance, download all data in a torrent to a single file, or to a number of fixed size sector aligned files, regardless of the number and sizes of the files in the torrent.

      The new specified FileStorage must have the exact same size as the current one.

      Parameters:
      f - the file storage
    • addTracker

      public void addTracker(String url)
      Adds a tracker to the announce-list.
      Parameters:
      url - the tracker url
    • addTracker

      public void addTracker(String url, int tier)
      Adds a tracker to the announce-list. The tier determines the order in which the trackers are to be tried.
      Parameters:
      url - the tracker url
      tier - the tracker tier
    • trackers

      public ArrayList<AnnounceEntry> trackers()
      Will return a sorted list with the trackers of this torrent info.

      Each announce entry contains a string, which is the tracker url, and a tier index. The tier index is the high-level priority. No matter which trackers that works or not, the ones with lower tier will always be tried before the one with higher tier number.

      Returns:
      the list of trackers
    • similarTorrents

      public ArrayList<Sha1Hash> similarTorrents()
      This function is related to BEP38_ (mutable torrents). The vector returned from this correspond to the "similar" in the .torrent file. The info-hashes from within the info-dict and from outside of it are included.

      BEP38: http://www.bittorrent.org/beps/bep_0038.html

      Returns:
    • collections

      public ArrayList<String> collections()
      This function is related to BEP38_ (mutable torrents). The vector returned from this correspond to the "collections" keys in the .torrent file. The collections from within the info-dict and from outside of it are included.

      BEP38: http://www.bittorrent.org/beps/bep_0038.html

      Returns:
    • clearTrackers

      public void clearTrackers()
      Clear the internal list of trackers.
    • addUrlSeed

      public void addUrlSeed(String url)
      Adds one url to the list of url seeds. Currently, the only transport protocol supported for the url is http.
      Parameters:
      url -
      See Also:
    • addUrlSeed

      public void addUrlSeed(String url, String externAuth)
      Adds one url to the list of url seeds. Currently, the only transport protocol supported for the url is http.

      The externAuth argument can be used for other authorization schemes than basic HTTP authorization. If set, it will override any username and password found in the URL itself. The string will be sent as the HTTP authorization header's value (without specifying "Basic").

      This is the same as calling addUrlSeed(String, String, List) with an empty list.

      Parameters:
      url -
      externAuth -
    • addUrlSeed

      public void addUrlSeed(String url, String externAuth, List<Pair<String,String>> extraHeaders)
      Adds one url to the list of url seeds. Currently, the only transport protocol supported for the url is http.

      The externAuth argument can be used for other authorization schemes than basic HTTP authorization. If set, it will override any username and password found in the URL itself. The string will be sent as the HTTP authorization header's value (without specifying "Basic").

      The extraHeaders argument can be used to insert custom HTTP headers in the requests to a specific web seed.

      Parameters:
      url -
      externAuth -
      extraHeaders -
    • addHttpSeed

      public void addHttpSeed(String url)
      Adds one url to the list of http seeds. Currently, the only transport protocol supported for the url is http.
      Parameters:
      url -
    • addHttpSeed

      public void addHttpSeed(String url, String externAuth)
      Adds one url to the list of http seeds. Currently, the only transport protocol supported for the url is http.

      The externAuth argument can be used for other authorization schemes than basic HTTP authorization. If set, it will override any username and password found in the URL itself. The string will be sent as the HTTP authorization header's value (without specifying "Basic").

      Parameters:
      url -
      externAuth -
    • addHttpSeed

      public void addHttpSeed(String url, String externAuth, List<Pair<String,String>> extraHeaders)
      Adds one url to the list of http seeds. Currently, the only transport protocol supported for the url is http.

      The externAuth argument can be used for other authorization schemes than basic HTTP authorization. If set, it will override any username and password found in the URL itself. The string will be sent as the HTTP authorization header's value (without specifying "Basic").

      The extraHeaders argument defaults to an empty list, but can be used to insert custom HTTP headers in the requests to a specific web seed.

      Parameters:
      url -
      externAuth -
      extraHeaders -
    • webSeeds

      public ArrayList<WebSeedEntry> webSeeds()
      Returns all url seeds and http seeds in the torrent. Each entry is a WebSeedEntry and may refer to either a url seed or http seed.
      Returns:
      the list of web seeds
    • setWebSeeds

      public void setWebSeeds(List<WebSeedEntry> seeds)
      Replaces all web seeds with the ones specified in the seeds list.
      Parameters:
      seeds - the list of web seeds
    • totalSize

      public long totalSize()
      The total number of bytes the torrent-file represents (all the files in it).
      Returns:
    • pieceLength

      public int pieceLength()
      The number of byte for each piece.

      The difference between pieceSize(int) and pieceLength() is that pieceSize(int) takes the piece index as argument and gives you the exact size of that piece. It will always be the same as pieceLength() except in the case of the last piece, which may be smaller.

      Returns:
    • numPieces

      public int numPieces()
      The total number of pieces.
      Returns:
    • infoHashV1

      public Sha1Hash infoHashV1()
      returns the SHA1 info-hash of the torrent.
      Returns:
    • infoHashV2

      public Sha256Hash infoHashV2()
    • infoHashType

      public info_hash_t infoHashType()
    • numFiles

      public int numFiles()
      If you need index-access to files you can use this method to access files using indices.
      Returns:
    • mapBlock

      public ArrayList<FileSlice> mapBlock(int piece, long offset, int size)
      This function will map a piece index, a byte offset within that piece and a size (in bytes) into the corresponding files with offsets where that data for that piece is supposed to be stored.
      Parameters:
      piece -
      offset -
      size -
      Returns:
      See Also:
    • mapFile

      public PeerRequest mapFile(int file, long offset, int size)
      This function will map a range in a specific file into a range in the torrent. The offset parameter is the offset in the file, given in bytes, where 0 is the start of the file.

      The input range is assumed to be valid within the torrent. offset + size is not allowed to be greater than the file size. index must refer to a valid file, i.e. it cannot be >= numFiles().

      Parameters:
      file -
      offset -
      size -
      Returns:
      See Also:
    • isValid

      public boolean isValid()
      Returns true if this torrent_info object has a torrent loaded.

      This is primarily used to determine if a magnet link has had its metadata resolved yet or not.

      Returns:
    • isPrivate

      public boolean isPrivate()
      Returns true if this torrent is private. i.e., it should not be distributed on the trackerless network (the kademlia DHT).
      Returns:
    • isI2p

      public boolean isI2p()
      Returns true if this is an i2p torrent. This is determined by whether or not it has a tracker whose URL domain name ends with ".i2p". i2p torrents disable the DHT and local peer discovery as well as talking to peers over anything other than the i2p network.
      Returns:
    • pieceSize

      public int pieceSize(int index)
    • hashForPiece

      public Sha1Hash hashForPiece(int index)
      takes a piece-index and returns the 20-bytes sha1-hash for that piece and ``info_hash()`` returns the 20-bytes sha1-hash for the info-section of the torrent file.
      Parameters:
      index -
      Returns:
    • isLoaded

      public boolean isLoaded()
    • name

      public String name()
      returns the name of the torrent.

      the name is an UTF-8 encoded strings.

      Returns:
    • creationDate

      public long creationDate()
      Returns the creation date of he torrent as time_t (`posix time`_). If there's no time stamp in the torrent file, a value of zero is returned.
      Returns:
      the time
    • creator

      public String creator()
      Returns the creator string in the torrent. If there is no creator string it will return an empty string.
      Returns:
      the creator
    • comment

      public String comment()
      Returns the comment associated with the torrent. If there's no comment, it will return an empty string.

      The comment is an UTF-8 encoded strings.

      Returns:
      the comment
    • nodes

      public ArrayList<Pair<String,Integer>> nodes()
      If this torrent contains any DHT nodes, they are returned in their original form (host name and port number).
      Returns:
    • addNode

      public void addNode(String host, int port)
      This is used when creating torrent. Use this to add a known DHT node. It may be used, by the client, to bootstrap into the DHT network.
      Parameters:
      host -
      port -
    • info

      public bdecode_node info(String key)
      This function looks up keys from the info-dictionary of the loaded torrent file. It can be used to access extension values put in the .torrent file. If the specified key cannot be found, it returns NULL.
      Parameters:
      key -
      Returns:
    • makeMagnetUri

      public String makeMagnetUri()
      Generates a magnet URI from the specified torrent. If the torrent is invalid, null is returned.

      For more information about magnet links, see magnet-links_.

    • toEntry

      public Entry toEntry()
    • bencode

      public byte[] bencode()
    • bdecode

      public static TorrentInfo bdecode(byte[] data)