A simple Java library for parsing and encoding .torrent files using the Bencode format.
- Java 17 or higher
- Maven (for building locally)
Because this library is not currently published to Maven Central, you will need to install it to your local Maven repository first.
git clone https://github.com/robothaver/JavaTorrentFileParser
cd JavaTorrentFileParser
mvn clean installMaven:
<dependency>
<groupId>com.robothaver</groupId>
<artifactId>TorrentFileParser</artifactId>
<version>1.2</version>
</dependency>- Parse
.torrentfiles into aTorrentMetadataobject - Parse
.torrentfiles into aMap<String, Object> - Calculate the info hash
- Support for both single-file and multi-file torrents
- Access optional metadata fields (trackers, comments, creator info, Azureus properties and more)
- Reusable and thread-safe parser
- Encode a
Map<String, Object>into a Bencoded byte array - Encode a
TorrentMetadataobject into a Bencoded byte array - Reusable and thread-safe encoder
- If you parse into a
TorrentMetadataobject, the parser automatically converts known text fields into Java Strings. However, any unrecognized fields placed into theotherValuesmap will remain as raw byte arrays and must be converted manually. - If you parse directly to a
Map<String, Object>, all string values will remain as byte arrays and require manual conversion.
publicstaticvoidmain(String[] args) throwsIOException, MalformedTorrentFileException, NoSuchAlgorithmException {
Pathpath = Path.of("Path to torrent file");
byte[] bytes = Files.readAllBytes(path);
Instantstart = Instant.now();
TorrentFileParsertorrentParser = newTorrentFileParserImpl();
// Parse to metadata object// If InfoHashCalculator is provided the info hash will get calculatedTorrentMetadatatorrentMetadata = torrentParser.parseToMetadata(bytes, newInfoHashCalculatorImpl());
// Or use torrentParser.parseToMap() to parse to a mapInstantfinish = Instant.now();
System.out.println(torrentMetadata.getName());
longtimeElapsed = Duration.between(start, finish).toMillis();
System.out.println("Parsing finished in " + timeElapsed + "ms");
}Note: If you parse to a map and provide an InfoHashCalculator, the calculated info hash will get added to the root of the returned map.
publicstaticvoidmain(String[] args) throwsNoSuchAlgorithmException, IOException, MalformedTorrentFileException {
Pathpath = Path.of("Path to torrent file");
TorrentFileParserImpltorrentFileParser = newTorrentFileParserImpl();
byte[] fileBytes = Files.readAllBytes(path);
// Encode a map to bencoded byte array (reencode a .torrent file in this example)Map<String, Object> torrentMap = torrentFileParser.parseToMap(fileBytes, null);
TorrentEncoderImpltorrentEncoder = newTorrentEncoderImpl();
byte[] bytes = torrentEncoder.encodeTorrentMap(torrentMap);
// Encode metadata object to bencoded byte arrayTorrentMetadatatorrentMetadata = torrentFileParser.parseToMetadata(fileBytes, null);
byte[] metadataBytes = torrentEncoder.encodeTorrentMetadata(torrentMetadata);
}publicclassTorrentMetadata {
privatefinalMap<String, Object> otherValues;
privateStringannounce;
privateStringname;
privateLongpieceLength;
privatebooleanisSingleFile;
privatebooleanisPrivate;
privateLongtotalLength;
privatebyte[] pieces;
privatefinalList<TorrentFile> files;
StringinfoHash; // null if no InfoHashCalculator is provided// Optional fieldsprivateList<List<String>> announceList;
privateStringcreator;
privateLongcreationDate;
privateStringsource;
privateStringcomment;
privateStringencoding;
privateMap<String, Object> azureusProperties;
}