Skip to content

Commit

Permalink
Fix #8 use UTF-8 as the Charset for all byte[] <-> String conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
smillidge committed Jul 1, 2013
1 parent 4268e08 commit be23149
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
int expiry = dis.readInt();
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray,CHARSET);
byte value[] = new byte[header.getBodyLength() - header.getExtraLength() - header.getKeyLength()];
dis.read(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcacheRequest;
import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcacheResponse;
Expand Down Expand Up @@ -53,7 +52,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
DataInputStream dis = new DataInputStream(bis);
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray, CHARSET);
byte value[] = new byte[header.getBodyLength() - header.getKeyLength()];
dis.read(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
DataInputStream dis = new DataInputStream(bis);
long delta = dis.readLong();
long initial = dis.readLong();
long returnVal = initial;
int expiration = dis.readInt();
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray,CHARSET);

Object object = cache.get(key);
if (object != null && object instanceof CacheEntry) {
Expand All @@ -74,10 +73,9 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
if (counter < 0) { // counter should not go below zero
counter = 0;
}
returnVal = counter;
cache.put(key, new CacheEntry(header.getOpaque(), Long.toString(counter).getBytes(), cas), expiration);
cache.put(key, new CacheEntry(header.getOpaque(), Long.toString(counter).getBytes(CHARSET), cas), expiration);
ByteBuffer buff = ByteBuffer.allocate(8);
buff.putLong(returnVal);
buff.putLong(counter);
returnArray = buff.array();
responseHeader.setStatus(ResponseStatus.NO_ERROR.status);
} catch (NumberFormatException nfe) {
Expand All @@ -89,7 +87,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
}
} else if (object == null) {
if (expiration != 0xffffffff) {
cache.put(key, new CacheEntry(header.getOpaque(),Long.toString(initial).getBytes(), cas), expiration);
cache.put(key, new CacheEntry(header.getOpaque(),Long.toString(initial).getBytes(CHARSET), cas), expiration);
responseHeader.setStatus(ResponseStatus.NO_ERROR.status);
ByteBuffer buff = ByteBuffer.allocate(8);
buff.putLong(initial);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcachedBinaryHeader;
import uk.co.c2b2.coherence.memcached.server.binaryprotocol.OpCode;

import java.nio.charset.Charset;

/**
*
Expand All @@ -49,7 +48,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {

// decode the key
byte keyBytes[] = request.getData();
String key = new String(keyBytes, Charset.defaultCharset());
String key = new String(keyBytes, CHARSET);


Object objectEntry = cache.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
private byte[] serializeToJSON(Object object) {
Gson gson = new Gson();
String jsonStr = gson.toJson(object);
return jsonStr.getBytes();
return jsonStr.getBytes(Charset.forName("UTF-8"));
}

protected boolean keyRequired = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
int expiration = dis.readInt();
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray, CHARSET);

Object object = cache.get(key);
if (object != null && object instanceof CacheEntry) {
Expand All @@ -75,7 +75,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
ByteBuffer buff = ByteBuffer.allocate(8);
buff.putLong(counter);
returnArray = buff.array();
cache.put(key, new CacheEntry(header.getOpaque(),Long.toString(counter).getBytes(),cas), expiration);
cache.put(key, new CacheEntry(header.getOpaque(),Long.toString(counter).getBytes(CHARSET),cas), expiration);
responseHeader.setStatus(ResponseStatus.NO_ERROR.status);
} catch (NumberFormatException nfe) {
responseHeader.setStatus(ResponseStatus.NON_NUMERIC.status);
Expand All @@ -86,7 +86,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
}
} else if (object == null) {
if (expiration != 0xffffffff) {
cache.put(key, new CacheEntry(header.getOpaque(), Long.toString(initial).getBytes(), cas), expiration);
cache.put(key, new CacheEntry(header.getOpaque(), Long.toString(initial).getBytes(CHARSET), cas), expiration);
responseHeader.setStatus(ResponseStatus.NO_ERROR.status);
ByteBuffer buff = ByteBuffer.allocate(8);
buff.putLong(initial);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package uk.co.c2b2.coherence.memcached.server.binaryprotocol.operation;

import com.tangosol.net.NamedCache;
import java.nio.charset.Charset;
import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcacheRequest;
import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcacheResponse;

Expand All @@ -29,6 +30,8 @@
*/
public interface MemCacheOperation {

public static Charset CHARSET = Charset.forName("UTF-8");

public MemcacheResponse doOperation(MemcacheRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcacheRequest;
import uk.co.c2b2.coherence.memcached.server.binaryprotocol.MemcacheResponse;
Expand Down Expand Up @@ -53,7 +52,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
DataInputStream dis = new DataInputStream(bis);
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray, CHARSET);
byte value[] = new byte[header.getBodyLength() - header.getKeyLength()];
dis.read(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
int expiry = dis.readInt();
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray, CHARSET);
byte value[] = new byte[header.getBodyLength() - header.getExtraLength() - header.getKeyLength()];
dis.read(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MemcacheResponse doOperation(MemcacheRequest request) {
int expiry = dis.readInt();
byte keyArray[] = new byte[header.getKeyLength()];
dis.read(keyArray);
String key = new String(keyArray, Charset.defaultCharset());
String key = new String(keyArray, CHARSET);
byte value[] = new byte[header.getBodyLength() - header.getExtraLength() - header.getKeyLength()];
dis.read(value);

Expand Down

0 comments on commit be23149

Please sign in to comment.