Package com.frostwire.jlibtorrent
Class Entry
java.lang.Object
com.frostwire.jlibtorrent.Entry
Variant container for bencoded data structures.
Entry represents a single node in a bencoded hierarchical data structure.
Bencoding is the binary encoding format used by BitTorrent for .torrent files, magnet
links metadata, DHT messages, and tracker communications. It can encode four types:
- String: Binary-safe byte sequences
- Integer: Signed 64-bit numbers
- List: Ordered sequences of other entries
- Dictionary: Key-value maps (keys are always strings)
Parsing Bencoded Data:
// Parse .torrent file
byte[] torrentData = Files.readAllBytes(Paths.get("download.torrent"));
Entry root = Entry.bdecode(torrentData);
// Or from file directly
Entry root = Entry.bdecode(new File("download.torrent"));
// Root is typically a dictionary
Map<String, Entry> dict = root.dictionary();
Entry announce = dict.get("announce");
String trackerUrl = announce.string();
Understanding the Structure:
// Bencoded data structure:
// {
// "announce": "http://tracker.example.com:80/announce",
// "info": {
// "name": "filename.iso",
// "piece length": 262144,
// "pieces": [binary hash data],
// "length": 4294967296
// },
// "creation date": 1234567890
// }
Entry root = Entry.bdecode(torrentData);
Map<String, Entry> dict = root.dictionary();
// Strings
String announce = dict.get("announce").string();
// Nested dictionaries
Map<String, Entry> info = dict.get("info").dictionary();
String name = info.get("name").string();
long pieceLength = info.get("piece length").integer();
// Lists (piece hashes are typically a binary string with 20-byte chunks)
Entry pieces = info.get("pieces");
String piecesData = pieces.string();
Creating Bencoded Data:
// Create from primitive types
Entry stringEntry = new Entry("hello");
Entry intEntry = new Entry(42L);
// Create from Java collections
List<Object> list = Arrays.asList(
"item1",
42,
new Entry("nested")
);
Entry listEntry = Entry.fromList(list);
// Create nested dictionary
Map<String, Object> dict = new HashMap<>();
dict.put("name", "My Torrent");
dict.put("size", 1000000);
Entry dictEntry = Entry.fromMap(dict);
Encoding and Converting:
Entry entry = Entry.bdecode(torrentData);
// Get string representation (for debugging)
String debug = entry.toString(); // Human-readable format
// Re-encode to bencoded binary
byte[] bencoded = entry.bencode();
Files.write(Paths.get("output.torrent"), bencoded);
// Extract values
String str = entry.string(); // For string entries
long num = entry.integer(); // For integer entries
List<Entry> list = entry.list(); // For list entries
Map<String, Entry> dict = entry.dictionary(); // For dictionary entries
Common Patterns - DHT Immutable Items:
// DHT can store and retrieve bencoded data
SessionManager sm = ...;
Sha1Hash key = new Sha1Hash("...");
// Retrieve immutable DHT item
Entry item = sm.dhtGetItem(key, timeout);
if (item != null) {
System.out.println("DHT item: " + item.toString());
// If it's a dictionary
Map<String, Entry> data = item.dictionary();
for (String k : data.keySet()) {
System.out.println(k + " = " + data.get(k));
}
}
Performance Notes: - Creating from lists/maps involves copying data into bencoded structures - Large files may have corresponding large Entry objects in memory - Consider streaming for very large torrent files
- See Also:
-
Constructor Details
-
Entry
public Entry(com.frostwire.jlibtorrent.swig.entry e) -
Entry
-
Entry
public Entry(long n)
-
-
Method Details
-
swig
public com.frostwire.jlibtorrent.swig.entry swig() -
bencode
public byte[] bencode() -
string
-
integer
public long integer() -
list
-
dictionary
-
toString
-
bdecode
-
bdecode
- Throws:
IOException
-
fromList
-
fromMap
-