SPARK-1656: Fix potential resource leaks

JIRA: https://issues.apache.org/jira/browse/SPARK-1656
Why not do the full `try catch finally` here, and put `outputStream.close()` in finally?