Skip to content

Commit ac77a0f

Browse files
committed
Made messageDigest static, fixed parser not resetting iterator after parsing to map, refactored project structure
1 parent ffa1e9e commit ac77a0f

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.robothaver.torrentfileparser.domain.Torrent;
44
import com.robothaver.torrentfileparser.exception.MalformedTorrentFileException;
5+
import com.robothaver.torrentfileparser.parser.TorrentFileParser;
56

67
import java.io.IOException;
78
import java.nio.file.Files;
@@ -17,7 +18,7 @@ public static void main(String[] args) {
1718
byte[] bytes = Files.readAllBytes(path);
1819
Instant start = Instant.now();
1920

20-
TorrentFileParser torrentParser = new TorrentFileParser(bytes);
21+
TorrentFileParser torrentParser = new TorrentFileParser(bytes, true);
2122
Torrent torrent = torrentParser.parseToTorrent();
2223
System.out.println(torrent.getName());
2324

src/main/java/com/robothaver/torrentfileparser/domain/InfoHasCompute.java renamed to src/main/java/com/robothaver/torrentfileparser/parser/InfoHasCompute.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
package com.robothaver.torrentfileparser.domain;
1+
package com.robothaver.torrentfileparser.parser;
22

33
import java.security.MessageDigest;
44
import java.security.NoSuchAlgorithmException;
55
import java.util.HexFormat;
66

77
public class InfoHasCompute {
8-
public static String getInfoHash(byte[] infoBytes) {
8+
private static final MessageDigest messageDigest;
9+
10+
static {
911
try {
10-
return getSHAsum(infoBytes);
12+
messageDigest = MessageDigest.getInstance("SHA-1");
1113
} catch (NoSuchAlgorithmException e) {
12-
throw new RuntimeException(e);
14+
throw new IllegalStateException(e);
1315
}
1416
}
1517

16-
private static String getSHAsum(byte[] input) throws NoSuchAlgorithmException {
17-
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
18-
return byteArrayToHex(messageDigest.digest(input));
18+
private InfoHasCompute() {
19+
throw new IllegalStateException("Utility class");
20+
}
21+
22+
public static String getInfoHash(byte[] infoBytes) {
23+
return byteArrayToHex(messageDigest.digest(infoBytes));
1924
}
2025

2126
private static String byteArrayToHex(byte[] bytes) {

src/main/java/com/robothaver/torrentfileparser/domain/TorrentBuilder.java renamed to src/main/java/com/robothaver/torrentfileparser/parser/TorrentBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package com.robothaver.torrentfileparser.domain;
1+
package com.robothaver.torrentfileparser.parser;
2+
3+
import com.robothaver.torrentfileparser.domain.Torrent;
4+
import com.robothaver.torrentfileparser.domain.TorrentFile;
25

36
import java.util.List;
47
import java.util.Map;
@@ -27,6 +30,7 @@ public void processKeyValue(String key, Object value) {
2730
case "pieces" -> torrent.setPieces(String.valueOf(value));
2831
case "comment" -> torrent.setComment(String.valueOf(value));
2932
case "private" -> torrent.setPrivate((long) value == 1);
33+
default -> throw new IllegalStateException("Unknown key!");
3034
}
3135
}
3236

src/main/java/com/robothaver/torrentfileparser/TorrentFileParser.java renamed to src/main/java/com/robothaver/torrentfileparser/parser/TorrentFileParser.java

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

33
import com.robothaver.torrentfileparser.domain.Torrent;
4-
import com.robothaver.torrentfileparser.domain.TorrentBuilder;
54
import com.robothaver.torrentfileparser.exception.MalformedTorrentFileException;
65

76
import java.util.*;
@@ -41,7 +40,9 @@ public Torrent parseToTorrent() throws MalformedTorrentFileException {
4140
}
4241

4342
public Map<String, Object> parseToMap() throws MalformedTorrentFileException {
44-
return (Map<String, Object>) parse();
43+
Object parse = parse();
44+
iterator = 0;
45+
return (Map<String, Object>) parse;
4546
}
4647

4748
private boolean isInt(byte currentByte) {

0 commit comments

Comments
 (0)