Skip to content

Commit 5ca2ed1

Browse files
Mike WiederholdMichael Wiederhold
authored andcommitted
SPY-49: BaseSerializingTranscoder does not close resources.
Change-Id: Ifa6b356a9faaad39f12f2cf28d34fbc837faa7b7 Reviewed-on: http://review.couchbase.org/9454 Reviewed-by: Michael Wiederhold <[email protected]> Tested-by: Michael Wiederhold <[email protected]>
1 parent 6a383c5 commit 5ca2ed1

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/main/java/net/spy/memcached/transcoders/BaseSerializingTranscoder.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,20 @@ protected byte[] serialize(Object o) {
7575
throw new NullPointerException("Can't serialize null");
7676
}
7777
byte[] rv=null;
78+
ByteArrayOutputStream bos = null;
79+
ObjectOutputStream os = null;
7880
try {
79-
ByteArrayOutputStream bos=new ByteArrayOutputStream();
80-
ObjectOutputStream os=new ObjectOutputStream(bos);
81+
bos=new ByteArrayOutputStream();
82+
os=new ObjectOutputStream(bos);
8183
os.writeObject(o);
8284
os.close();
8385
bos.close();
8486
rv=bos.toByteArray();
8587
} catch(IOException e) {
8688
throw new IllegalArgumentException("Non-serializable object", e);
89+
} finally {
90+
CloseUtil.close(os);
91+
CloseUtil.close(bos);
8792
}
8893
return rv;
8994
}
@@ -93,10 +98,12 @@ protected byte[] serialize(Object o) {
9398
*/
9499
protected Object deserialize(byte[] in) {
95100
Object rv=null;
101+
ByteArrayInputStream bis = null;
102+
ObjectInputStream is = null;
96103
try {
97104
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);
100107
rv=is.readObject();
101108
is.close();
102109
bis.close();
@@ -107,6 +114,9 @@ protected Object deserialize(byte[] in) {
107114
} catch (ClassNotFoundException e) {
108115
getLogger().warn("Caught CNFE decoding %d bytes of data",
109116
in == null ? 0 : in.length, e);
117+
} finally {
118+
CloseUtil.close(is);
119+
CloseUtil.close(bis);
110120
}
111121
return rv;
112122
}
@@ -144,7 +154,7 @@ protected byte[] decompress(byte[] in) {
144154
if(in != null) {
145155
ByteArrayInputStream bis=new ByteArrayInputStream(in);
146156
bos=new ByteArrayOutputStream();
147-
GZIPInputStream gis;
157+
GZIPInputStream gis = null;
148158
try {
149159
gis = new GZIPInputStream(bis);
150160

@@ -156,6 +166,10 @@ protected byte[] decompress(byte[] in) {
156166
} catch (IOException e) {
157167
getLogger().warn("Failed to decompress data", e);
158168
bos = null;
169+
} finally {
170+
CloseUtil.close(gis);
171+
CloseUtil.close(bis);
172+
CloseUtil.close(bos);
159173
}
160174
}
161175
return bos == null ? null : bos.toByteArray();

0 commit comments

Comments
 (0)