You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@heron.apache.org by nl...@apache.org on 2018/09/19 21:57:46 UTC

[incubator-heron] branch master updated: refactor JavaSerializer for closing input/output stream

This is an automated email from the ASF dual-hosted git repository.

nlu90 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-heron.git


The following commit(s) were added to refs/heads/master by this push:
     new db3f1c4  refactor JavaSerializer for closing input/output stream
db3f1c4 is described below

commit db3f1c406c7ff73113e2c469b44636baee1eb77f
Author: Neng Lu <nl...@twitter.com>
AuthorDate: Wed Sep 19 14:56:56 2018 -0700

    refactor JavaSerializer for closing input/output stream
---
 .../apache/heron/api/serializer/JavaSerializer.java  | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/heron/api/src/java/org/apache/heron/api/serializer/JavaSerializer.java b/heron/api/src/java/org/apache/heron/api/serializer/JavaSerializer.java
index 75a1fba..cb942db 100644
--- a/heron/api/src/java/org/apache/heron/api/serializer/JavaSerializer.java
+++ b/heron/api/src/java/org/apache/heron/api/serializer/JavaSerializer.java
@@ -34,25 +34,23 @@ public class JavaSerializer implements IPluggableSerializer {
 
   @Override
   public byte[] serialize(Object object) {
-    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-    try {
-      ObjectOutputStream oos = new ObjectOutputStream(bos);
+    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
+         ObjectOutputStream oos = new ObjectOutputStream(bos)) {
       oos.writeObject(object);
       oos.flush();
-    } catch (IOException e) {
-      throw new RuntimeException("Failed to serialize object: " + object.toString(), e);
+      return bos.toByteArray();
+    } catch (IOException ex) {
+      throw new RuntimeException("Failed to serialize object: " + object.toString(), ex);
     }
-    return bos.toByteArray();
   }
 
   @Override
   public Object deserialize(byte[] input) {
-    ByteArrayInputStream bis = new ByteArrayInputStream(input);
-    try {
-      ObjectInputStream ois = new ObjectInputStream(bis);
+    try (ByteArrayInputStream bis = new ByteArrayInputStream(input);
+          ObjectInputStream ois = new ObjectInputStream(bis)) {
       return ois.readObject();
-    } catch (IOException | ClassNotFoundException e) {
-      throw new RuntimeException(e);
+    } catch (IOException | ClassNotFoundException ex) {
+      throw new RuntimeException(ex);
     }
   }
 }