diff --git a/pom.xml b/pom.xml index 77170b0..211d867 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + org.sonatype.oss @@ -72,8 +73,8 @@ maven-compiler-plugin 2.0.2 - 1.6 - 1.6 + 1.5 + 1.5 diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 4893cc6..86363ac 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -14,6 +14,7 @@ import java.util.Map; import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.Protocol.Keyword; +import redis.clients.util.SafeEncoder; public class BinaryClient extends Connection { public enum LIST_POSITION { @@ -21,7 +22,7 @@ public class BinaryClient extends Connection { public final byte[] raw; private LIST_POSITION() { - raw = name().getBytes(Protocol.UTF8); + raw = SafeEncoder.encode(name()); } } diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 8f57a3d..d4a1120 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -12,6 +12,7 @@ import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.util.JedisByteHashMap; +import redis.clients.util.SafeEncoder; public class BinaryJedis implements BinaryJedisCommands { protected Client client = null; @@ -2317,8 +2318,8 @@ public class BinaryJedis implements BinaryJedisCommands { Set set = new LinkedHashSet(); Iterator iterator = membersWithScores.iterator(); while (iterator.hasNext()) { - set.add(new Tuple(iterator.next(), Double.valueOf(new String( - iterator.next(), Protocol.UTF8)))); + set.add(new Tuple(iterator.next(), Double.valueOf(SafeEncoder + .encode(iterator.next())))); } return set; } diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 9e66593..ccb8e98 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -4,6 +4,8 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import redis.clients.util.SafeEncoder; + public class Client extends BinaryClient { public Client(final String host) { super(host); @@ -14,82 +16,81 @@ public class Client extends BinaryClient { } public void set(final String key, final String value) { - set(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); + set(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void get(final String key) { - get(key.getBytes(Protocol.UTF8)); + get(SafeEncoder.encode(key)); } public void exists(final String key) { - exists(key.getBytes(Protocol.UTF8)); + exists(SafeEncoder.encode(key)); } public void del(final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < keys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } del(bkeys); } public void type(final String key) { - type(key.getBytes(Protocol.UTF8)); + type(SafeEncoder.encode(key)); } public void keys(final String pattern) { - keys(pattern.getBytes(Protocol.UTF8)); + keys(SafeEncoder.encode(pattern)); } public void rename(final String oldkey, final String newkey) { - rename(oldkey.getBytes(Protocol.UTF8), newkey.getBytes(Protocol.UTF8)); + rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); } public void renamenx(final String oldkey, final String newkey) { - renamenx(oldkey.getBytes(Protocol.UTF8), newkey.getBytes(Protocol.UTF8)); + renamenx(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); } public void expire(final String key, final int seconds) { - expire(key.getBytes(Protocol.UTF8), seconds); + expire(SafeEncoder.encode(key), seconds); } public void expireAt(final String key, final long unixTime) { - expireAt(key.getBytes(Protocol.UTF8), unixTime); + expireAt(SafeEncoder.encode(key), unixTime); } public void ttl(final String key) { - ttl(key.getBytes(Protocol.UTF8)); + ttl(SafeEncoder.encode(key)); } public void move(final String key, final int dbIndex) { - move(key.getBytes(Protocol.UTF8), dbIndex); + move(SafeEncoder.encode(key), dbIndex); } public void getSet(final String key, final String value) { - getSet(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); + getSet(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void mget(final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } mget(bkeys); } public void setnx(final String key, final String value) { - setnx(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); + setnx(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void setex(final String key, final int seconds, final String value) { - setex(key.getBytes(Protocol.UTF8), seconds, value - .getBytes(Protocol.UTF8)); + setex(SafeEncoder.encode(key), seconds, SafeEncoder.encode(value)); } public void mset(final String... keysvalues) { final byte[][] bkeysvalues = new byte[keysvalues.length][]; for (int i = 0; i < keysvalues.length; i++) { - bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8); + bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); } mset(bkeysvalues); } @@ -97,175 +98,173 @@ public class Client extends BinaryClient { public void msetnx(final String... keysvalues) { final byte[][] bkeysvalues = new byte[keysvalues.length][]; for (int i = 0; i < keysvalues.length; i++) { - bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8); + bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); } msetnx(bkeysvalues); } public void decrBy(final String key, final int integer) { - decrBy(key.getBytes(Protocol.UTF8), integer); + decrBy(SafeEncoder.encode(key), integer); } public void decr(final String key) { - decr(key.getBytes(Protocol.UTF8)); + decr(SafeEncoder.encode(key)); } public void incrBy(final String key, final int integer) { - incrBy(key.getBytes(Protocol.UTF8), integer); + incrBy(SafeEncoder.encode(key), integer); } public void incr(final String key) { - incr(key.getBytes(Protocol.UTF8)); + incr(SafeEncoder.encode(key)); } public void append(final String key, final String value) { - append(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); + append(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void substr(final String key, final int start, final int end) { - substr(key.getBytes(Protocol.UTF8), start, end); + substr(SafeEncoder.encode(key), start, end); } public void hset(final String key, final String field, final String value) { - hset(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), value - .getBytes(Protocol.UTF8)); + hset(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder + .encode(value)); } public void hget(final String key, final String field) { - hget(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); + hget(SafeEncoder.encode(key), SafeEncoder.encode(field)); } public void hsetnx(final String key, final String field, final String value) { - hsetnx(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), - value.getBytes(Protocol.UTF8)); + hsetnx(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder + .encode(value)); } public void hmset(final String key, final Map hash) { final Map bhash = new HashMap(hash .size()); for (final Entry entry : hash.entrySet()) { - bhash.put(entry.getKey().getBytes(Protocol.UTF8), entry.getValue() - .getBytes(Protocol.UTF8)); + bhash.put(SafeEncoder.encode(entry.getKey()), SafeEncoder + .encode(entry.getValue())); } - hmset(key.getBytes(Protocol.UTF8), bhash); + hmset(SafeEncoder.encode(key), bhash); } public void hmget(final String key, final String... fields) { final byte[][] bfields = new byte[fields.length][]; for (int i = 0; i < bfields.length; i++) { - bfields[i] = fields[i].getBytes(Protocol.UTF8); + bfields[i] = SafeEncoder.encode(fields[i]); } - hmget(key.getBytes(Protocol.UTF8), bfields); + hmget(SafeEncoder.encode(key), bfields); } public void hincrBy(final String key, final String field, final int value) { - hincrBy(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8), - value); + hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value); } public void hexists(final String key, final String field) { - hexists(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); + hexists(SafeEncoder.encode(key), SafeEncoder.encode(field)); } public void hdel(final String key, final String field) { - hdel(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); + hdel(SafeEncoder.encode(key), SafeEncoder.encode(field)); } public void hlen(final String key) { - hlen(key.getBytes(Protocol.UTF8)); + hlen(SafeEncoder.encode(key)); } public void hkeys(final String key) { - hkeys(key.getBytes(Protocol.UTF8)); + hkeys(SafeEncoder.encode(key)); } public void hvals(final String key) { - hvals(key.getBytes(Protocol.UTF8)); + hvals(SafeEncoder.encode(key)); } public void hgetAll(final String key) { - hgetAll(key.getBytes(Protocol.UTF8)); + hgetAll(SafeEncoder.encode(key)); } public void rpush(final String key, final String string) { - rpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); + rpush(SafeEncoder.encode(key), SafeEncoder.encode(string)); } public void lpush(final String key, final String string) { - lpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); + lpush(SafeEncoder.encode(key), SafeEncoder.encode(string)); } public void llen(final String key) { - llen(key.getBytes(Protocol.UTF8)); + llen(SafeEncoder.encode(key)); } public void lrange(final String key, final int start, final int end) { - lrange(key.getBytes(Protocol.UTF8), start, end); + lrange(SafeEncoder.encode(key), start, end); } public void ltrim(final String key, final int start, final int end) { - ltrim(key.getBytes(Protocol.UTF8), start, end); + ltrim(SafeEncoder.encode(key), start, end); } public void lindex(final String key, final int index) { - lindex(key.getBytes(Protocol.UTF8), index); + lindex(SafeEncoder.encode(key), index); } public void lset(final String key, final int index, final String value) { - lset(key.getBytes(Protocol.UTF8), index, value.getBytes(Protocol.UTF8)); + lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value)); } public void lrem(final String key, int count, final String value) { - lrem(key.getBytes(Protocol.UTF8), count, value.getBytes(Protocol.UTF8)); + lrem(SafeEncoder.encode(key), count, SafeEncoder.encode(value)); } public void lpop(final String key) { - lpop(key.getBytes(Protocol.UTF8)); + lpop(SafeEncoder.encode(key)); } public void rpop(final String key) { - rpop(key.getBytes(Protocol.UTF8)); + rpop(SafeEncoder.encode(key)); } public void rpoplpush(final String srckey, final String dstkey) { - rpoplpush(srckey.getBytes(Protocol.UTF8), dstkey - .getBytes(Protocol.UTF8)); + rpoplpush(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey)); } public void sadd(final String key, final String member) { - sadd(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + sadd(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void smembers(final String key) { - smembers(key.getBytes(Protocol.UTF8)); + smembers(SafeEncoder.encode(key)); } public void srem(final String key, final String member) { - srem(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + srem(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void spop(final String key) { - spop(key.getBytes(Protocol.UTF8)); + spop(SafeEncoder.encode(key)); } public void smove(final String srckey, final String dstkey, final String member) { - smove(srckey.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8), - member.getBytes(Protocol.UTF8)); + smove(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey), + SafeEncoder.encode(member)); } public void scard(final String key) { - scard(key.getBytes(Protocol.UTF8)); + scard(SafeEncoder.encode(key)); } public void sismember(final String key, final String member) { - sismember(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + sismember(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void sinter(final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } sinter(bkeys); } @@ -273,15 +272,15 @@ public class Client extends BinaryClient { public void sinterstore(final String dstkey, final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } - sinterstore(dstkey.getBytes(Protocol.UTF8), bkeys); + sinterstore(SafeEncoder.encode(dstkey), bkeys); } public void sunion(final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } sunion(bkeys); } @@ -289,15 +288,15 @@ public class Client extends BinaryClient { public void sunionstore(final String dstkey, final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } - sunionstore(dstkey.getBytes(Protocol.UTF8), bkeys); + sunionstore(SafeEncoder.encode(dstkey), bkeys); } public void sdiff(final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } sdiff(bkeys); } @@ -305,198 +304,197 @@ public class Client extends BinaryClient { public void sdiffstore(final String dstkey, final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = keys[i].getBytes(Protocol.UTF8); + bkeys[i] = SafeEncoder.encode(keys[i]); } - sdiffstore(dstkey.getBytes(Protocol.UTF8), bkeys); + sdiffstore(SafeEncoder.encode(dstkey), bkeys); } public void srandmember(final String key) { - srandmember(key.getBytes(Protocol.UTF8)); + srandmember(SafeEncoder.encode(key)); } public void zadd(final String key, final double score, final String member) { - zadd(key.getBytes(Protocol.UTF8), score, member.getBytes(Protocol.UTF8)); + zadd(SafeEncoder.encode(key), score, SafeEncoder.encode(member)); } public void zrange(final String key, final int start, final int end) { - zrange(key.getBytes(Protocol.UTF8), start, end); + zrange(SafeEncoder.encode(key), start, end); } public void zrem(final String key, final String member) { - zrem(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + zrem(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void zincrby(final String key, final double score, final String member) { - zincrby(key.getBytes(Protocol.UTF8), score, member - .getBytes(Protocol.UTF8)); + zincrby(SafeEncoder.encode(key), score, SafeEncoder.encode(member)); } public void zrank(final String key, final String member) { - zrank(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + zrank(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void zrevrank(final String key, final String member) { - zrevrank(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + zrevrank(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void zrevrange(final String key, final int start, final int end) { - zrevrange(key.getBytes(Protocol.UTF8), start, end); + zrevrange(SafeEncoder.encode(key), start, end); } public void zrangeWithScores(final String key, final int start, final int end) { - zrangeWithScores(key.getBytes(Protocol.UTF8), start, end); + zrangeWithScores(SafeEncoder.encode(key), start, end); } public void zrevrangeWithScores(final String key, final int start, final int end) { - zrevrangeWithScores(key.getBytes(Protocol.UTF8), start, end); + zrevrangeWithScores(SafeEncoder.encode(key), start, end); } public void zcard(final String key) { - zcard(key.getBytes(Protocol.UTF8)); + zcard(SafeEncoder.encode(key)); } public void zscore(final String key, final String member) { - zscore(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); + zscore(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void watch(final String key) { - watch(key.getBytes(Protocol.UTF8)); + watch(SafeEncoder.encode(key)); } public void sort(final String key) { - sort(key.getBytes(Protocol.UTF8)); + sort(SafeEncoder.encode(key)); } public void sort(final String key, final SortingParams sortingParameters) { - sort(key.getBytes(Protocol.UTF8), sortingParameters); + sort(SafeEncoder.encode(key), sortingParameters); } public void blpop(final String[] args) { final byte[][] bargs = new byte[args.length][]; for (int i = 0; i < bargs.length; i++) { - bargs[i] = args[i].getBytes(Protocol.UTF8); + bargs[i] = SafeEncoder.encode(args[i]); } blpop(bargs); } public void sort(final String key, final SortingParams sortingParameters, final String dstkey) { - sort(key.getBytes(Protocol.UTF8), sortingParameters, dstkey - .getBytes(Protocol.UTF8)); + sort(SafeEncoder.encode(key), sortingParameters, SafeEncoder + .encode(dstkey)); } public void sort(final String key, final String dstkey) { - sort(key.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8)); + sort(SafeEncoder.encode(key), SafeEncoder.encode(dstkey)); } public void brpop(final String[] args) { final byte[][] bargs = new byte[args.length][]; for (int i = 0; i < bargs.length; i++) { - bargs[i] = args[i].getBytes(Protocol.UTF8); + bargs[i] = SafeEncoder.encode(args[i]); } brpop(bargs); } public void zcount(final String key, final double min, final double max) { - zcount(key.getBytes(Protocol.UTF8), min, max); + zcount(SafeEncoder.encode(key), min, max); } public void zrangeByScore(final String key, final double min, final double max) { - zrangeByScore(key.getBytes(Protocol.UTF8), min, max); + zrangeByScore(SafeEncoder.encode(key), min, max); } public void zrangeByScore(final String key, final String min, final String max) { - zrangeByScore(key.getBytes(Protocol.UTF8), min.getBytes(Protocol.UTF8), - max.getBytes(Protocol.UTF8)); + zrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min), + SafeEncoder.encode(max)); } public void zrangeByScore(final String key, final double min, final double max, final int offset, int count) { - zrangeByScore(key.getBytes(Protocol.UTF8), min, max, offset, count); + zrangeByScore(SafeEncoder.encode(key), min, max, offset, count); } public void zrangeByScoreWithScores(final String key, final double min, final double max) { - zrangeByScoreWithScores(key.getBytes(Protocol.UTF8), min, max); + zrangeByScoreWithScores(SafeEncoder.encode(key), min, max); } public void zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) { - zrangeByScoreWithScores(key.getBytes(Protocol.UTF8), min, max, offset, + zrangeByScoreWithScores(SafeEncoder.encode(key), min, max, offset, count); } public void zremrangeByRank(final String key, final int start, final int end) { - zremrangeByRank(key.getBytes(Protocol.UTF8), start, end); + zremrangeByRank(SafeEncoder.encode(key), start, end); } public void zremrangeByScore(final String key, final double start, final double end) { - zremrangeByScore(key.getBytes(Protocol.UTF8), start, end); + zremrangeByScore(SafeEncoder.encode(key), start, end); } public void zunionstore(final String dstkey, final String... sets) { final byte[][] bsets = new byte[sets.length][]; for (int i = 0; i < bsets.length; i++) { - bsets[i] = sets[i].getBytes(Protocol.UTF8); + bsets[i] = SafeEncoder.encode(sets[i]); } - zunionstore(dstkey.getBytes(Protocol.UTF8), bsets); + zunionstore(SafeEncoder.encode(dstkey), bsets); } public void zunionstore(final String dstkey, final ZParams params, final String... sets) { final byte[][] bsets = new byte[sets.length][]; for (int i = 0; i < bsets.length; i++) { - bsets[i] = sets[i].getBytes(Protocol.UTF8); + bsets[i] = SafeEncoder.encode(sets[i]); } - zunionstore(dstkey.getBytes(Protocol.UTF8), params, bsets); + zunionstore(SafeEncoder.encode(dstkey), params, bsets); } public void zinterstore(final String dstkey, final String... sets) { final byte[][] bsets = new byte[sets.length][]; for (int i = 0; i < bsets.length; i++) { - bsets[i] = sets[i].getBytes(Protocol.UTF8); + bsets[i] = SafeEncoder.encode(sets[i]); } - zinterstore(dstkey.getBytes(Protocol.UTF8), bsets); + zinterstore(SafeEncoder.encode(dstkey), bsets); } public void zinterstore(final String dstkey, final ZParams params, final String... sets) { final byte[][] bsets = new byte[sets.length][]; for (int i = 0; i < bsets.length; i++) { - bsets[i] = sets[i].getBytes(Protocol.UTF8); + bsets[i] = SafeEncoder.encode(sets[i]); } - zinterstore(dstkey.getBytes(Protocol.UTF8), params, bsets); + zinterstore(SafeEncoder.encode(dstkey), params, bsets); } public void strlen(final String key) { - strlen(key.getBytes(Protocol.UTF8)); + strlen(SafeEncoder.encode(key)); } public void lpushx(final String key, final String string) { - lpushx(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); + lpushx(SafeEncoder.encode(key), SafeEncoder.encode(string)); } public void persist(final String key) { - persist(key.getBytes(Protocol.UTF8)); + persist(SafeEncoder.encode(key)); } public void rpushx(final String key, final String string) { - rpushx(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); + rpushx(SafeEncoder.encode(key), SafeEncoder.encode(string)); } public void echo(final String string) { - echo(string.getBytes(Protocol.UTF8)); + echo(SafeEncoder.encode(string)); } public void linsert(final String key, final LIST_POSITION where, final String pivot, final String value) { - linsert(key.getBytes(Protocol.UTF8), where, pivot - .getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); + linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot), + SafeEncoder.encode(value)); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 486ddcf..f3728af 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -10,6 +10,7 @@ import java.util.List; import redis.clients.jedis.Protocol.Command; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; +import redis.clients.util.SafeEncoder; public class Connection { private String host; @@ -53,7 +54,7 @@ public class Connection { protected Connection sendCommand(final Command cmd, final String... args) { final byte[][] bargs = new byte[args.length][]; for (int i = 0; i < args.length; i++) { - bargs[i] = args[i].getBytes(Protocol.UTF8); + bargs[i] = SafeEncoder.encode(args[i]); } return sendCommand(cmd, bargs); } @@ -144,14 +145,14 @@ public class Connection { if (null == resp) { return null; } else { - return new String(resp, Protocol.UTF8); + return SafeEncoder.encode(resp); } } public String getBulkReply() { final byte[] result = getBinaryBulkReply(); if (null != result) { - return new String(result, Protocol.UTF8); + return SafeEncoder.encode(result); } else { return null; } @@ -177,7 +178,7 @@ public class Connection { if (barray == null) { result.add(null); } else { - result.add(new String(barray, Protocol.UTF8)); + result.add(SafeEncoder.encode(barray)); } } return result; diff --git a/src/main/java/redis/clients/jedis/JedisPubSub.java b/src/main/java/redis/clients/jedis/JedisPubSub.java index e0c5dec..6ff0779 100644 --- a/src/main/java/redis/clients/jedis/JedisPubSub.java +++ b/src/main/java/redis/clients/jedis/JedisPubSub.java @@ -1,9 +1,16 @@ package redis.clients.jedis; +import static redis.clients.jedis.Protocol.Keyword.MESSAGE; +import static redis.clients.jedis.Protocol.Keyword.PMESSAGE; +import static redis.clients.jedis.Protocol.Keyword.PSUBSCRIBE; +import static redis.clients.jedis.Protocol.Keyword.PUNSUBSCRIBE; +import static redis.clients.jedis.Protocol.Keyword.SUBSCRIBE; +import static redis.clients.jedis.Protocol.Keyword.UNSUBSCRIBE; + import java.util.Arrays; import java.util.List; -import static redis.clients.jedis.Protocol.Keyword.*; +import redis.clients.util.SafeEncoder; public abstract class JedisPubSub { private int subscribedChannels = 0; @@ -67,76 +74,54 @@ public abstract class JedisPubSub { List reply = client.getObjectMultiBulkReply(); final Object firstObj = reply.get(0); if (!(firstObj instanceof byte[])) { - throw - new JedisException("Unknown message type: "+ firstObj); + throw new JedisException("Unknown message type: " + firstObj); } final byte[] resp = (byte[]) firstObj; - if(Arrays.equals(SUBSCRIBE.raw, resp)) { + if (Arrays.equals(SUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); final byte[] bchannel = (byte[]) reply.get(1); - final String strchannel = - (bchannel == null) ? - null : - new String(bchannel, Protocol.UTF8); - onSubscribe(strchannel, subscribedChannels); + final String strchannel = (bchannel == null) ? null + : SafeEncoder.encode(bchannel); + onSubscribe(strchannel, subscribedChannels); } else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); final byte[] bchannel = (byte[]) reply.get(1); - final String strchannel = - (bchannel == null) ? - null : - new String(bchannel, Protocol.UTF8); - onUnsubscribe(strchannel, subscribedChannels); + final String strchannel = (bchannel == null) ? null + : SafeEncoder.encode(bchannel); + onUnsubscribe(strchannel, subscribedChannels); } else if (Arrays.equals(MESSAGE.raw, resp)) { final byte[] bchannel = (byte[]) reply.get(1); final byte[] bmesg = (byte[]) reply.get(2); - final String strchannel = - (bchannel == null) ? - null : - new String(bchannel, Protocol.UTF8); - final String strmesg = - (bmesg == null) ? - null : - new String(bmesg, Protocol.UTF8); + final String strchannel = (bchannel == null) ? null + : SafeEncoder.encode(bchannel); + final String strmesg = (bmesg == null) ? null : SafeEncoder + .encode(bmesg); onMessage(strchannel, strmesg); } else if (Arrays.equals(PMESSAGE.raw, resp)) { - final byte[] bpattern = (byte[]) reply.get(1); + final byte[] bpattern = (byte[]) reply.get(1); final byte[] bchannel = (byte[]) reply.get(2); final byte[] bmesg = (byte[]) reply.get(3); - final String strpattern = - (bpattern == null) ? - null : - new String(bpattern, Protocol.UTF8); - final String strchannel = - (bchannel == null) ? - null : - new String(bchannel, Protocol.UTF8); - final String strmesg = - (bmesg == null) ? - null : - new String(bmesg, Protocol.UTF8); - onPMessage( - strpattern, - strchannel, - strmesg); + final String strpattern = (bpattern == null) ? null + : SafeEncoder.encode(bpattern); + final String strchannel = (bchannel == null) ? null + : SafeEncoder.encode(bchannel); + final String strmesg = (bmesg == null) ? null : SafeEncoder + .encode(bmesg); + onPMessage(strpattern, strchannel, strmesg); } else if (Arrays.equals(PSUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); - final byte[] bpattern = (byte[]) reply.get(1); - final String strpattern = - (bpattern == null) ? - null : - new String(bpattern, Protocol.UTF8); + final byte[] bpattern = (byte[]) reply.get(1); + final String strpattern = (bpattern == null) ? null + : SafeEncoder.encode(bpattern); onPSubscribe(strpattern, subscribedChannels); } else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); - final byte[] bpattern = (byte[]) reply.get(1); - final String strpattern = - (bpattern == null) ? - null : - new String(bpattern, Protocol.UTF8); + final byte[] bpattern = (byte[]) reply.get(1); + final String strpattern = (bpattern == null) ? null + : SafeEncoder.encode(bpattern); onPUnsubscribe(strpattern, subscribedChannels); } else { - throw new JedisException("Unknown message type: "+ firstObj); + throw new JedisException("Unknown message type: " + firstObj); } } while (isSubscribed()); } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 7c55fc2..7ec6f20 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -1,19 +1,19 @@ package redis.clients.jedis; import java.io.IOException; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; +import redis.clients.util.SafeEncoder; public final class Protocol { public static final int DEFAULT_PORT = 6379; public static final int DEFAULT_TIMEOUT = 2000; - public static final Charset UTF8 = Charset.forName("UTF-8"); + public static final String CHARSET = "UTF-8"; public static final byte DOLLAR_BYTE = '$'; public static final byte ASTERISK_BYTE = '*'; @@ -76,7 +76,7 @@ public final class Protocol { } private byte[] processStatusCodeReply(final RedisInputStream is) { - return is.readLine().getBytes(UTF8); + return SafeEncoder.encode(is.readLine()); } private byte[] processBulkReply(final RedisInputStream is) { @@ -122,15 +122,15 @@ public final class Protocol { } public static final byte[] toByteArray(final int value) { - return String.valueOf(value).getBytes(Protocol.UTF8); + return SafeEncoder.encode(String.valueOf(value)); } public static final byte[] toByteArray(final long value) { - return String.valueOf(value).getBytes(Protocol.UTF8); + return SafeEncoder.encode(String.valueOf(value)); } public static final byte[] toByteArray(final double value) { - return String.valueOf(value).getBytes(Protocol.UTF8); + return SafeEncoder.encode(String.valueOf(value)); } public static enum Command { @@ -139,7 +139,7 @@ public final class Protocol { public final byte[] raw; Command() { - raw = this.name().getBytes(UTF8); + raw = SafeEncoder.encode(this.name()); } } @@ -148,7 +148,7 @@ public final class Protocol { public final byte[] raw; Keyword() { - raw = this.name().toLowerCase().getBytes(UTF8); + raw = SafeEncoder.encode(this.name().toLowerCase()); } } diff --git a/src/main/java/redis/clients/jedis/SortingParams.java b/src/main/java/redis/clients/jedis/SortingParams.java index 2129025..cfdf03b 100644 --- a/src/main/java/redis/clients/jedis/SortingParams.java +++ b/src/main/java/redis/clients/jedis/SortingParams.java @@ -1,10 +1,20 @@ package redis.clients.jedis; +import static redis.clients.jedis.Protocol.Keyword.ALPHA; +import static redis.clients.jedis.Protocol.Keyword.ASC; +import static redis.clients.jedis.Protocol.Keyword.BY; +import static redis.clients.jedis.Protocol.Keyword.DESC; +import static redis.clients.jedis.Protocol.Keyword.GET; +import static redis.clients.jedis.Protocol.Keyword.LIMIT; +import static redis.clients.jedis.Protocol.Keyword.NOSORT; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import static redis.clients.jedis.Protocol.Keyword.*; + +import redis.clients.util.SafeEncoder; + /** * Builder Class for {@link Jedis#sort(String, SortingParams) SORT} Parameters. * @@ -26,7 +36,7 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams by(final String pattern) { - return by(pattern.getBytes(Protocol.UTF8)); + return by(SafeEncoder.encode(pattern)); } /** @@ -43,9 +53,9 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams by(final byte[] pattern) { - params.add(BY.raw); - params.add(pattern); - return this; + params.add(BY.raw); + params.add(pattern); + return this; } /** @@ -57,13 +67,13 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams nosort() { - params.add(BY.raw); - params.add(NOSORT.raw); - return this; + params.add(BY.raw); + params.add(NOSORT.raw); + return this; } public Collection getParams() { - return Collections.unmodifiableCollection(params); + return Collections.unmodifiableCollection(params); } /** @@ -72,8 +82,8 @@ public class SortingParams { * @return the sortingParams Object */ public SortingParams desc() { - params.add(DESC.raw); - return this; + params.add(DESC.raw); + return this; } /** @@ -82,8 +92,8 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams asc() { - params.add(ASC.raw); - return this; + params.add(ASC.raw); + return this; } /** @@ -95,10 +105,10 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams limit(final int start, final int count) { - params.add(LIMIT.raw); - params.add(Protocol.toByteArray(start)); - params.add(Protocol.toByteArray(count)); - return this; + params.add(LIMIT.raw); + params.add(Protocol.toByteArray(start)); + params.add(Protocol.toByteArray(count)); + return this; } /** @@ -108,8 +118,8 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams alpha() { - params.add(ALPHA.raw); - return this; + params.add(ALPHA.raw); + return this; } /** @@ -128,11 +138,11 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams get(String... patterns) { - for (final String pattern : patterns) { - params.add(GET.raw); - params.add(pattern.getBytes(Protocol.UTF8)); - } - return this; + for (final String pattern : patterns) { + params.add(GET.raw); + params.add(SafeEncoder.encode(pattern)); + } + return this; } /** @@ -151,10 +161,10 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams get(byte[]... patterns) { - for (final byte[] pattern : patterns) { - params.add(GET.raw); - params.add(pattern); - } - return this; + for (final byte[] pattern : patterns) { + params.add(GET.raw); + params.add(pattern); + } + return this; } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Tuple.java b/src/main/java/redis/clients/jedis/Tuple.java index 425748d..6df12d2 100644 --- a/src/main/java/redis/clients/jedis/Tuple.java +++ b/src/main/java/redis/clients/jedis/Tuple.java @@ -2,75 +2,77 @@ package redis.clients.jedis; import java.util.Arrays; +import redis.clients.util.SafeEncoder; + public class Tuple { private byte[] element; private Double score; @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result; - if (null != element) { - for(final byte b : element) { - result = prime * result + b; - } - } - long temp; - temp = Double.doubleToLongBits(score); - result = prime * result + (int) (temp ^ (temp >>> 32)); - return result; + final int prime = 31; + int result = 1; + result = prime * result; + if (null != element) { + for (final byte b : element) { + result = prime * result + b; + } + } + long temp; + temp = Double.doubleToLongBits(score); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Tuple other = (Tuple) obj; - if (element == null) { - if (other.element != null) - return false; - } else if (!Arrays.equals(element, other.element)) - return false; - if (Double.doubleToLongBits(score) != Double - .doubleToLongBits(other.score)) - return false; - return true; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Tuple other = (Tuple) obj; + if (element == null) { + if (other.element != null) + return false; + } else if (!Arrays.equals(element, other.element)) + return false; + if (Double.doubleToLongBits(score) != Double + .doubleToLongBits(other.score)) + return false; + return true; } public Tuple(String element, Double score) { - super(); - this.element = element.getBytes(Protocol.UTF8); - this.score = score; + super(); + this.element = SafeEncoder.encode(element); + this.score = score; } public Tuple(byte[] element, Double score) { - super(); - this.element = element; - this.score = score; - } + super(); + this.element = element; + this.score = score; + } public String getElement() { - if(null != element) { - return new String(element, Protocol.UTF8); - } else { - return null; - } + if (null != element) { + return SafeEncoder.encode(element); + } else { + return null; + } } public byte[] getBinaryElement() { - return element; + return element; } public double getScore() { - return score; + return score; } - + public String toString() { - return '['+Arrays.toString(element)+','+score+']'; + return '[' + Arrays.toString(element) + ',' + score + ']'; } } diff --git a/src/main/java/redis/clients/jedis/ZParams.java b/src/main/java/redis/clients/jedis/ZParams.java index a260397..daa6f8e 100644 --- a/src/main/java/redis/clients/jedis/ZParams.java +++ b/src/main/java/redis/clients/jedis/ZParams.java @@ -1,40 +1,44 @@ package redis.clients.jedis; +import static redis.clients.jedis.Protocol.Keyword.AGGREGATE; +import static redis.clients.jedis.Protocol.Keyword.WEIGHTS; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import static redis.clients.jedis.Protocol.Keyword.*; +import redis.clients.util.SafeEncoder; public class ZParams { public enum Aggregate { - SUM, MIN, MAX; - - public final byte[] raw; - Aggregate() { - raw = name().getBytes(Protocol.UTF8); - } + SUM, MIN, MAX; + + public final byte[] raw; + + Aggregate() { + raw = SafeEncoder.encode(name()); + } } private List params = new ArrayList(); public ZParams weights(final int... weights) { - params.add(WEIGHTS.raw); - for (final int weight : weights) { - params.add(Protocol.toByteArray(weight)); - } + params.add(WEIGHTS.raw); + for (final int weight : weights) { + params.add(Protocol.toByteArray(weight)); + } - return this; + return this; } public Collection getParams() { - return Collections.unmodifiableCollection(params); + return Collections.unmodifiableCollection(params); } public ZParams aggregate(final Aggregate aggregate) { - params.add(AGGREGATE.raw); - params.add(aggregate.raw); - return this; + params.add(AGGREGATE.raw); + params.add(aggregate.raw); + return this; } } diff --git a/src/main/java/redis/clients/util/Hashing.java b/src/main/java/redis/clients/util/Hashing.java index 5eaa023..3323347 100644 --- a/src/main/java/redis/clients/util/Hashing.java +++ b/src/main/java/redis/clients/util/Hashing.java @@ -3,38 +3,37 @@ package redis.clients.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import redis.clients.jedis.Protocol; - public interface Hashing { public static final Hashing MURMUR_HASH = new MurmurHash(); public static final Hashing MD5 = new Hashing() { - private MessageDigest md5 = null; // avoid recurring construction - - public long hash(String key) { - return hash(key.getBytes(Protocol.UTF8)); - } + private MessageDigest md5 = null; // avoid recurring construction - public long hash(byte[] key) { - if (md5 == null) { - try { - md5 = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException( - "++++ no md5 algorythm found"); - } - } + public long hash(String key) { + return hash(SafeEncoder.encode(key)); + } - md5.reset(); - md5.update(key); - byte[] bKey = md5.digest(); - long res = ((long) (bKey[3] & 0xFF) << 24) - | ((long) (bKey[2] & 0xFF) << 16) - | ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF); - return res; - } + public long hash(byte[] key) { + if (md5 == null) { + try { + md5 = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException( + "++++ no md5 algorythm found"); + } + } + + md5.reset(); + md5.update(key); + byte[] bKey = md5.digest(); + long res = ((long) (bKey[3] & 0xFF) << 24) + | ((long) (bKey[2] & 0xFF) << 16) + | ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF); + return res; + } }; public long hash(String key); + public long hash(byte[] key); } \ No newline at end of file diff --git a/src/main/java/redis/clients/util/MurmurHash.java b/src/main/java/redis/clients/util/MurmurHash.java index 934f7cd..e9327da 100644 --- a/src/main/java/redis/clients/util/MurmurHash.java +++ b/src/main/java/redis/clients/util/MurmurHash.java @@ -20,8 +20,6 @@ package redis.clients.util; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import redis.clients.jedis.Protocol; - /** * This is a very fast, non-cryptographic hash suitable for general hash-based * lookup. See http://murmurhash.googlepages.com/ for more details. @@ -42,7 +40,7 @@ public class MurmurHash implements Hashing { * @return The 32 bit hash of the bytes in question. */ public static int hash(byte[] data, int seed) { - return hash(ByteBuffer.wrap(data), seed); + return hash(ByteBuffer.wrap(data), seed); } /** @@ -59,7 +57,7 @@ public class MurmurHash implements Hashing { * @return The 32-bit hash of the data in question. */ public static int hash(byte[] data, int offset, int length, int seed) { - return hash(ByteBuffer.wrap(data, offset, length), seed); + return hash(ByteBuffer.wrap(data, offset, length), seed); } /** @@ -72,97 +70,97 @@ public class MurmurHash implements Hashing { * @return The 32 bit murmur hash of the bytes in the buffer. */ public static int hash(ByteBuffer buf, int seed) { - // save byte order for later restoration - ByteOrder byteOrder = buf.order(); - buf.order(ByteOrder.LITTLE_ENDIAN); + // save byte order for later restoration + ByteOrder byteOrder = buf.order(); + buf.order(ByteOrder.LITTLE_ENDIAN); - int m = 0x5bd1e995; - int r = 24; + int m = 0x5bd1e995; + int r = 24; - int h = seed ^ buf.remaining(); + int h = seed ^ buf.remaining(); - int k; - while (buf.remaining() >= 4) { - k = buf.getInt(); + int k; + while (buf.remaining() >= 4) { + k = buf.getInt(); - k *= m; - k ^= k >>> r; - k *= m; + k *= m; + k ^= k >>> r; + k *= m; - h *= m; - h ^= k; - } + h *= m; + h ^= k; + } - if (buf.remaining() > 0) { - ByteBuffer finish = ByteBuffer.allocate(4).order( - ByteOrder.LITTLE_ENDIAN); - // for big-endian version, use this first: - // finish.position(4-buf.remaining()); - finish.put(buf).rewind(); - h ^= finish.getInt(); - h *= m; - } + if (buf.remaining() > 0) { + ByteBuffer finish = ByteBuffer.allocate(4).order( + ByteOrder.LITTLE_ENDIAN); + // for big-endian version, use this first: + // finish.position(4-buf.remaining()); + finish.put(buf).rewind(); + h ^= finish.getInt(); + h *= m; + } - h ^= h >>> 13; - h *= m; - h ^= h >>> 15; + h ^= h >>> 13; + h *= m; + h ^= h >>> 15; - buf.order(byteOrder); - return h; + buf.order(byteOrder); + return h; } public static long hash64A(byte[] data, int seed) { - return hash64A(ByteBuffer.wrap(data), seed); + return hash64A(ByteBuffer.wrap(data), seed); } public static long hash64A(byte[] data, int offset, int length, int seed) { - return hash64A(ByteBuffer.wrap(data, offset, length), seed); + return hash64A(ByteBuffer.wrap(data, offset, length), seed); } public static long hash64A(ByteBuffer buf, int seed) { - ByteOrder byteOrder = buf.order(); - buf.order(ByteOrder.LITTLE_ENDIAN); + ByteOrder byteOrder = buf.order(); + buf.order(ByteOrder.LITTLE_ENDIAN); - long m = 0xc6a4a7935bd1e995L; - int r = 47; + long m = 0xc6a4a7935bd1e995L; + int r = 47; - long h = seed ^ (buf.remaining() * m); + long h = seed ^ (buf.remaining() * m); - long k; - while (buf.remaining() >= 8) { - k = buf.getLong(); + long k; + while (buf.remaining() >= 8) { + k = buf.getLong(); - k *= m; - k ^= k >>> r; - k *= m; + k *= m; + k ^= k >>> r; + k *= m; - h ^= k; - h *= m; - } + h ^= k; + h *= m; + } - if (buf.remaining() > 0) { - ByteBuffer finish = ByteBuffer.allocate(8).order( - ByteOrder.LITTLE_ENDIAN); - // for big-endian version, do this first: - // finish.position(8-buf.remaining()); - finish.put(buf).rewind(); - h ^= finish.getLong(); - h *= m; - } + if (buf.remaining() > 0) { + ByteBuffer finish = ByteBuffer.allocate(8).order( + ByteOrder.LITTLE_ENDIAN); + // for big-endian version, do this first: + // finish.position(8-buf.remaining()); + finish.put(buf).rewind(); + h ^= finish.getLong(); + h *= m; + } - h ^= h >>> r; - h *= m; - h ^= h >>> r; + h ^= h >>> r; + h *= m; + h ^= h >>> r; - buf.order(byteOrder); - return h; + buf.order(byteOrder); + return h; } public long hash(byte[] key) { - return hash64A(key, 0x1234ABCD); + return hash64A(key, 0x1234ABCD); } - + public long hash(String key) { - return hash(key.getBytes(Protocol.UTF8)); + return hash(SafeEncoder.encode(key)); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/util/SafeEncoder.java b/src/main/java/redis/clients/util/SafeEncoder.java new file mode 100644 index 0000000..3b95395 --- /dev/null +++ b/src/main/java/redis/clients/util/SafeEncoder.java @@ -0,0 +1,28 @@ +package redis.clients.util; + +import java.io.UnsupportedEncodingException; + +import redis.clients.jedis.JedisException; +import redis.clients.jedis.Protocol; + +/** + * The only reason to have this is to be able to compatible with java 1.5 :( + * + */ +public class SafeEncoder { + public static byte[] encode(final String str) { + try { + return str.getBytes(Protocol.CHARSET); + } catch (UnsupportedEncodingException e) { + throw new JedisException(e); + } + } + + public static String encode(final byte[] data) { + try { + return new String(data, Protocol.CHARSET); + } catch (UnsupportedEncodingException e) { + throw new JedisException(e); + } + } +} diff --git a/src/main/java/redis/clients/util/Sharded.java b/src/main/java/redis/clients/util/Sharded.java index d0f4f75..41a15ef 100644 --- a/src/main/java/redis/clients/util/Sharded.java +++ b/src/main/java/redis/clients/util/Sharded.java @@ -2,8 +2,10 @@ package redis.clients.util; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.TreeMap; +import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -22,69 +24,73 @@ public class Sharded> { private Pattern tagPattern = null; // the tag is anything between {} public static final Pattern DEFAULT_KEY_TAG_PATTERN = Pattern - .compile("\\{(.+?)\\}"); + .compile("\\{(.+?)\\}"); public Sharded(List shards) { - this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works - // with 64-bits not 128 + this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works + // with 64-bits not 128 } public Sharded(List shards, Hashing algo) { - this.algo = algo; - initialize(shards); + this.algo = algo; + initialize(shards); } public Sharded(List shards, Pattern tagPattern) { - this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good - // as we works with - // 64-bits not 128 + this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good + // as we works with + // 64-bits not 128 } public Sharded(List shards, Hashing algo, Pattern tagPattern) { - this.algo = algo; - this.tagPattern = tagPattern; - initialize(shards); + this.algo = algo; + this.tagPattern = tagPattern; + initialize(shards); } private void initialize(List shards) { - nodes = new TreeMap(); + nodes = new TreeMap(); - int totalWeight = 0; + int totalWeight = 0; - for (ShardInfo shard : shards) { - totalWeight += shard.getWeight(); - } + for (ShardInfo shard : shards) { + totalWeight += shard.getWeight(); + } - long oneForthOfStep = (1L << 62) / totalWeight; // 62 vs 64 to normalize - // math in Long + long oneForthOfStep = (1L << 62) / totalWeight; // 62 vs 64 to normalize + // math in Long - long floor = Long.MIN_VALUE; - for (int i = 0; i != shards.size(); ++i) { - final S shardInfo = shards.get(i); - shardInfo.initResource(); - nodes.put(floor, shardInfo); - floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to - // compensate - // 62 vs 64 - } + long floor = Long.MIN_VALUE; + for (int i = 0; i != shards.size(); ++i) { + final S shardInfo = shards.get(i); + shardInfo.initResource(); + nodes.put(floor, shardInfo); + floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to + // compensate + // 62 vs 64 + } } - public R getShard(byte[] key) { - return nodes - .floorEntry(algo.hash(key)) - .getValue() - .getResource(); - } + public R getShard(byte[] key) { + return getShardInfo(key).getResource(); + } - public R getShard(String key) { - return nodes - .floorEntry(algo.hash(getKeyTag(key))) - .getValue() - .getResource(); - } + public R getShard(String key) { + return getShardInfo(key).getResource(); + } + + private S getShardInfo(byte[] key) { + Iterator> iterator = nodes.headMap(algo.hash(key)) + .entrySet().iterator(); + Entry next = iterator.next(); + if (iterator.hasNext()) { + next = iterator.next(); + } + return next.getValue(); + } public S getShardInfo(String key) { - return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue(); + return getShardInfo(SafeEncoder.encode(getKeyTag(key))); } /** @@ -97,15 +103,15 @@ public class Sharded> { * @return The tag if it exists, or the original key */ public String getKeyTag(String key) { - if (tagPattern != null) { - Matcher m = tagPattern.matcher(key); - if (m.find()) - return m.group(1); - } - return key; + if (tagPattern != null) { + Matcher m = tagPattern.matcher(key); + if (m.find()) + return m.group(1); + } + return key; } public Collection getAllShards() { - return Collections.unmodifiableCollection(nodes.values()); + return Collections.unmodifiableCollection(nodes.values()); } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 441fb6a..a4b5cc0 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -9,6 +9,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; import redis.clients.jedis.tests.commands.JedisCommandTestBase; +import redis.clients.util.SafeEncoder; public class JedisTest extends JedisCommandTestBase { @Test @@ -25,7 +26,7 @@ public class JedisTest extends JedisCommandTestBase { bigdata[b] = (byte) ((byte) b % 255); } Map hash = new HashMap(); - hash.put("data", new String(bigdata, Protocol.UTF8)); + hash.put("data", SafeEncoder.encode(bigdata)); String status = jedis.hmset("foo", hash); assertEquals("OK", status); diff --git a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java index 8df99d6..6fafd08 100644 --- a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java @@ -37,7 +37,7 @@ public class PipeliningTest extends Assert { }); assertEquals(2, results.size()); - assertArrayEquals("OK".getBytes(Protocol.UTF8), (byte[])results.get(0)); - assertArrayEquals("bar".getBytes(Protocol.UTF8), (byte[])results.get(1)); + assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[])results.get(0)); + assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[])results.get(1)); } } diff --git a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java index 425c465..2d97fd0 100644 --- a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import redis.clients.jedis.Protocol; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; +import redis.clients.util.SafeEncoder; public class ProtocolTest extends JedisTestBase { @Test @@ -24,7 +25,7 @@ public class ProtocolTest extends JedisTestBase { Protocol protocol = new Protocol(); protocol.sendCommand(new RedisOutputStream(pos), Protocol.Command.GET, - "SOMEKEY".getBytes(Protocol.UTF8)); + "SOMEKEY".getBytes(Protocol.CHARSET)); pos.close(); String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n"; @@ -43,7 +44,7 @@ public class ProtocolTest extends JedisTestBase { InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes()); Protocol protocol = new Protocol(); byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); - assertArrayEquals("foobar".getBytes(Protocol.UTF8), response); + assertArrayEquals(SafeEncoder.encode("foobar"), response); } @Test @@ -52,8 +53,8 @@ public class ProtocolTest extends JedisTestBase { "$30\r\n012345678901234567890123456789\r\n".getBytes()); Protocol protocol = new Protocol(); byte[] response = (byte[]) protocol.read(new RedisInputStream(fis)); - assertArrayEquals("012345678901234567890123456789" - .getBytes(Protocol.UTF8), response); + assertArrayEquals(SafeEncoder.encode("012345678901234567890123456789"), + response); } @Test @@ -69,7 +70,7 @@ public class ProtocolTest extends JedisTestBase { InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes()); Protocol protocol = new Protocol(); byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); - assertArrayEquals("OK".getBytes(Protocol.UTF8), response); + assertArrayEquals(SafeEncoder.encode("OK"), response); } @Test @@ -90,10 +91,10 @@ public class ProtocolTest extends JedisTestBase { List response = (List) protocol .read(new RedisInputStream(is)); List expected = new ArrayList(); - expected.add("foo".getBytes(Protocol.UTF8)); - expected.add("bar".getBytes(Protocol.UTF8)); - expected.add("Hello".getBytes(Protocol.UTF8)); - expected.add("World".getBytes(Protocol.UTF8)); + expected.add(SafeEncoder.encode("foo")); + expected.add(SafeEncoder.encode("bar")); + expected.add(SafeEncoder.encode("Hello")); + expected.add(SafeEncoder.encode("World")); assertEquals(expected, response); } diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java index 2c23847..e0107e0 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java @@ -9,11 +9,11 @@ import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; -import redis.clients.jedis.Protocol; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import redis.clients.util.Hashing; +import redis.clients.util.SafeEncoder; public class ShardedJedisTest extends Assert { private static HostAndPort redis1 = HostAndPortUtil.getRedisServers() @@ -143,11 +143,11 @@ public class ShardedJedisTest extends Assert { }); List expected = new ArrayList(2); - expected.add("a".getBytes(Protocol.UTF8)); - expected.add("b".getBytes(Protocol.UTF8)); + expected.add(SafeEncoder.encode("a")); + expected.add(SafeEncoder.encode("b")); assertEquals(2, results.size()); - assertArrayEquals("a".getBytes(Protocol.UTF8), (byte[]) results.get(0)); - assertArrayEquals("b".getBytes(Protocol.UTF8), (byte[]) results.get(1)); + assertArrayEquals(SafeEncoder.encode("a"), (byte[]) results.get(0)); + assertArrayEquals(SafeEncoder.encode("b"), (byte[]) results.get(1)); } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index 0375080..1aee8b8 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -7,7 +7,7 @@ import java.util.Set; import org.junit.Test; import redis.clients.jedis.JedisException; -import redis.clients.jedis.Protocol; +import redis.clients.util.SafeEncoder; public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; @@ -447,8 +447,8 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { assertEquals("hello world", result); // Binary - byte[] bresult = jedis.echo("hello world".getBytes(Protocol.UTF8)); - assertArrayEquals("hello world".getBytes(Protocol.UTF8), bresult); + byte[] bresult = jedis.echo(SafeEncoder.encode("hello world")); + assertArrayEquals(SafeEncoder.encode("hello world"), bresult); } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index 7f45f21..76c7796 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -5,9 +5,9 @@ import java.util.Set; import org.junit.Test; -import redis.clients.jedis.Protocol; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; +import redis.clients.util.SafeEncoder; public class SortedSetCommandsTest extends JedisCommandTestBase { final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; @@ -359,7 +359,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bscore = jedis.zscore(bfoo, bc); assertEquals((Double) 0.1d, bscore); - bscore = jedis.zscore(bfoo, "s".getBytes(Protocol.UTF8)); + bscore = jedis.zscore(bfoo, SafeEncoder.encode("s")); assertNull(bscore); } @@ -440,8 +440,8 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { assertEquals(bexpected, brange); brange = jedis.zrangeByScore(bfoo, 0d, 2d, 1, 1); - Set brange2 = jedis.zrangeByScore(bfoo, "-inf" - .getBytes(Protocol.UTF8), "(2".getBytes(Protocol.UTF8)); + Set brange2 = jedis.zrangeByScore(bfoo, SafeEncoder + .encode("-inf"), SafeEncoder.encode("(2")); assertEquals(bexpected, brange2); bexpected = new LinkedHashSet(); @@ -601,8 +601,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { jedis.zadd(bbar, 2, ba); jedis.zadd(bbar, 2, bb); - int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bfoo, - bbar); + int bresult = jedis.zunionstore(SafeEncoder.encode("dst"), bfoo, bbar); assertEquals(2, bresult); @@ -610,8 +609,8 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(bb, new Double(4))); bexpected.add(new Tuple(ba, new Double(3))); - assertEquals(bexpected, jedis.zrangeWithScores("dst" - .getBytes(Protocol.UTF8), 0, 100)); + assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder + .encode("dst"), 0, 100)); } @Test @@ -643,7 +642,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { ZParams bparams = new ZParams(); bparams.weights(2, 2); bparams.aggregate(ZParams.Aggregate.SUM); - int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bparams, + int bresult = jedis.zunionstore(SafeEncoder.encode("dst"), bparams, bfoo, bbar); assertEquals(2, bresult); @@ -652,8 +651,8 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(bb, new Double(8))); bexpected.add(new Tuple(ba, new Double(6))); - assertEquals(bexpected, jedis.zrangeWithScores("dst" - .getBytes(Protocol.UTF8), 0, 100)); + assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder + .encode("dst"), 0, 100)); } @Test @@ -676,16 +675,15 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { jedis.zadd(bfoo, 2, bb); jedis.zadd(bbar, 2, ba); - int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bfoo, - bbar); + int bresult = jedis.zinterstore(SafeEncoder.encode("dst"), bfoo, bbar); assertEquals(1, bresult); Set bexpected = new LinkedHashSet(); bexpected.add(new Tuple(ba, new Double(3))); - assertEquals(bexpected, jedis.zrangeWithScores("dst" - .getBytes(Protocol.UTF8), 0, 100)); + assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder + .encode("dst"), 0, 100)); } @Test @@ -714,7 +712,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { ZParams bparams = new ZParams(); bparams.weights(2, 2); bparams.aggregate(ZParams.Aggregate.SUM); - int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bparams, + int bresult = jedis.zinterstore(SafeEncoder.encode("dst"), bparams, bfoo, bbar); assertEquals(1, bresult); @@ -722,7 +720,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { Set bexpected = new LinkedHashSet(); bexpected.add(new Tuple(ba, new Double(6))); - assertEquals(bexpected, jedis.zrangeWithScores("dst" - .getBytes(Protocol.UTF8), 0, 100)); + assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder + .encode("dst"), 0, 100)); } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java index 00a01c3..ac88554 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -169,7 +169,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set("mykey", val); List resp = t.exec(); assertEquals(1, resp.size()); - assertArrayEquals(Keyword.OK.name().getBytes(Protocol.UTF8), + assertArrayEquals(Keyword.OK.name().getBytes(Protocol.CHARSET), (byte[]) resp.get(0)); // Binary @@ -188,7 +188,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set(bmykey, bval); resp = t.exec(); assertEquals(1, resp.size()); - assertArrayEquals(Keyword.OK.name().getBytes(Protocol.UTF8), + assertArrayEquals(Keyword.OK.name().getBytes(Protocol.CHARSET), (byte[]) resp.get(0)); }