[#1880] Close resources in finally block to avoid file handle leaks in case of exceptions
[#1880] Use commons-io API to fix wrong usage of InputStream.read()
[#1880] Use commons-io API to avoid re-inventing the wheel