Parameters encoding fixed

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

@ -15,12 +15,11 @@
*/
package org.redisson.client.handler;
import java.util.Arrays;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
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>> {
@ -34,9 +33,15 @@ public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>
out.writeBytes(toChars(1 + msg.getParams().length));
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()) {
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);

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

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

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

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

Loading…
Cancel
Save