@@ -75,15 +75,20 @@ protected byte[] serialize(Object o) {
75
75
throw new NullPointerException ("Can't serialize null" );
76
76
}
77
77
byte [] rv =null ;
78
+ ByteArrayOutputStream bos = null ;
79
+ ObjectOutputStream os = null ;
78
80
try {
79
- ByteArrayOutputStream bos =new ByteArrayOutputStream ();
80
- ObjectOutputStream os =new ObjectOutputStream (bos );
81
+ bos =new ByteArrayOutputStream ();
82
+ os =new ObjectOutputStream (bos );
81
83
os .writeObject (o );
82
84
os .close ();
83
85
bos .close ();
84
86
rv =bos .toByteArray ();
85
87
} catch (IOException e ) {
86
88
throw new IllegalArgumentException ("Non-serializable object" , e );
89
+ } finally {
90
+ CloseUtil .close (os );
91
+ CloseUtil .close (bos );
87
92
}
88
93
return rv ;
89
94
}
@@ -93,10 +98,12 @@ protected byte[] serialize(Object o) {
93
98
*/
94
99
protected Object deserialize (byte [] in ) {
95
100
Object rv =null ;
101
+ ByteArrayInputStream bis = null ;
102
+ ObjectInputStream is = null ;
96
103
try {
97
104
if (in != null ) {
98
- ByteArrayInputStream bis =new ByteArrayInputStream (in );
99
- ObjectInputStream is =new ObjectInputStream (bis );
105
+ bis =new ByteArrayInputStream (in );
106
+ is =new ObjectInputStream (bis );
100
107
rv =is .readObject ();
101
108
is .close ();
102
109
bis .close ();
@@ -107,6 +114,9 @@ protected Object deserialize(byte[] in) {
107
114
} catch (ClassNotFoundException e ) {
108
115
getLogger ().warn ("Caught CNFE decoding %d bytes of data" ,
109
116
in == null ? 0 : in .length , e );
117
+ } finally {
118
+ CloseUtil .close (is );
119
+ CloseUtil .close (bis );
110
120
}
111
121
return rv ;
112
122
}
@@ -144,7 +154,7 @@ protected byte[] decompress(byte[] in) {
144
154
if (in != null ) {
145
155
ByteArrayInputStream bis =new ByteArrayInputStream (in );
146
156
bos =new ByteArrayOutputStream ();
147
- GZIPInputStream gis ;
157
+ GZIPInputStream gis = null ;
148
158
try {
149
159
gis = new GZIPInputStream (bis );
150
160
@@ -156,6 +166,10 @@ protected byte[] decompress(byte[] in) {
156
166
} catch (IOException e ) {
157
167
getLogger ().warn ("Failed to decompress data" , e );
158
168
bos = null ;
169
+ } finally {
170
+ CloseUtil .close (gis );
171
+ CloseUtil .close (bis );
172
+ CloseUtil .close (bos );
159
173
}
160
174
}
161
175
return bos == null ? null : bos .toByteArray ();
0 commit comments