|
1 | 1 | # Java Torrent File Parser |
2 | | -A simple Java library that allows you to read the contents of a `.torrent` file. |
3 | | - |
4 | | -# Features |
5 | | -- Parse a .torrent file into a TorrentMetadata object |
6 | | -- Parse a .torrent file into a Map<String, Object> |
7 | | -- Get the info hash |
8 | | -- Supports single-file and multi-file torrents |
9 | | -- Access optional metadata fields such as trackers, comments, and creator info |
10 | | - |
11 | | -### TorrentMetadata object structure |
12 | | - public class TorrentMetadata { |
13 | | - Map<String, Object> otherValues; |
14 | | - String announce; |
15 | | - Long totalLength; |
16 | | - List<TorrentFile> files; |
17 | | - String name; |
18 | | - Long pieceLength; |
19 | | - String pieces; |
20 | | - boolean isSingleFile; |
21 | | - boolean isPrivate; |
22 | | - String infoHash; // null if computeInfoHash is set to false |
| 2 | +A simple Java library for parsing and encoding `.torrent` files using the Bencode format. |
| 3 | + |
| 4 | +## Requirements |
| 5 | +- Java 17 or higher |
| 6 | +- Maven (for building locally) |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Because this library is not currently published to Maven Central, you will need to install it to your local Maven repository first. |
| 11 | + |
| 12 | +```bash |
| 13 | +git clone https://github.com/robothaver/JavaTorrentFileParser |
| 14 | +cd JavaTorrentFileParser |
| 15 | +mvn clean install |
| 16 | +``` |
| 17 | + |
| 18 | +**Maven:** |
| 19 | +```xml |
| 20 | +<dependency> |
| 21 | + <groupId>com.robothaver</groupId> |
| 22 | + <artifactId>TorrentFileParser</artifactId> |
| 23 | + <version>1.2</version> |
| 24 | +</dependency> |
| 25 | +``` |
| 26 | + |
| 27 | +## Features |
| 28 | + |
| 29 | +### Parsing |
| 30 | + |
| 31 | +- Parse `.torrent` files into a `TorrentMetadata` object |
| 32 | +- Parse `.torrent` files into a `Map<String, Object>` |
| 33 | +- Calculate the info hash |
| 34 | +- Support for both single-file and multi-file torrents |
| 35 | +- Access optional metadata fields (trackers, comments, creator info, Azureus properties and more) |
| 36 | +- Reusable and thread-safe parser |
| 37 | + |
| 38 | +### Encoding |
| 39 | + |
| 40 | +- Encode a `Map<String, Object>` into a Bencoded byte array |
| 41 | +- Encode a `TorrentMetadata` object into a Bencoded byte array |
| 42 | +- Reusable and thread-safe encoder |
| 43 | + |
| 44 | +## Raw byte and string handling |
| 45 | +- If you parse into a `TorrentMetadata` object, the parser automatically converts known text fields into Java Strings. However, any unrecognized fields placed into the `otherValues` map will remain as raw byte arrays and must be converted manually. |
| 46 | +- If you parse directly to a `Map<String, Object>`, all string values will remain as byte arrays and require manual conversion. |
| 47 | + |
| 48 | +## Usage |
| 49 | +### Parser usage |
| 50 | +```java |
| 51 | +public static void main(String[] args) throws IOException, MalformedTorrentFileException, NoSuchAlgorithmException { |
| 52 | + Path path = Path.of("Path to torrent file"); |
| 53 | + byte[] bytes = Files.readAllBytes(path); |
| 54 | + Instant start = Instant.now(); |
| 55 | + |
| 56 | + TorrentFileParser torrentParser = new TorrentFileParserImpl(); |
| 57 | + |
| 58 | + // Parse to metadata object |
| 59 | + // If InfoHashCalculator is provided the info hash will get calculated |
| 60 | + TorrentMetadata torrentMetadata = torrentParser.parseToMetadata(bytes, new InfoHashCalculatorImpl()); |
| 61 | + // Or use torrentParser.parseToMap() to parse to a map |
23 | 62 |
|
24 | | - // Optional fields |
25 | | - List<List<String>> announceList; |
26 | | - String creator; |
27 | | - Long creationDate; |
28 | | - String source; |
29 | | - String comment; |
30 | | - String encoding; |
31 | | - Map<String, Object> azureusProperties; |
32 | | - } |
| 63 | + Instant finish = Instant.now(); |
| 64 | + |
| 65 | + System.out.println(torrentMetadata.getName()); |
| 66 | + long timeElapsed = Duration.between(start, finish).toMillis(); |
| 67 | + System.out.println("Parsing finished in " + timeElapsed + "ms"); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +*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.* |
| 72 | + |
| 73 | +### Encoder usage |
| 74 | +```java |
| 75 | +public static void main(String[] args) throws NoSuchAlgorithmException, IOException, MalformedTorrentFileException { |
| 76 | + Path path = Path.of("Path to torrent file"); |
| 77 | + TorrentFileParserImpl torrentFileParser = new TorrentFileParserImpl(); |
| 78 | + byte[] fileBytes = Files.readAllBytes(path); |
| 79 | + |
| 80 | + // Encode a map to bencoded byte array (reencode a .torrent file in this example) |
| 81 | + Map<String, Object> torrentMap = torrentFileParser.parseToMap(fileBytes, null); |
| 82 | + TorrentEncoderImpl torrentEncoder = new TorrentEncoderImpl(); |
| 83 | + byte[] bytes = torrentEncoder.encodeTorrentMap(torrentMap); |
| 84 | + |
| 85 | + // Encode metadata object to bencoded byte array |
| 86 | + TorrentMetadata torrentMetadata = torrentFileParser.parseToMetadata(fileBytes, null); |
| 87 | + byte[] metadataBytes = torrentEncoder.encodeTorrentMetadata(torrentMetadata); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## TorrentMetadata object structure |
| 92 | +```java |
| 93 | +public class TorrentMetadata { |
| 94 | + private final Map<String, Object> otherValues; |
| 95 | + private String announce; |
| 96 | + private String name; |
| 97 | + private Long pieceLength; |
| 98 | + private boolean isSingleFile; |
| 99 | + private boolean isPrivate; |
| 100 | + private Long totalLength; |
| 101 | + private byte[] pieces; |
| 102 | + private final List<TorrentFile> files; |
| 103 | + String infoHash; // null if no InfoHashCalculator is provided |
| 104 | + |
| 105 | + // Optional fields |
| 106 | + private List<List<String>> announceList; |
| 107 | + private String creator; |
| 108 | + private Long creationDate; |
| 109 | + private String source; |
| 110 | + private String comment; |
| 111 | + private String encoding; |
| 112 | + private Map<String, Object> azureusProperties; |
| 113 | +} |
| 114 | +``` |
0 commit comments