try-with-resource to prevent resource leaks
CLOUDSTACK-6856 : Fixed few data corruption and retries issues for failing cases

Signed-off-by: SrikanteswaraRao Talluri <talluri@apache.org>