When serializing to a stream, flush its stream writer when finished

Fixes #27.