Java implementation of the Bencode format.
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}]}
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}]}