Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

do not define different flags to compatible with memcached.gem

  • Loading branch information...
commit 4b93673c527c3ec3360a799852d085e481e6c607 1 parent 5ab76f1
@flyerhzm flyerhzm authored
View
46 src/main/java/com/openfeint/memcached/transcoder/MarshalTranscoder.java
@@ -3,6 +3,7 @@
import net.spy.memcached.CachedData;
import net.spy.memcached.transcoders.Transcoder;
import org.jruby.Ruby;
+import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
@@ -17,9 +18,8 @@
*
*/
public class MarshalTranscoder implements Transcoder {
- private Ruby ruby;
+ protected Ruby ruby;
private int flags;
- static final int SPECIAL_LONG = (3 << 8);
public MarshalTranscoder(Ruby ruby) {
this(ruby, 0);
@@ -42,24 +42,21 @@ public CachedData encode(Object o) {
marshal.dumpObject((IRubyObject) o);
byte[] bytes = baos.toByteArray();
return new CachedData(getFlags(), bytes, bytes.length);
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ } catch (IOException e) {
+ throw ruby.newIOErrorFromException(e);
}
} else {
- byte[] bytes = o.toString().getBytes();
- return new CachedData(SPECIAL_LONG, bytes, bytes.length);
+ return encodeNumber(o);
}
}
public Object decode(CachedData d) {
- if (d.getFlags() == SPECIAL_LONG) {
- return Long.parseLong(new String(d.getData()).trim());
- } else {
- try {
- return new UnmarshalStream(ruby, new ByteArrayInputStream(d.getData()), null, false, false).unmarshalObject();
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
- }
+ try {
+ return new UnmarshalStream(ruby, new ByteArrayInputStream(d.getData()), null, false, false).unmarshalObject();
+ } catch (RaiseException e) {
+ return decodeNumber(d, e);
+ } catch (IOException e) {
+ return decodeNumber(d, e);
}
}
@@ -70,4 +67,25 @@ public int getMaxSize() {
public int getFlags() {
return flags;
}
+
+ protected CachedData encodeNumber(Object o) {
+ byte[] bytes = o.toString().getBytes();
+ return new CachedData(getFlags(), bytes, bytes.length);
+ }
+
+ protected Long decodeNumber(CachedData d, RaiseException originalException) {
+ try {
+ return Long.valueOf(new String(d.getData()).trim());
+ } catch (NumberFormatException e) {
+ throw ruby.newRuntimeError(originalException.getLocalizedMessage());
+ }
+ }
+
+ protected Long decodeNumber(CachedData d, IOException originalException) {
+ try {
+ return Long.valueOf(new String(d.getData()).trim());
+ } catch (NumberFormatException e) {
+ throw ruby.newIOErrorFromException(originalException);
+ }
+ }
}
View
78 src/main/java/com/openfeint/memcached/transcoder/MarshalZlibTranscoder.java
@@ -6,6 +6,7 @@
import net.spy.memcached.CachedData;
import net.spy.memcached.transcoders.Transcoder;
import org.jruby.Ruby;
+import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
@@ -22,21 +23,15 @@
* MarshalZlibTranscoder do marshaling/unmarshaling and compressing/decompressing with zlib.
*
*/
-public class MarshalZlibTranscoder implements Transcoder {
- private Ruby ruby;
- private int flags;
+public class MarshalZlibTranscoder extends MarshalTranscoder {
+ static final int COMPRESS_FLAG = 1;
public MarshalZlibTranscoder(Ruby ruby) {
- this(ruby, 1);
+ super(ruby, COMPRESS_FLAG);
}
public MarshalZlibTranscoder(Ruby ruby, int flags) {
- this.ruby = ruby;
- this.flags = flags;
- }
-
- public boolean asyncDecode(CachedData d) {
- return false;
+ super(ruby, flags);
}
public CachedData encode(Object o) {
@@ -47,7 +42,7 @@ public CachedData encode(Object o) {
marshal.dumpObject((IRubyObject) o);
byte[] bytes;
- if (flags == 1) {
+ if (getFlags() == COMPRESS_FLAG) {
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
ZOutputStream zout = new ZOutputStream(out2, JZlib.Z_DEFAULT_COMPRESSION);
zout.write(out1.toByteArray());
@@ -57,51 +52,40 @@ public CachedData encode(Object o) {
bytes = out1.toByteArray();
}
- return new CachedData(getFlags(), bytes, bytes.length);
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ return new CachedData(super.getFlags(), bytes, bytes.length);
+ } catch (IOException e) {
+ throw ruby.newIOErrorFromException(e);
}
} else {
- byte[] bytes = o.toString().getBytes();
- return new CachedData(MarshalTranscoder.SPECIAL_LONG, bytes, bytes.length);
+ return super.encodeNumber(o);
}
}
public Object decode(CachedData d) {
- if (d.getFlags() == MarshalTranscoder.SPECIAL_LONG) {
- return Long.parseLong(new String(d.getData()));
- } else {
- try {
- byte[] bytes;
- if (d.getFlags() == 1) {
- ByteArrayInputStream in = new ByteArrayInputStream(d.getData());
- ZInputStream zin = new ZInputStream(in);
+ try {
+ byte[] bytes;
+ if (d.getFlags() == COMPRESS_FLAG) {
+ ByteArrayInputStream in = new ByteArrayInputStream(d.getData());
+ ZInputStream zin = new ZInputStream(in);
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- int nRead;
- byte[] data = new byte[1024];
- while ((nRead = zin.read(data, 0, data.length)) != -1) {
- buffer.write(data, 0, nRead);
- }
- buffer.flush();
- bytes = buffer.toByteArray();
- zin.close();
- } else {
- bytes = d.getData();
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ int nRead;
+ byte[] data = new byte[1024];
+ while ((nRead = zin.read(data, 0, data.length)) != -1) {
+ buffer.write(data, 0, nRead);
}
-
- return new UnmarshalStream(ruby, new ByteArrayInputStream(bytes), null, false, false).unmarshalObject();
- } catch (IOException ioe) {
- throw ruby.newIOErrorFromException(ioe);
+ buffer.flush();
+ bytes = buffer.toByteArray();
+ zin.close();
+ } else {
+ bytes = d.getData();
}
- }
- }
- public int getMaxSize() {
- return CachedData.MAX_SIZE;
- }
-
- public int getFlags() {
- return flags;
+ return new UnmarshalStream(ruby, new ByteArrayInputStream(bytes), null, false, false).unmarshalObject();
+ } catch (RaiseException e) {
+ return super.decodeNumber(d, e);
+ } catch (IOException e) {
+ return super.decodeNumber(d, e);
+ }
}
}
View
BIN  target/spymemcached-ext-0.0.1.jar
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.