Skip to content

Commit cd10437

Browse files
committed
Rename Torrent to TorrentMetadata, add otherValues map
1 parent 833c325 commit cd10437

6 files changed

Lines changed: 50 additions & 35 deletions

File tree

src/main/java/com/robothaver/torrentfileparser/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.robothaver.torrentfileparser;
22

3-
import com.robothaver.torrentfileparser.domain.Torrent;
3+
import com.robothaver.torrentfileparser.domain.TorrentMetadata;
44
import com.robothaver.torrentfileparser.exception.MalformedTorrentFileException;
55
import com.robothaver.torrentfileparser.parser.TorrentFileParser;
66
import com.robothaver.torrentfileparser.parser.TorrentFileParserImpl;
@@ -18,10 +18,10 @@ public static void main(String[] args) throws IOException, MalformedTorrentFileE
1818
Instant start = Instant.now();
1919

2020
TorrentFileParser torrentParser = new TorrentFileParserImpl();
21-
Torrent torrent = torrentParser.parseToTorrent(bytes, true);
21+
TorrentMetadata torrentMetadata = torrentParser.parseToMetadata(bytes, true);
2222
Instant finish = Instant.now();
2323

24-
System.out.println(torrent.getName());
24+
System.out.println(torrentMetadata.getName());
2525
long timeElapsed = Duration.between(start, finish).toMillis();
2626
System.out.println("Parsing finished in " + timeElapsed + "ms");
2727
}

src/main/java/com/robothaver/torrentfileparser/domain/Torrent.java renamed to src/main/java/com/robothaver/torrentfileparser/domain/TorrentMetadata.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.robothaver.torrentfileparser.domain;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.List;
56
import java.util.Map;
67

