+ used UTF8 instead of ISO-8559-1 as the default charset

This commit is contained in:
Costin Leau
2010-11-12 15:11:07 +02:00
parent 278a915075
commit 9795a9254c
4 changed files with 37 additions and 88 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.datastore.redis.connection.jredis;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
@@ -39,9 +40,9 @@ public class JredisConnection implements RedisConnection {
private final JRedis jredis;
private final String charset;
private final Charset charset;
public JredisConnection(JRedis jredis, String charset) {
public JredisConnection(JRedis jredis, Charset charset) {
this.jredis = jredis;
this.charset = charset;
}
@@ -127,7 +128,7 @@ public class JredisConnection implements RedisConnection {
@Override
public Collection<byte[]> keys(byte[] pattern) {
try {
return JredisUtils.convert(charset, jredis.keys(pattern));
return JredisUtils.convert(charset, jredis.keys(JredisUtils.convert(charset, pattern)));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -475,7 +476,7 @@ public class JredisConnection implements RedisConnection {
try {
List<byte[]> result = jredis.sdiff(set1, sets);
return JredisUtils.convertToStringCollection(result, Set.class);
return new LinkedHashSet<byte[]>(result);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -500,7 +501,7 @@ public class JredisConnection implements RedisConnection {
try {
List<byte[]> result = jredis.sinter(set1, sets);
return JredisUtils.convertToStringCollection(result, Set.class);
return new LinkedHashSet<byte[]>(result);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -650,8 +651,7 @@ public class JredisConnection implements RedisConnection {
@Override
public Set<byte[]> zRange(byte[] key, int start, int end) {
try {
return JredisUtils.convertToStringCollection(jredis.zrange(JredisUtils.convert(charset, key), (long) start,
(long) end), Set.class);
return new LinkedHashSet<byte[]>(jredis.zrange(JredisUtils.convert(charset, key), (long) start, (long) end));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -666,8 +666,7 @@ public class JredisConnection implements RedisConnection {
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max) {
try {
return JredisUtils.convertToStringCollection(jredis.zrangebyscore(JredisUtils.convert(charset, key), min,
max), Set.class);
return new LinkedHashSet<byte[]>(jredis.zrangebyscore(JredisUtils.convert(charset, key), min, max));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -727,8 +726,7 @@ public class JredisConnection implements RedisConnection {
@Override
public Set<byte[]> zRevRange(byte[] key, int start, int end) {
try {
return JredisUtils.convertToStringCollection(
jredis.zrevrange(JredisUtils.convert(charset, key), start, end), Set.class);
return new LinkedHashSet<byte[]>(jredis.zrevrange(JredisUtils.convert(charset, key), start, end));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -800,9 +798,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Set<Entry> hGetAll(byte[] key) {
public Map<byte[], byte[]> hGetAll(byte[] key) {
try {
return JredisUtils.convert(jredis.hgetall(JredisUtils.convert(charset, key)));
return JredisUtils.convertMap(charset, jredis.hgetall(JredisUtils.convert(charset, key)));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -838,7 +836,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public void hMSet(byte[] key, byte[][] fields, byte[][] values) {
public void hMSet(byte[] key, Map<byte[], byte[]> values) {
throw new UnsupportedOperationException();
}

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.datastore.redis.connection.jredis;
import java.nio.charset.Charset;
import org.jredis.JRedis;
import org.jredis.connector.ConnectionSpec;
import org.jredis.connector.Connection.Socket.Property;
@@ -47,8 +49,9 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
// taken from JRedis code
private int poolSize = 5;
private String charset = "ISO-8859-1";
private Charset charset = Charset.forName("UTF8");
/**
* Constructs a new <code>JredisConnectionFactory</code> instance.
@@ -89,7 +92,6 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
this.connectionSpec = connectionSpec;
}
@Override
public void afterPropertiesSet() {
if (StringUtils.hasLength(password)) {
@@ -182,7 +184,7 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
*
* @return
*/
public String getCharset() {
public Charset getCharset() {
return charset;
}
@@ -190,7 +192,7 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
/**
* @param charset
*/
public void setCharset(String charset) {
public void setCharset(Charset charset) {
this.charset = charset;
}
}

View File

@@ -16,22 +16,18 @@
package org.springframework.datastore.redis.connection.jredis;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jredis.RedisException;
import org.jredis.RedisType;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.redis.connection.DataType;
import org.springframework.datastore.redis.connection.DefaultEntry;
import org.springframework.datastore.redis.connection.RedisHashCommands.Entry;
/**
* Helper class featuring methods for JRedis connection handling, providing support for exception translation.
@@ -44,41 +40,18 @@ public abstract class JredisUtils {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
static String convert(byte[] bytes) {
return new String(bytes);
static String convert(Charset charset, byte[] bytes) {
return new String(bytes, charset);
}
static String convert(String encoding, byte[] bytes) {
try {
return new String(bytes, encoding);
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
}
static String[] convertMultiple(String encoding, byte[]... bytes) {
static String[] convertMultiple(Charset charset, byte[]... bytes) {
String[] result = new String[bytes.length];
try {
for (int i = 0; i < bytes.length; i++) {
result[i] = new String(bytes[i], encoding);
}
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
for (int i = 0; i < bytes.length; i++) {
result[i] = new String(bytes[i], charset);
}
return result;
}
static <T extends Collection<String>> T convertToStringCollection(List<byte[]> bytes, Class<T> collectionType) {
Collection<String> col = (List.class.isAssignableFrom(collectionType) ? new ArrayList<String>(bytes.size())
: new LinkedHashSet<String>(bytes.size()));
for (byte[] bs : bytes) {
col.add(new String(bs));
}
return (T) col;
}
static DataType convertDataType(RedisType type) {
switch (type) {
case NONE:
@@ -98,56 +71,32 @@ public abstract class JredisUtils {
return null;
}
static Set<Entry> convert(Map<String, byte[]> map) {
Set<Entry> entries = new LinkedHashSet<Entry>(map.size());
static Map<byte[], byte[]> convertMap(Charset charset, Map<String, byte[]> map) {
Map<byte[], byte[]> result = new LinkedHashMap<byte[], byte[]>(map.size());
for (Map.Entry<String, byte[]> entry : map.entrySet()) {
entries.add(new DefaultEntry(entry.getKey(), new String(entry.getValue())));
}
return entries;
}
static Map<String, byte[]> convert(String[] keys, String[] values) {
Map<String, byte[]> result = new LinkedHashMap<String, byte[]>(keys.length);
for (int i = 0; i < values.length; i++) {
result.put(keys[i], values[i].getBytes());
result.put(entry.getKey().getBytes(charset), entry.getValue());
}
return result;
}
static Collection<byte[]> convert(String charset, List<String> keys) {
static Collection<byte[]> convert(Charset charset, List<String> keys) {
Collection<byte[]> list = new ArrayList<byte[]>(keys.size());
try {
for (String string : keys) {
list.add(string.getBytes(charset));
}
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
for (String string : keys) {
list.add(string.getBytes(charset));
}
return list;
}
static byte[] convert(String charset, String string) {
try {
return string.getBytes(charset);
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
static byte[] convert(Charset charset, String string) {
return string.getBytes(charset);
}
static Map<String, byte[]> convert(String encoding, Map<byte[], byte[]> tuple) {
static Map<String, byte[]> convert(Charset charset, Map<byte[], byte[]> tuple) {
Map<String, byte[]> result = new LinkedHashMap<String, byte[]>(tuple.size());
try {
for (Map.Entry<byte[], byte[]> entry : tuple.entrySet()) {
result.put(new String(entry.getKey(), encoding), entry.getValue());
}
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
for (Map.Entry<byte[], byte[]> entry : tuple.entrySet()) {
result.put(new String(entry.getKey(), charset), entry.getValue());
}
return result;
}
}

View File

@@ -60,7 +60,7 @@ public abstract class AbstractRedisCollectionTest<T> {
@After
public void tearDown() throws Exception {
// remove the collection entirely since clear() doesn't always work
collection.getCommands().del(collection.getKey());
collection.getCommands().del(collection.getKey().getBytes());
((RedisConnection) collection.getCommands()).close();
destroyCollection();
}