java.lang.Object
org.klomp.snark.MagnetURI

public class MagnetURIextends Object
Magnet URI parser and handler for BitTorrent magnet links.
  • Field Details

    • _trackers

      private final List<String> _trackers
      Trackers named in the link.
    • _name

      private final String _name
      Torrent name, null if the link gives none.
    • _ih

      private final byte[] _ih
      The info hash, if the link gives one.
    • _length

      private final long _length
      Total length, -1 if the link gives none.
    • MAGNET

      public static final String MAGNET
      BEP 9 magnet prefix.
      See Also:
    • MAGNET_FULL

      public static final String MAGNET_FULL
      Full magnet URI prefix with info hash parameter.
      See Also:
    • MAGGOT

      public static final String MAGGOT
      Maggot protocol prefix.
      See Also:
    • MAGNET_FULL_V2

      public static final String MAGNET_FULL_V2
      Link will contain two xt params. Duplicate xt params are not parsed yet, needed for dual v1/v2 links. See also: BitTorrent v2
      See Also:
  • Constructor Details

  • Method Details

    • getInfoHash

      public byte[] getInfoHash()
      Returns the 20-byte info hash of the torrent.
      Returns:
      20 bytes or null
    • getLength

      public long getLength()
      Returns the length from the xl parameter of the magnet link.
      Returns:
      the length or 0 if not specified.
    • getName

      public String getName()
      Returns the display name for this magnet link.
      Returns:
      pretty name or null, NOT HTML escaped
    • getTrackerURL

      public String getTrackerURL()
      Returns the first valid I2P tracker URL from the magnet link.
      Returns:
      first valid tracker url or null
    • getTrackerURLs

      public List<String> getTrackerURLs()
      Returns all valid I2P tracker URLs from the magnet link.
      Returns:
      all valid tracker urls or null if none
    • getParam

      private static String getParam(String key, String uri)
      The first decoded parameter, or null.
      Parameters:
      key - the parameter name to look for
      uri - the magnet URI to search
      Returns:
      first decoded parameter or null
    • getMultiParam

      private static List<String> getMultiParam(String key, String uri)
      All decoded parameters, or null.
      Parameters:
      key - the parameter name to look for
      uri - the magnet URI to search
      Returns:
      all decoded parameters or null
    • getTrackerParam

      private static List<String> getTrackerParam(String uri)
      All valid I2P trackers, or null if none.
      Parameters:
      uri - the magnet URI whose tr parameters are filtered
      Returns:
      all valid I2P trackers or null if none
    • decode

      private static String decode(String s)
      Decode %xx encoding, convert to UTF-8 if necessary. Copied from i2ptunnel LocalHTTPServer. Also converts '+' to ' ' so the dn parameter comes out right These are coming in via a application/x-www-form-urlencoded form so the pluses are in there... hopefully any real + is encoded as %2B.
      Parameters:
      s - the string to decode, %xx escapes and + for space
      Returns:
      the decoded string, s unchanged if it contains neither % nor +