public class MetaInfo extends Object
This class parses and stores the metadata from .torrent files, including:
Note: This class has a known limitation where it doesn't propagate custom meta fields into the bencoded info data, and from there to the info_hash. However, it currently works with torrents created by I2P-BT, I2PRufus and Azureus.
| Constructor and Description |
|---|
MetaInfo(InputStream in)Creates a new MetaInfo from the given InputStream. |
MetaInfo(Map<String,BEValue> m)Creates a new MetaInfo from a Map of BEValues and the SHA1 over the original bencoded info
dictionary (this is a hack, we could reconstruct the bencoded stream and recalculate the
hash). |
MetaInfo(MetaInfo old,
String newAnnounce,
List<List<String>> newAnnounceList,
String newComment,
String newCreatedBy,
List<String> newUrlList)Will not change infohash. |
MetaInfo(String announce,
String name,
String nameUtf8,
List<List<String>> files,
List<Long> lengths,
int pieceLength,
byte[] pieceHashes,
long length,
int privateTorrent,
List<List<String>> announceList,
String createdBy,
List<String> urlList,
String comment)Preserves privateTorrent int value, for use by main(). |
MetaInfo(String announce,
String name,
String nameUtf8,
List<List<String>> files,
List<Long> lengths,
List<String> attributes,
int pieceLength,
byte[] pieceHashes,
long length,
boolean privateTorrent,
List<List<String>> announceList,
String createdBy,
List<String> urlList,
String comment)Called by Storage when creating a new torrent from local data. |
| Modifier and Type | Method and Description |
|---|---|
boolean | checkPiece(int piece,
byte[] bs,
int off,
int length)Checks that the given piece has the same SHA1 hash as the given byte array. |
(package private) boolean | checkPiece(PartialPiece pp)Checks a piece against the stored hashes using a PartialPiece. |
(package private) long[] | fileEnds()Cumulative exclusive end offset of each file; single-file torrents return null. |
(package private) int | fileIndex(long pos)Index of the first file whose cumulative exclusive end offset is above the given position,
mapping a torrent byte offset to the file containing it. |
String | getAnnounce()Returns the string representing the URL of the tracker for this torrent. |
List<List<String>> | getAnnounceList()Returns a list of lists of urls. |
String | getComment()The comment string or null. |
String | getCreatedBy()The created-by string or null. |
long | getCreationDate()The creation date (ms) or zero. |
long | getDataLength()Returns the total length of the non-padding files in this torrent, or
getTotalLength() if there are no padding files. |
List<List<String>> | getFiles()Returns a list of lists of file name hierarchies or null if it is a single name. |
byte[] | getInfoBytes()Side effect: Caches infoBytesLength. |
int | getInfoBytesLength()The size of getInfoBytes(). |
byte[] | getInfoHash()Returns the original 20 byte SHA1 hash over the bencoded info map. |
List<Long> | getLengths()Returns a list of Longs indication the size of the individual files, or null if it is a
single file. |
String | getName()Returns the requested name for the file or toplevel directory. |
static String | getNameAndInfoHash(InputStream in,
byte[] infoHashOut)Efficiently returns the name and the 20 byte SHA1 hash of the info dictionary in a torrent
file. |
byte[] | getPieceHashes()Returns the piece hashes. |
int | getPieceLength(int piece)Return the length of a piece. |
int | getPieces()Returns the number of pieces in the torrent. |
int | getPrivateTrackerStatus()The private tracker flag: 0 (default), 1 (set to 1), -1 (set to 0). |
byte[] | getTorrentData()Returns the bencoded torrent file data. |
long | getTotalLength()Returns the total length of the torrent in bytes. |
List<String> | getWebSeedURLs()Returns the web seed URLs for BEP 19 HTTP seeding. |
boolean | isPaddingFile(int filenum)Is this file a padding file? |
boolean | isPrivate()Is it a private torrent? |
boolean | isRangePadding(long offset,
int length)True if every byte in the given range lies within BEP 47 padding files. |
static void | main(String[] args)Command-line utility to inspect and optionally modify torrent file metadata. |
MetaInfo | reannounce(String announce)Creates a copy of this MetaInfo that shares everything except the announce URL. |
String | toString()Returns a human-readable summary of this torrent's metadata. |
public MetaInfo(InputStream in) throws IOException
in - the stream containing the bencoded torrent dataIOException - if the stream cannot be read or the bencoded data is malformedpublic MetaInfo(Map<String,BEValue> m) throws InvalidBEncodingException
m - the parsed bencoded dictionary as a map of keys to BEValuesInvalidBEncodingException - if the info dictionary is missing or malformedpublic MetaInfo(MetaInfo old, String newAnnounce, List<List<String>> newAnnounceList, String newComment, String newCreatedBy, List<String> newUrlList)
old - the source MetaInfo whose infohash, name, files, and hashes are reusednewAnnounce - the replacement announce URL, may be nullnewAnnounceList - tiered list of replacement announce URLs, may be nullnewComment - replacement comment string, may be nullnewCreatedBy - replacement created-by string, may be nullnewUrlList - replacement web seed URLs, may be nullpublic MetaInfo(String announce, String name, String nameUtf8, List<List<String>> files, List<Long> lengths, int pieceLength, byte[] pieceHashes, long length, int privateTorrent, List<List<String>> announceList, String createdBy, List<String> urlList, String comment)
announce - the tracker announce URL, may be nullname - the file or top-level directory namenameUtf8 - unused, retained for API compatibilityfiles - list of file path components per file, null for single-file torrentlengths - list of file sizes in bytes, null for single-file torrentpieceLength - the length of each data piece in bytespieceHashes - concatenated 20-byte SHA1 hashes of all pieceslength - total torrent size in bytesprivateTorrent - 0 = not present, 1 = private, -1 = explicitly not privateannounceList - tiered list of alternate announce URLs, may be nullcreatedBy - application name that created the torrent, may be nullurlList - web seed URLs for BEP 19 HTTP seeding, may be nullcomment - free-form user comment, may be nullpublic MetaInfo(String announce, String name, String nameUtf8, List<List<String>> files, List<Long> lengths, List<String> attributes, int pieceLength, byte[] pieceHashes, long length, boolean privateTorrent, List<List<String>> announceList, String createdBy, List<String> urlList, String comment)
announce - the tracker announce URL, may be nullname - the file or top-level directory namenameUtf8 - unused, retained for API compatibilityfiles - list of file path components per file, null for single-file torrentlengths - list of file sizes in bytes, null for single-file torrentattributes - per-file attribute strings (BEP 47 "p" marks padding files), may be nullpieceLength - the length of each data piece in bytespieceHashes - concatenated 20-byte SHA1 hashes of all pieceslength - total torrent size in bytesprivateTorrent - whether the tracker restricts peer sharing to the swarmannounceList - tiered list of alternate announce URLs, may be nullcreatedBy - application name that created the torrent, may be nullurlList - web seed URLs for BEP 19 HTTP seeding, may be nullcomment - free-form user comment, may be nullpublic boolean checkPiece(int piece,
byte[] bs,
int off,
int length)piece - the piece indexbs - the byte array containing the piece dataoff - the offset within the byte array where the piece data startslength - the number of bytes in the pieceIndexOutOfBoundsException - if the piece index is out of rangeboolean checkPiece(PartialPiece pp)
pp - the PartialPiece containing the piece data and hashlong[] fileEnds()
int fileIndex(long pos)
pos - torrent byte offsetpublic String getAnnounce()
public List<List<String>> getAnnounceList()
public String getComment()
public String getCreatedBy()
public long getCreationDate()
public long getDataLength()
getTotalLength() if there are no padding files. The projected downloaded size.public List<List<String>> getFiles()
public byte[] getInfoBytes()
public int getInfoBytesLength()
public byte[] getInfoHash()
public List<Long> getLengths()
public String getName()
public static String getNameAndInfoHash(InputStream in, byte[] infoHashOut) throws IOException
in - the stream containing the bencoded torrent datainfoHashOut - 20-byte array that receives the info hashIOException - if the stream cannot be read or the data is malformedpublic byte[] getPieceHashes()
public int getPieceLength(int piece)
getPieces()-1).IndexOutOfBoundsException - when piece is equal to or greater then the number of pieces
in the torrent.public int getPieces()
public int getPrivateTrackerStatus()
public byte[] getTorrentData()
public long getTotalLength()
public List<String> getWebSeedURLs()
public boolean isPaddingFile(int filenum)
filenum - the index of the file in the files listpublic boolean isPrivate()
public boolean isRangePadding(long offset,
int length)Padding bytes are all zeros and are never stored on disk, so chunks or pieces entirely within padding need not be requested from peers (BEP 47).
offset - global byte offset into the torrentlength - number of bytespublic static void main(String[] args)
args - command-line argumentspublic MetaInfo reannounce(String announce) throws InvalidBEncodingException
announce - may be nullInvalidBEncodingException