All Projects → dampcake → bencode

dampcake / bencode

Licence: Apache-2.0 license
Bencode Input/Output Streams for Java

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to bencode

bencode.py
Simple bencode parser (for Python 2, Python 3 and PyPy)
Stars: ✭ 28 (-42.86%)
Mutual labels:  bencode, bencode-parser
bencoder.pyx
A fast bencode implementation in Cython
Stars: ✭ 26 (-46.94%)
Mutual labels:  bencode, bencoder
BencodeNET
.NET library for encoding/decoding bencode and reading/writing torrent files
Stars: ✭ 133 (+171.43%)
Mutual labels:  bencode, bencode-parser
php-bencode
C++ PHP extension which can boost the process of encoding and decoding of Bencode.
Stars: ✭ 16 (-67.35%)
Mutual labels:  bencode
torrentool
The tool to work with torrent files.
Stars: ✭ 96 (+95.92%)
Mutual labels:  bencode
bento
🍱 A fast, correct, pure-Elixir library for reading and writing Bencoded metainfo (.torrent) files.
Stars: ✭ 71 (+44.9%)
Mutual labels:  bencode
bencode
A netstring and bencode implementation for Clojure.
Stars: ✭ 44 (-10.2%)
Mutual labels:  bencode
bencode online
Bencode & bdecode in your browser
Stars: ✭ 24 (-51.02%)
Mutual labels:  bencode
bencode
PHP Bencode (BitTorrent) Encoder/Decoder
Stars: ✭ 19 (-61.22%)
Mutual labels:  bencode

bencode

Build Status Coverage Status Maven GitHub license

Bencode Input/Output Streams for Java

Requires JDK 1.8 or higher

Bencode Spec

Bencode Wikipedia

Javadoc

http://dampcake.github.io/bencode

Usage

Maven

<dependency>
    <groupId>com.dampcake</groupId>
    <artifactId>bencode</artifactId>
    <version>1.4</version>
</dependency>

Gradle

compile 'com.dampcake:bencode:1.4'

Examples

Bencode Data

Bencode bencode = new Bencode();
byte[] encoded = bencode.encode(new HashMap<Object, Object>() {{
    put("string", "value");
    put("number", 123456);
    put("list", new ArrayList<Object>() {{
        add("list-item-1");
        add("list-item-2");
    }});
    put("dict", new ConcurrentSkipListMap() {{
        put(123, "test");
        put(456, "thing");
    }});
}});

System.out.println(new String(encoded, bencode.getCharset()));

Outputs: d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee

Decode Bencoded Data:

Bencode bencode = new Bencode();
Map<String, Object> dict = bencode.decode("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee".getBytes(), Type.DICTIONARY);

System.out.println(dict);

Outputs: {dict={123=test, 456=thing}, list=[list-item-1, list-item-2], number=123456, string=value}

Write bencoded data to a Stream:

ByteArrayOutputStream out = new ByteArrayOutputStream();
BencodeOutputStream bencoder = new BencodeOutputStream(out);

bencoder.writeDictionary(new HashMap<Object, Object>() {{
    put("string", "value");
    put("number", 123456);
    put("list", new ArrayList<Object>() {{
        add("list-item-1");
        add("list-item-2");
    }});
    put("dict", new ConcurrentSkipListMap() {{
        put("dict-item-1", "test");
        put("dict-item-2", "thing");
    }});
}});

System.out.println(new String(out.toByteArray()));

Outputs: d4:dictd11:dict-item-14:test11:dict-item-25:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee

Read bencoded data to a Stream:

String input = "d4:dictd11:dict-item-14:test11:dict-item-25:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee";
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
BencodeInputStream bencode = new BencodeInputStream(in);

Type type = bencode.nextType(); // Returns Type.DICTIONARY
Map<String, Object> dict = bencode.readDictionary();

System.out.println(dict);

Outputs: {dict={dict-item-1=test, dict-item-2=thing}, list=[list-item-1, list-item-2], number=123456, string=value}

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].