Fixed issue with converting String to byte array
This commit is contained in:
@@ -3,34 +3,74 @@ package redis.clients.jedis;
|
|||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Protocol {
|
public class Protocol {
|
||||||
|
public static final Charset CHARSET = Charset.forName("UTF-8");
|
||||||
|
|
||||||
public static final String DOLLAR = "$";
|
public static final String DOLLAR = "$";
|
||||||
public static final String ASTERISK = "*";
|
public static final String ASTERISK = "*";
|
||||||
public static final String PLUS = "+";
|
public static final String PLUS = "+";
|
||||||
public static final String MINUS = "-";
|
public static final String MINUS = "-";
|
||||||
public static final String COLON = ":";
|
public static final String COLON = ":";
|
||||||
public static final String COMMAND_DELIMITER = "\r\n";
|
public static final String COMMAND_DELIMITER = "\r\n";
|
||||||
|
public static final byte[] COMMAND_DELIMITER_BYTES = "\r\n"
|
||||||
|
.getBytes(CHARSET);
|
||||||
public static final int DEFAULT_PORT = 6379;
|
public static final int DEFAULT_PORT = 6379;
|
||||||
|
|
||||||
public static final byte DOLLAR_BYTE = DOLLAR.getBytes()[0];
|
public static final byte DOLLAR_BYTE = DOLLAR.getBytes(CHARSET)[0];
|
||||||
public static final byte ASTERISK_BYTE = ASTERISK.getBytes()[0];
|
public static final byte ASTERISK_BYTE = ASTERISK.getBytes(CHARSET)[0];
|
||||||
public static final byte PLUS_BYTE = PLUS.getBytes()[0];
|
public static final byte PLUS_BYTE = PLUS.getBytes(CHARSET)[0];
|
||||||
public static final byte MINUS_BYTE = MINUS.getBytes()[0];
|
public static final byte MINUS_BYTE = MINUS.getBytes(CHARSET)[0];
|
||||||
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
public static final byte COLON_BYTE = COLON.getBytes(CHARSET)[0];
|
||||||
|
|
||||||
public void sendCommand(DataOutputStream os, String name, String... args) {
|
public void sendCommand(DataOutputStream os, String name, String... args) {
|
||||||
StringBuilder builder = new StringBuilder(ASTERISK + (args.length + 1)
|
StringBuilder sb = new StringBuilder();
|
||||||
+ COMMAND_DELIMITER + DOLLAR + name.length()
|
sb.append(ASTERISK);
|
||||||
+ COMMAND_DELIMITER + name + COMMAND_DELIMITER);
|
sb.append((new Integer(args.length + 1)).toString());
|
||||||
for (String arg : args) {
|
sb.append(COMMAND_DELIMITER);
|
||||||
builder.append(DOLLAR).append(arg.length()).append(
|
sb.append(DOLLAR);
|
||||||
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
sb.append((new Integer(name.length())).toString());
|
||||||
}
|
sb.append(COMMAND_DELIMITER);
|
||||||
try {
|
sb.append(name);
|
||||||
os.write(builder.toString().getBytes());
|
sb.append(COMMAND_DELIMITER);
|
||||||
|
|
||||||
|
for (String arg : args) {
|
||||||
|
int size = arg.getBytes(CHARSET).length;
|
||||||
|
|
||||||
|
sb.append(DOLLAR);
|
||||||
|
sb.append((new Integer(size)).toString());
|
||||||
|
sb.append(COMMAND_DELIMITER);
|
||||||
|
sb.append(arg);
|
||||||
|
sb.append(COMMAND_DELIMITER);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
os.write(sb.toString().getBytes(CHARSET));
|
||||||
|
/*
|
||||||
|
os.write(ASTERISK_BYTE);
|
||||||
|
os.write((new Integer(args.length + 1)).toString()
|
||||||
|
.getBytes(CHARSET));
|
||||||
|
os.write(COMMAND_DELIMITER_BYTES);
|
||||||
|
os.write(DOLLAR_BYTE);
|
||||||
|
os.write((new Integer(name.length())).toString().getBytes(CHARSET));
|
||||||
|
os.write(COMMAND_DELIMITER_BYTES);
|
||||||
|
os.write(name.getBytes(CHARSET));
|
||||||
|
os.write(COMMAND_DELIMITER_BYTES);
|
||||||
|
|
||||||
|
for (String arg : args) {
|
||||||
|
byte[] barg = arg.getBytes(CHARSET);
|
||||||
|
|
||||||
|
os.write(DOLLAR_BYTE);
|
||||||
|
os.write((new Integer(barg.length)).toString()
|
||||||
|
.getBytes(CHARSET));
|
||||||
|
os.write(COMMAND_DELIMITER_BYTES);
|
||||||
|
os.write(barg);
|
||||||
|
os.write(COMMAND_DELIMITER_BYTES);
|
||||||
|
}
|
||||||
|
*/
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new JedisException(e);
|
throw new JedisException(e);
|
||||||
}
|
}
|
||||||
@@ -108,7 +148,7 @@ public class Protocol {
|
|||||||
throw new JedisException(e);
|
throw new JedisException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new String(read);
|
return new String(read, CHARSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object processInteger(DataInputStream is) {
|
private Object processInteger(DataInputStream is) {
|
||||||
|
|||||||
@@ -1,14 +1,34 @@
|
|||||||
package redis.clients.jedis.tests;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisException;
|
import redis.clients.jedis.JedisException;
|
||||||
|
import redis.clients.jedis.Protocol;
|
||||||
|
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
|
||||||
|
|
||||||
public class JedisTest {
|
public class JedisTest extends JedisCommandTestBase {
|
||||||
@Test(expected = JedisException.class)
|
@Test(expected = JedisException.class)
|
||||||
public void useWithoutConnecting() {
|
public void useWithoutConnecting() {
|
||||||
Jedis jedis = new Jedis("localhost");
|
Jedis jedis = new Jedis("localhost");
|
||||||
jedis.dbSize();
|
jedis.dbSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkBinaryData() {
|
||||||
|
byte[] bigdata = new byte[1777];
|
||||||
|
for (int b = 0; b < bigdata.length; b++) {
|
||||||
|
bigdata[b] = (byte) ((byte) b % 255);
|
||||||
|
}
|
||||||
|
Map<String, String> hash = new HashMap<String, String>();
|
||||||
|
hash.put("data", new String(bigdata, Protocol.CHARSET));
|
||||||
|
|
||||||
|
String status = jedis.hmset("foo", hash);
|
||||||
|
assertEquals("OK", status);
|
||||||
|
assertEquals(hash, jedis.hgetAll("foo"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,8 @@ import java.util.Map;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Protocol;
|
||||||
|
|
||||||
public class HashesCommandsTest extends JedisCommandTestBase {
|
public class HashesCommandsTest extends JedisCommandTestBase {
|
||||||
@Test
|
@Test
|
||||||
public void hset() {
|
public void hset() {
|
||||||
@@ -154,5 +156,4 @@ public class HashesCommandsTest extends JedisCommandTestBase {
|
|||||||
expected.put("car", "bar");
|
expected.put("car", "bar");
|
||||||
assertEquals(expected, hash);
|
assertEquals(expected, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user