]> source.dussan.org Git - sonarqube.git/commitdiff
Close all resources automatically
authorZipeng WU <zipeng.wu@sonarsource.com>
Fri, 11 Dec 2020 16:40:56 +0000 (17:40 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 14 Dec 2020 20:07:14 +0000 (20:07 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/notification/NotificationQueueDto.java

index c6a8080024870a56beaaac85943de4a0ab58d34c..b7516aabdc0b548b84f1d9f8cfd711428721a3a0 100644 (file)
@@ -24,7 +24,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 import org.sonar.api.notifications.Notification;
@@ -72,18 +71,14 @@ public class NotificationQueueDto {
   }
 
   public static <T extends Notification> NotificationQueueDto toNotificationQueueDto(T notification) {
-    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-    try {
-      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
+    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
       objectOutputStream.writeObject(notification);
       objectOutputStream.close();
       return new NotificationQueueDto().setData(byteArrayOutputStream.toByteArray());
 
     } catch (IOException e) {
       throw new SonarException("Unable to write notification", e);
-
-    } finally {
-      IOUtils.closeQuietly(byteArrayOutputStream);
     }
   }
 
@@ -92,15 +87,11 @@ public class NotificationQueueDto {
       return null;
     }
 
-    ByteArrayInputStream byteArrayInputStream = null;
-    try {
-      byteArrayInputStream = new ByteArrayInputStream(this.data);
-      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
+    try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.data);
+      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
       Object result = objectInputStream.readObject();
       objectInputStream.close();
       return (T) result;
-    } finally {
-      IOUtils.closeQuietly(byteArrayInputStream);
     }
   }