From 07c06cfbebbdb104171df70e2d6db4cd7a57067a Mon Sep 17 00:00:00 2001
From: ivan <ivan@ivanLaptop>
Date: Wed, 23 Sep 2015 18:12:15 +0200
Subject: [PATCH] [s3] added try-with-resources

---
 .../main/java/com/yahoo/ycsb/db/S3Client.java | 32 +++++++++----------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java b/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java
index 8beb52d5..c50a1778 100644
--- a/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java
+++ b/s3/src/main/java/com/yahoo/ycsb/db/S3Client.java
@@ -336,28 +336,26 @@ public class S3Client extends DB {
       System.arraycopy(sourceArray, 0, destinationArray, offset, sizeArray);
       offset += sizeArray;
     }
-    InputStream input = new ByteArrayInputStream(destinationArray);
-    ObjectMetadata metadata = new ObjectMetadata();
-    metadata.setContentLength(totalSize);
-    try {
-      PutObjectResult res = 
-          s3Client.putObject(bucket, key, input, metadata);
-      if(res.getETag() == null) {
-        return 1;
-      }
-    } catch (Exception e) {
-      System.err.println("Not possible to write object :"+key);
-      e.printStackTrace();
-      return 1;
-    } finally {
+    try (InputStream input = new ByteArrayInputStream(destinationArray)) {
+      ObjectMetadata metadata = new ObjectMetadata();
+      metadata.setContentLength(totalSize);
       try {
-        input.close();
+        PutObjectResult res = 
+            s3Client.putObject(bucket, key, input, metadata);
+        if(res.getETag() == null) {
+          return 1;
+        }
       } catch (Exception e) {
-        System.err.println("Not possible to close the stream :"+e.toString());
+        System.err.println("Not possible to write object :"+key);
         e.printStackTrace();
         return 1;
+      } finally {
+        return 0;
       }
-      return 0;
+    } catch (Exception e) {
+      System.err.println("Error in the creation of the stream :"+e.toString());
+      e.printStackTrace();
+      return 1;
     }
   }
 
-- 
GitLab