torrent_bencode 0.1.0+1

This library provides classes to encode/decode bencoded bytes. Mainly used to implement bittorrent related functions.

Features #

  • Decode/encode bencoded bytes.

Usage #

Example:

import 'dart:typed_data';
import 'package:bencode/bencode.dart';

main() async {
  var decoded = bDecoder.convertString('d5:hello5:worlde');
  // or final decoded = bDecoder.convert('d5:hello5:worlde'.runes.toString());
  print(decoded);
  // Output: {hello: world}
  print(decoded is Map);
  // Output: true

  decoded = bDecoder.convertString('i123e');
  print(decoded);
  // Output: 123
  print(decoded is int);
  // Output: true

  decoded = bDecoder.convertString('li123ei123ee');
  print(decoded);
  // Output: [123, 123]
  print(decoded is List);
  // Output: true

  decoded = bDecoder.convertString('11:hello world');
  print(decoded);
  // bencoded strings are converted to bytes by default.
  // Output: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  print(String.fromCharCodes(decoded));
  // Output: hello world
  print(decoded is List<int>);
  // Output: true

  var encoded = bEncoder.convert({'hello': 'world'});
  print(encoded);
  // Output: [100, 53, 58, 104, 101, 108, 108, 111, 53, 58, 119, 111, 114, 108, 100, 101]
  print(String.fromCharCodes(encoded));
  // Output: d5:hello5:worlde

  encoded = bEncoder.convert(123);
  print(String.fromCharCodes(encoded));
  // Output: i123e

  encoded = bEncoder.convert([123, 456]);
  print(String.fromCharCodes(encoded));
  // Output: li123ei456ee

  encoded = bEncoder.convert(Uint8List.fromList([123, 123]));
  print(encoded);
  // Output: [50, 58, 123, 123]
  print(String.fromCharCodes(encoded));
  // Output: 2:{{
}

Decode bittorrent files:

import 'dart:io';
import 'package:bencode/bencode.dart';

main() async {
  final data = File('test/multi.torrent').readAsBytesSync();
  final decoded = bDecoder.convert(data);
  print(decoded.keys);
  // Output: (announce, announce-list, created by, creation date, encoding, info)
}

Tests #

Run tests:

pub run test

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.1.0+1 #

  • Update examples

0.1.0 #

  • Initial version, created by Stagehand

example/torrent_bencode.dart

import 'dart:typed_data';
import 'package:torrent_bencode/torrent_bencode.dart';

main() async {
  var decoded = bDecoder.convertString('d5:hello5:worlde');
  // or final decoded = bDecoder.convert('d5:hello5:worlde'.runes.toString());
  print(decoded);
  // Output: {hello: world}
  print(decoded is Map);
  // Output: true

  decoded = bDecoder.convertString('i123e');
  print(decoded);
  // Output: 123
  print(decoded is int);
  // Output: true

  decoded = bDecoder.convertString('li123ei123ee');
  print(decoded);
  // Output: [123, 123]
  print(decoded is List);
  // Output: true

  decoded = bDecoder.convertString('11:hello world');
  print(decoded);
  // bencoded strings are converted to bytes by default.
  // Output: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  print(String.fromCharCodes(decoded));
  // Output: hello world
  print(decoded is List<int>);
  // Output: true

  var encoded = bEncoder.convert({'hello': 'world'});
  print(encoded);
  // Output: [100, 53, 58, 104, 101, 108, 108, 111, 53, 58, 119, 111, 114, 108, 100, 101]
  print(String.fromCharCodes(encoded));
  // Output: d5:hello5:worlde

  encoded = bEncoder.convert(123);
  print(String.fromCharCodes(encoded));
  // Output: i123e

  encoded = bEncoder.convert([123, 456]);
  print(String.fromCharCodes(encoded));
  // Output: li123ei456ee

  encoded = bEncoder.convert(Uint8List.fromList([123, 123]));
  print(encoded);
  // Output: [50, 58, 123, 123]
  print(String.fromCharCodes(encoded));
  // Output: 2:{{
}

Use this package as a library

1. Depend on it

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


dependencies:
  torrent_bencode: ^0.1.0+1

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:torrent_bencode/torrent_bencode.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
23
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 Aug 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:torrent_bencode/torrent_bencode.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
Dev dependencies
pedantic ^1.0.0
test ^1.0.0