wrap all closeables in a try finally block

Shell wasn't closing closable resources, so this patch just closes resources opened.
" byte[] buf = new byte[512];"

doesn't need to be in the try block