Close the stream properly