From c2c63ffd613649a18df6703d4f46ccf8619aa8a1 Mon Sep 17 00:00:00 2001 From: Santhosh Edukulla Date: Fri, 25 Jul 2014 17:12:46 +0530 Subject: [PATCH] Fixed Coverity Issue --- .../bridge/io/S3FileSystemBucketAdapter.java | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/awsapi/src/com/cloud/bridge/io/S3FileSystemBucketAdapter.java b/awsapi/src/com/cloud/bridge/io/S3FileSystemBucketAdapter.java index 85d1e0ed759..a0834c244d2 100644 --- a/awsapi/src/com/cloud/bridge/io/S3FileSystemBucketAdapter.java +++ b/awsapi/src/com/cloud/bridge/io/S3FileSystemBucketAdapter.java @@ -152,30 +152,33 @@ public class S3FileSystemBucketAdapter implements S3BucketAdapter { file.delete(); file.createNewFile(); - final FileOutputStream fos = new FileOutputStream(file); - byte[] buffer = new byte[4096]; + try(final FileOutputStream fos = new FileOutputStream(file);) { + byte[] buffer = new byte[4096]; - // -> get the input stream for the next file part - for (int i = 0; i < parts.length; i++) { - DataHandler nextPart = loadObject(mountedRoot, sourceBucket, parts[i].getPath()); - InputStream is = nextPart.getInputStream(); + // -> get the input stream for the next file part + for (int i = 0; i < parts.length; i++) { + DataHandler nextPart = loadObject(mountedRoot, sourceBucket, parts[i].getPath()); + InputStream is = nextPart.getInputStream(); - int len = 0; - while ((len = is.read(buffer)) > 0) { - fos.write(buffer, 0, len); - md5.update(buffer, 0, len); - totalLength += len; - } - is.close(); - - // -> after each file write tell the client we are still here to keep connection alive - if (null != client) { - client.write(new String(" ").getBytes()); - client.flush(); + int len = 0; + while ((len = is.read(buffer)) > 0) { + fos.write(buffer, 0, len); + md5.update(buffer, 0, len); + totalLength += len; + } + is.close(); + + // -> after each file write tell the client we are still here to keep connection alive + if (null != client) { + client.write(new String(" ").getBytes()); + client.flush(); + } } + return new OrderedPair(StringHelper.toHexString(md5.digest()), new Long(totalLength)); + }catch (IOException e) { + logger.error("concatentateObjects unexpected exception " + e.getMessage(), e); + throw new OutOfStorageException(e); } - fos.close(); - return new OrderedPair(StringHelper.toHexString(md5.digest()), new Long(totalLength)); //Create an ordered pair whose first element is the MD4 digest as a (lowercase) hex String } catch (IOException e) { logger.error("concatentateObjects unexpected exception " + e.getMessage(), e);