Git Product home page Git Product logo

bencode's Introduction

Bencode

Java implementation of the Bencode format.

Build Status Coverage Status

Usage examples

US-ASCII

As the original algorithm specifies, default charset encoding is US-ASCII. So, this charset is used by default.

See EncoderDecoderExample.java.

ByteArrayOutputStream out = new ByteArrayOutputStream();
BEncoder encoder = new BEncoder(out);

List<Object> data = asList(1, "foo", asList("1", 42),
        singletonMap("key", asList("foo", singletonMap("a", 42))));

for (Object datum : data) {
    encoder.encode(datum);
}

InputStream in = new ByteArrayInputStream(out.toByteArray());
BDecoder decoder = new BDecoder(in);

for (Object e : decoder) {
    System.out.println(e);
}

Output:

1
foo
[1, 42]
{key=[foo, {a=42}]}

Other charset encodings

If you need, for example Cyrillic alphabet's symbols support, you can use the UTF-8 charset.

See EncoderDecoderUtf8Example.java.

Charset charset = Charset.forName("UTF-8");

ByteArrayOutputStream out = new ByteArrayOutputStream();
BEncoder encoder = new BEncoder(out, charset);

List<Object> data = asList(1, "слово", asList("один", 42),
        singletonMap("ключ", asList("значение 1", singletonMap("ключ 2", 42))));

for (Object datum : data) {
    encoder.encode(datum);
}

InputStream in = new ByteArrayInputStream(out.toByteArray());
BDecoder decoder = new BDecoder(in, charset);

for (Object e : decoder) {
    System.out.println(e);
}

Output:

1
слово
[один, 42]
{ключ=[значение 1, {ключ 2=42}]}

Links

bencode's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.