Make input streams buffered and close them after use

Several input streams were unbuffered and others were not closed
after use.

(cherry picked from commit 1cf4d85199faa172d2f8ced2341ce1e86938a2e2)