Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

close ZInputStream and ZOutputStream to avoid memory leak

  • Loading branch information...
commit 6f87c3eebeeedabb0b5deb41074684d38a05df29 1 parent 2b88b6b
Richard Huang flyerhzm authored
24 src/main/java/com/openfeint/memcached/transcoder/MarshalZlibTranscoder.java
View
@@ -32,6 +32,7 @@ public MarshalZlibTranscoder(Ruby ruby, int flags) {
public CachedData encode(Object o) {
if (o instanceof IRubyObject) {
+ ZOutputStream zout = null;
try {
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
MarshalStream marshal = new MarshalStream(ruby, out1, Integer.MAX_VALUE);
@@ -40,9 +41,10 @@ public CachedData encode(Object o) {
byte[] bytes;
if (getFlags() == COMPRESS_FLAG) {
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
- ZOutputStream zout = new ZOutputStream(out2, JZlib.Z_DEFAULT_COMPRESSION);
+ zout = new ZOutputStream(out2, JZlib.Z_DEFAULT_COMPRESSION);
zout.write(out1.toByteArray());
- zout.close();
+ zout.flush();
+ zout.end();
bytes = out2.toByteArray();
} else {
bytes = out1.toByteArray();
@@ -51,6 +53,13 @@ public CachedData encode(Object o) {
return new CachedData(super.getFlags(), bytes, bytes.length);
} catch (IOException e) {
throw ruby.newIOErrorFromException(e);
+ } finally {
+ if (zout != null) {
+ try {
+ zout.close();
+ } catch (IOException e) {}
+ zout = null;
+ }
}
} else {
return super.encodeNumber(o);
@@ -58,11 +67,12 @@ public CachedData encode(Object o) {
}
public Object decode(CachedData d) {
+ ZInputStream zin = null;
try {
byte[] bytes;
if (d.getFlags() == COMPRESS_FLAG) {
ByteArrayInputStream in = new ByteArrayInputStream(d.getData());
- ZInputStream zin = new ZInputStream(in);
+ zin = new ZInputStream(in);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
@@ -72,7 +82,6 @@ public Object decode(CachedData d) {
}
buffer.flush();
bytes = buffer.toByteArray();
- zin.close();
} else {
bytes = d.getData();
}
@@ -82,6 +91,13 @@ public Object decode(CachedData d) {
return super.decodeNumber(d, e);
} catch (IOException e) {
return super.decodeNumber(d, e);
+ } finally {
+ if (zin != null) {
+ try {
+ zin.close();
+ } catch (IOException e) {}
+ zin = null;
+ }
}
}
}
BIN  target/spymemcached-ext-0.0.1.jar
View
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.