Skip to content

Commit 5ba03fa

Browse files
author
Dan Blaisdell
committed
make sure input and output streams are closed properly
1 parent 492f754 commit 5ba03fa

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/main/java/com/arhs/spring/cache/mongo/serializer/HessianSerializer.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,54 @@
77

88
import java.io.ByteArrayInputStream;
99
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
1011

11-
/**
12-
* Adapts HessianSerializer so it can be used in Redis. User: msellers Date:
13-
* 9/3/14 Time: 10:17 AM
14-
*/
1512
public class HessianSerializer implements Serializer {
1613

17-
private static final Logger m_logger = LoggerFactory.getLogger(HessianSerializer.class);
14+
private static final Logger LOGGER = LoggerFactory.getLogger(HessianSerializer.class);
1815

1916
@Override
2017
public byte[] serialize(Object obj) {
21-
try {
18+
Hessian2Output out = null;
19+
20+
try (
2221
ByteArrayOutputStream bos = new ByteArrayOutputStream();
23-
Hessian2Output out = new Hessian2Output(bos);
22+
) {
23+
out = new Hessian2Output(bos);
24+
2425
out.startMessage();
2526
out.writeObject(obj);
2627
out.completeMessage();
2728
out.close();
28-
byte[] array = bos.toByteArray();
2929

30-
return array;
30+
return bos.toByteArray();
3131
} catch (Exception e) {
32-
m_logger.error("Error Serializing a value to be put into the Redis cache", e);
32+
if (out != null) {
33+
try {
34+
out.close();
35+
} catch (IOException ex) {
36+
}
37+
}
38+
LOGGER.error("Error Serializing a value to be put into the Redis cache", e);
3339
throw new RuntimeException(e.getMessage());
3440
}
3541
}
3642

3743
@Override
3844
public Object deserialize(byte[] bytes) {
39-
try {
40-
Object toReturn = null;
41-
42-
if (bytes != null) {
43-
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
44-
Hessian2Input in = new Hessian2Input(bin);
45-
in.startMessage();
45+
if (bytes == null) {
46+
return null;
47+
}
4648

47-
toReturn = in.readObject();
48-
}
49+
try (
50+
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
51+
) {
52+
Hessian2Input in = new Hessian2Input(bin);
53+
in.startMessage();
4954

50-
return toReturn;
55+
return in.readObject();
5156
} catch (Exception e) {
52-
m_logger.error("Error De-Serializing a value from the Redis cache", e);
57+
LOGGER.error("Error De-Serializing a value from the Redis cache", e);
5358
throw new RuntimeException(e.getMessage());
5459
}
5560
}

src/test/java/com/arhs/spring/cache/mongo/serializer/HessianSerializerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ public void testSerializeDeserialize() throws IOException, ClassNotFoundExceptio
3737
Assert.assertEquals(date, out.date);
3838
}
3939

40-
4140
}

0 commit comments

Comments
 (0)