Project

bencode

0.05
Low commit activity in last 3 years
A long-lived project that still receives updates
A simple encoder and decoder for the BitTorrent serialization format.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Ruby bencode binding

This is a simple library for reading and writing bencoded data.

What is bencode?

Bencode is a simple data serialization format used by the popular BitTorrent P2P file sharing system.

It contains only four data types, namely:

  • byte strings
  • integers
  • lists
  • dictionaries

Examples

Encoding objects is as simple as calling #bencode on them:

"foo bar".bencode                   # => "7:foo bar"
42.bencode                          # => "i42e"
[1, 2, 3].bencode                   # => "li1ei2ei3ee"
{"foo" => 1, "bar" => -10}.bencode  # => "d3:bari-10e3:fooi1ee"

Decoding a complete data stream is as easy as calling BEncode.load(data).

Decoding a data stream in chunks works as follows:

irb(main):007:0> stream = BEncode::Parser.new(StringIO.new "d3:foo3:bared3:baz3:quxe")
=> #<BEncode::Parser:0x007fbe6b008c88 @stream=#<StringIO:0x007fbe6b008cd8>>
irb(main):008:0> stream.parse!
=> {"foo"=>"bar"}
irb(main):009:0> stream.parse!
=> {"baz"=>"qux"}

License

Released under the MIT license.

Contributors