Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

10 Commits

Repository files navigation

B-Encode

Build StatusCoverage StatusMaven Central

This library allows you to encode and decode B-encoded documents.

Before:

Map<String,BEncodedValue> document = newHashMap<String, BEncodedValue>() {{
put("string", newBEncodedValue("value"));
put("number", newBEncodedValue(123456));
put("list", newBEncodedValue(newArrayList<BEncodedValue>() {{
add(newBEncodedValue("list-item-1"));
add(newBEncodedValue("list-item-2"));
}}));
put("dict", newBEncodedValue(newHashMap<String, BEncodedValue>() {{
put("123", newBEncodedValue("test"));
put("456", newBEncodedValue("thing"));
}}));
}};
ByteArrayOutputStreambaos = newByteArrayOutputStream();
BEncoder.encode(document, baos);
StringencodedDocument = newString(baos.toByteArray());

After:

d
4:dict d
3:123	4:test
3:456	5:thing
e
4:list l
11:list-item-1
11:list-item-2
e
6:number	i123456e
6:string	5:value
e

The library can do the reverse operation as well: decode.

I want to decode a torrent file. How can I do that?

First you need to read the torrent and decode it:

FiletorrentFile = "file.torrent";
FileInputStreaminputStream = newFileInputStream(torrentFile);
BDecoderreader = newBDecoder(inputStream);
Map<String, BEncodedValue> document = reader.decodeMap().getMap();

Then you can start extracting information from it:

Stringannounce = document.get("announce").getString(); // StringsMap<String, BEncodedValue> info = document.get("info").getMap(); // MapsList<BEncodedValue> files = info.get("files").getList(); // Lists

You can check if a key exists using the 'containsKey' method as such:

if (document.containsKey("info")) {
System.out.println("The info field exists");
}

Use the library

The documentation page lists how you can add this library as a dependency of your project.

Credit

This library was created since no other viable libraries existed, and the ttorrent project contains a bunch of unecessary content. It is based on the work of Maxime Petazzoni.

About

Encode and decode B-encoded content

Topics

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages