Don't write to output stream if any error occurs.
Merge pull request #34 from hankbao/master

Make sure we don't write to output stream if any error occurs.