Parameters encoding fixed

pull/243/head
Nikita 10 years ago
parent 93eb8fad27
commit b7911a8fb3

@ -15,12 +15,11 @@
*/ */
package org.redisson.client.handler; package org.redisson.client.handler;
import java.util.Arrays;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Promise;
public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>> { public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>> {
@ -34,9 +33,15 @@ public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>
out.writeBytes(toChars(1 + msg.getParams().length)); out.writeBytes(toChars(1 + msg.getParams().length));
out.writeBytes(CRLF); out.writeBytes(CRLF);
writeArgument(out, msg.getCommand().getName().getBytes("UTF-8")); if (Arrays.binarySearch(msg.getCommand().getEncodeParamIndexes(), 0) != -1) {
writeArgument(out, msg.getCodec().encode(msg.getCommand().getName()));
}
int i = 1;
for (Object param : msg.getParams()) { for (Object param : msg.getParams()) {
writeArgument(out, param.toString().getBytes("UTF-8")); if (Arrays.binarySearch(msg.getCommand().getEncodeParamIndexes(), i) != -1) {
writeArgument(out, msg.getCodec().encode(param));
}
i++;
} }
// String o = out.toString(CharsetUtil.UTF_8); // String o = out.toString(CharsetUtil.UTF_8);

@ -2,6 +2,6 @@ package org.redisson.client.protocol;
public interface Encoder { public interface Encoder {
Object encode(Object in); byte[] encode(Object in);
} }

@ -18,16 +18,18 @@ package org.redisson.client.protocol;
public class RedisCommand<R> { public class RedisCommand<R> {
private final String name; private final String name;
private final int[] encodeParamIndexes;
private Decoder<R> reponseDecoder; private Decoder<R> reponseDecoder;
public RedisCommand(String name) { public RedisCommand(String name, int ... encodeParamIndexes) {
this(name, null); this(name, null, encodeParamIndexes);
} }
public RedisCommand(String name, Decoder<R> reponseDecoder) { public RedisCommand(String name, Decoder<R> reponseDecoder, int ... encodeParamIndexes) {
super(); super();
this.name = name; this.name = name;
this.reponseDecoder = reponseDecoder; this.reponseDecoder = reponseDecoder;
this.encodeParamIndexes = encodeParamIndexes;
} }
public String getName() { public String getName() {
@ -38,5 +40,8 @@ public class RedisCommand<R> {
return reponseDecoder; return reponseDecoder;
} }
public int[] getEncodeParamIndexes() {
return encodeParamIndexes;
}
} }

@ -17,7 +17,7 @@ package org.redisson.client.protocol;
public interface RedisCommands { public interface RedisCommands {
RedisCommand<Object> GET = new RedisCommand<Object>("GET"); RedisCommand<Object> GET = new RedisCommand<Object>("GET", 0);
RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder()); RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder(), 0, 1);
} }

@ -1,13 +1,19 @@
package org.redisson.client.protocol; package org.redisson.client.protocol;
import java.io.UnsupportedEncodingException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
public class StringCodec implements Codec { public class StringCodec implements Codec {
@Override @Override
public Object encode(Object in) { public byte[] encode(Object in) {
return in.toString(); try {
return in.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
} }
@Override @Override

Loading…
Cancel
Save