cjblomqvist/base64Stream — pull request #1 by sihorton (2012-11-19 22:18:11+00:00)
Title: Fix for multiple pipes

I have just found your module today and tried to read a file from disk, encode to base64 and stream out to another file:

var fs = require("fs")
base64Stream = require('base64Stream');

var read = fs.createReadStream(__dirname+"/example.txt");
var base = new base64Stream.BufferedStreamToBase64();
var write = fs.createWriteStream(__dirname+"/base64.txt");

read.pipe(base).pipe(write);

However in the output file I noticed that the oddBytes were repeated two times e.g. the end of the file was as follows:

KTs=KTs=

I changed the last line of the example code to the following:

read.pipe(base);
base.pipe(write);

But I got the same behaviour on all of the files that I tried. I just guessed that maybe the end event is being called twice so I changed the code and added a "sendOdd" flag. I set that whenever the oddBytes are sent after an end event, and then check it has not already been set before sending the oddBytes data. This fixes the issue for me.

/Simon