bencoding 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 62

Bencoding #

pub package

A dart library for encoding and decoding bencoded data, according to the BitTorrent specification.

Index #

About BEncoding #

from Wikipedia:

Bencode (pronounced like B encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.

It supports four different types of values:

  • byte strings
  • integers
  • lists
  • dictionaries

Bencoding is most commonly used in torrent files. These metadata files are simply bencoded dictionaries.

Usage #

Import #

import 'package:bencoding/bencode.dart' as bencode;

Encoding #

final data = {
  'string': 'Hello World',
  'integer': 12345,
  'dict': {
    'key': 'This is a string within a dictionary',
  },
  'list': [ 1, 2, 3, 4, 'string', 5, {} ],
};

final buffer = bencode.encode(data); // Return a Uint8List
final string = bencode.encodeToString(data); // Return a String

print('Buffer: $buffer');
print('String: $string');

Output

Buffer: [100, 54, 58, 115, 116, 114, 105, 110, 103, 49, 49, ...]
String: d6:string11:Hello World7:integeri12345e4:dictd3:key36:This is ...

Decoding #

final bencodedData = 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6:stringi5edeee';

print(bencode.decodeString(bencodedData));
// or you can use bencode.deocode(data) if your input data is a buffer (Uint8List)

Output

{
  'string': 'Hello World',
  'integer': 12345,
  'dict': {
    'key': 'This is a string within a dictionary',
  },
  'list': [ 1, 2, 3, 4, 'string', 5, {}],
}

API #

encode( dynamic data ) #

String | int | List | Map data

Returns Uint8List

encodeToString ( dynamic data ) #

String | int | List | Map data

Returns String

decode( Uint8List data ) #

Returns dynamic (String | int | List | Map)

decodeString ( String data ) #

Returns dynamic (String | int | List | Map)

1.0.0 #

  • Now the decoder check if strings are utf8 chars only, if is not the case the result will be a Uint8List

0.1.2 #

  • Updating README.md

0.1.1 #

  • Cleaning & formating

0.1.0 #

  • First version with every based features

example/main.dart

import 'package:bencoding/bencode.dart' as bencode;

void main() {
  final data = {
    'string': 'Hello World',
    'integer': 12345,
    'dict': {
      'key': 'This is a string within a dictionary',
    },
    'list': [1, 2, 3, 4, 'string', 5, {}],
  };

  final buffer = bencode.encode(data); // Return a Uint8List
  final string = bencode.encodeToString(data); // Return a String

  print('Buffer: $buffer');
  print('String: $string');

  final bencodedData = 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6:stringi5edeee';

  print(bencode.decodeString(bencodedData));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  bencoding: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:bencoding/bencode.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
24
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Mar 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Format lib/src/decoder.dart.

Run dartfmt to format lib/src/decoder.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0