|
7 | 7 |
|
8 | 8 | import java.io.ByteArrayInputStream;
|
9 | 9 | import java.io.ByteArrayOutputStream;
|
| 10 | +import java.io.IOException; |
10 | 11 |
|
11 |
| -/** |
12 |
| - * Adapts HessianSerializer so it can be used in Redis. User: msellers Date: |
13 |
| - * 9/3/14 Time: 10:17 AM |
14 |
| - */ |
15 | 12 | public class HessianSerializer implements Serializer {
|
16 | 13 |
|
17 |
| - private static final Logger m_logger = LoggerFactory.getLogger(HessianSerializer.class); |
| 14 | + private static final Logger LOGGER = LoggerFactory.getLogger(HessianSerializer.class); |
18 | 15 |
|
19 | 16 | @Override
|
20 | 17 | public byte[] serialize(Object obj) {
|
21 |
| - try { |
| 18 | + Hessian2Output out = null; |
| 19 | + |
| 20 | + try ( |
22 | 21 | ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
23 |
| - Hessian2Output out = new Hessian2Output(bos); |
| 22 | + ) { |
| 23 | + out = new Hessian2Output(bos); |
| 24 | + |
24 | 25 | out.startMessage();
|
25 | 26 | out.writeObject(obj);
|
26 | 27 | out.completeMessage();
|
27 | 28 | out.close();
|
28 |
| - byte[] array = bos.toByteArray(); |
29 | 29 |
|
30 |
| - return array; |
| 30 | + return bos.toByteArray(); |
31 | 31 | } 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); |
33 | 39 | throw new RuntimeException(e.getMessage());
|
34 | 40 | }
|
35 | 41 | }
|
36 | 42 |
|
37 | 43 | @Override
|
38 | 44 | 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 | + } |
46 | 48 |
|
47 |
| - toReturn = in.readObject(); |
48 |
| - } |
| 49 | + try ( |
| 50 | + ByteArrayInputStream bin = new ByteArrayInputStream(bytes); |
| 51 | + ) { |
| 52 | + Hessian2Input in = new Hessian2Input(bin); |
| 53 | + in.startMessage(); |
49 | 54 |
|
50 |
| - return toReturn; |
| 55 | + return in.readObject(); |
51 | 56 | } 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); |
53 | 58 | throw new RuntimeException(e.getMessage());
|
54 | 59 | }
|
55 | 60 | }
|
|
0 commit comments