Significant stream improvements.

This should make the stream API much more stable, particularly the
flushing part.

Signed-off-by: Evan Nemerson <evan@coeus-group.com>