// import 'dart:convert';
import 'dart:io';
main() async {
var src = File('./lib/convert/source.txt');
var output = File('./lib/convert/output.txt');
var ins = src.openRead();
var encoded = convert(ins, 128);
var outs = output.openWrite();
await encoded.pipe(outs);
outs.close();
// the second transform.
var output2 = File('./lib/convert/output2.txt');
var outs2 = output2.openWrite();
await convert(output.openRead(), -128).pipe(outs2);
outs2.close();
}
Stream<List<int>> convert(Stream<List<int>> source,
[int key = 128, int base = 256]) async* {
await for (var chunk in source) {
yield encode(chunk, key, base);
}
}
List<int> encode(List<int> original, [int key = 128, int base = 256]) {
var encoded = List<int>(original.length);
for (var i = 0; i < original.length; i++) {
encoded[i] = (original[i] + key) % base;
}
return encoded;
}