Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
refactor JavaSerializer for proper closing input/output streams (#3031)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neng Lu authored Sep 20, 2018
1 parent 72509b9 commit ebda8e7
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,23 @@ public void initialize(Map<String, Object> config) {

@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();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Failed to serialize object: " + object.toString(), e);
}
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);
throw new RuntimeException("Failed to deserialize object", e);
}
}
}

0 comments on commit ebda8e7

Please sign in to comment.