7-
public class Torrent {
8+
public class TorrentMetadata {
9+
private final Map<String, Object> otherValues = new HashMap<>();
810
private String announce;
911
private String name;
1012
private Long pieceLength;
@@ -24,6 +26,10 @@ public class Torrent {
2426
private String encoding = null;
2527
private Map<String, Object> azureusProperties = null;
2628

29+
public Map<String, Object> getOtherValues() {
30+
return otherValues;
31+
}
32+
2733
public String getAnnounce() {
2834
return announce;
2935
}
@@ -150,8 +156,9 @@ public void setInfoHash(String infoHash) {
150156

151157
@Override
152158
public String toString() {
153-
return "Torrent{" +
154-
"announce='" + announce + '\'' +
159+
return "TorrentMetadata{" +
160+
"otherValues=" + otherValues +
161+
", announce='" + announce + '\'' +
155162
", name='" + name + '\'' +
156163
", pieceLength=" + pieceLength +
157164
", isSingleFile=" + isSingleFile +

src/main/java/com/robothaver/torrentfileparser/parser/ParseWorker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.robothaver.torrentfileparser.parser;
22

3-
import com.robothaver.torrentfileparser.domain.Torrent;
3+
import com.robothaver.torrentfileparser.domain.TorrentMetadata;
44
import com.robothaver.torrentfileparser.exception.MalformedTorrentFileException;
55

66
import java.nio.charset.StandardCharsets;
@@ -20,7 +20,7 @@ public ParseWorker(byte[] bytes, boolean computeInfoHash) {
2020
this.infoDictStartIndex = -1;
2121
}
2222

23-
public Torrent parseToTorrent() throws MalformedTorrentFileException {
23+
public TorrentMetadata parseToMetadata() throws MalformedTorrentFileException {
2424
torrentBuilder = new TorrentBuilder();
2525
parse();
2626
return torrentBuilder.getTorrent();
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
11
package com.robothaver.torrentfileparser.parser;
22

3-
import com.robothaver.torrentfileparser.domain.Torrent;
3+
import com.robothaver.torrentfileparser.domain.TorrentMetadata;
44
import com.robothaver.torrentfileparser.domain.TorrentFile;
55

66
import java.util.List;
77
import java.util.Map;
88

99
public class TorrentBuilder {
10-
private final Torrent torrent = new Torrent();
10+
private final TorrentMetadata torrentMetadata = new TorrentMetadata();
1111
private Long lastLength;
1212

13+
@SuppressWarnings("unchecked")
1314
public void processKeyValue(String key, Object value) {
1415
switch (key) {
15-
case "announce" -> torrent.setAnnounce(String.valueOf(value));
16-
case "name" -> torrent.setName(String.valueOf(value));
17-
case "announce-list" -> torrent.setAnnounceList((List<List<String>>) value);
18-
case "azureus_properties" -> torrent.setAzureusProperties((Map<String, Object>) value);
19-
case "created by" -> torrent.setCreator(String.valueOf(value));
20-
case "creation date" -> torrent.setCreationDate((long) value);
21-
case "encoding" -> torrent.setEncoding(String.valueOf(value));
22-
case "files" -> torrent.setSingleFile(false);
16+
case "announce" -> torrentMetadata.setAnnounce(String.valueOf(value));
17+
case "name" -> torrentMetadata.setName(String.valueOf(value));
18+
case "announce-list" -> torrentMetadata.setAnnounceList((List<List<String>>) value);
19+
case "azureus_properties" -> torrentMetadata.setAzureusProperties((Map<String, Object>) value);
20+
case "created by" -> torrentMetadata.setCreator(String.valueOf(value));
21+
case "creation date" -> torrentMetadata.setCreationDate((long) value);
22+
case "encoding" -> torrentMetadata.setEncoding(String.valueOf(value));
23+
case "files" -> torrentMetadata.setSingleFile(false);
2324
case "length" -> {
2425
lastLength = (long) value;
25-
torrent.setTotalLength(torrent.getTotalLength() + lastLength);
26+
torrentMetadata.setTotalLength(torrentMetadata.getTotalLength() + lastLength);
27+
}
28+
case "path" -> parseFile(value);
29+
case "piece length" -> torrentMetadata.setPieceLength((long) value);
30+
case "source" -> torrentMetadata.setSource(String.valueOf(value));
31+
case "pieces" -> torrentMetadata.setPieces(String.valueOf(value));
32+
case "comment" -> torrentMetadata.setComment(String.valueOf(value));
33+
case "private" -> torrentMetadata.setPrivate((long) value == 1);
34+
default -> {
35+
if (key.equals("info")) return;
36+
torrentMetadata.getOtherValues().put(key, value);
2637
}
27-
case "path" -> torrent.getFiles().add(new TorrentFile(lastLength, formatFilePath(String.valueOf(value))));
28-
case "piece length" -> torrent.setPieceLength((long) value);
29-
case "source" -> torrent.setSource(String.valueOf(value));
30-
case "pieces" -> torrent.setPieces(String.valueOf(value));
31-
case "comment" -> torrent.setComment(String.valueOf(value));
32-
case "private" -> torrent.setPrivate((long) value == 1);
3338
}
3439
}
3540

3641
public void setInfoHash(byte[] infoBytes) {
37-
torrent.setInfoHash(InfoHasCompute.getInfoHash(infoBytes));
42+
torrentMetadata.setInfoHash(InfoHasCompute.getInfoHash(infoBytes));
3843
}
3944

40-
private String formatFilePath(String path) {
41-
return path.replace("[", "").replace("]", "").replace(", ", "/");
45+
private void parseFile(Object value) {
46+
@SuppressWarnings("unchecked")
47+
List<String> pathElements = (List<String>) value;
48+
String fullPath = String.join("/", pathElements);
49+
torrentMetadata.getFiles().add(new TorrentFile(lastLength, fullPath));
4250
}
4351

44-
public Torrent getTorrent() {
45-
return torrent;
52+
public TorrentMetadata getTorrent() {
53+
return torrentMetadata;
4654
}
4755
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.robothaver.torrentfileparser.parser;
22

3-
import com.robothaver.torrentfileparser.domain.Torrent;
3+
import com.robothaver.torrentfileparser.domain.TorrentMetadata;
44
import com.robothaver.torrentfileparser.exception.MalformedTorrentFileException;
55

66
import java.util.Map;
77

88
public interface TorrentFileParser {
9-
Torrent parseToTorrent(byte[] bytes, boolean computeInfoHash) throws MalformedTorrentFileException;
9+
TorrentMetadata parseToMetadata(byte[] bytes, boolean computeInfoHash) throws MalformedTorrentFileException;
1010

1111
Map<String, Object> parseToMap(byte[] bytes, boolean computeInfoHash) throws MalformedTorrentFileException;
1212
}

src/main/java/com/robothaver/torrentfileparser/parser/TorrentFileParserImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.robothaver.torrentfileparser.parser;
22

3-
import com.robothaver.torrentfileparser.domain.Torrent;
3+
import com.robothaver.torrentfileparser.domain.TorrentMetadata;
44
import com.robothaver.torrentfileparser.exception.MalformedTorrentFileException;
55

66
import java.util.Map;
77

88
public class TorrentFileParserImpl implements TorrentFileParser {
99
@Override
10-
public Torrent parseToTorrent(byte[] bytes, boolean computeInfoHash) throws MalformedTorrentFileException {
11-
return new ParseWorker(bytes, computeInfoHash).parseToTorrent();
10+
public TorrentMetadata parseToMetadata(byte[] bytes, boolean computeInfoHash) throws MalformedTorrentFileException {
11+
return new ParseWorker(bytes, computeInfoHash).parseToMetadata();
1212
}
1313

1414
@Override

0 commit comments

Comments
 (0)