Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classes and methods that rely on the default system encoding should not be used #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -943,7 +944,7 @@ static String dbgBuffer(ByteBuffer b, int size) {
* @param retryMessage the body of the retry message.
*/
protected void handleRetryInformation(final byte[] retryMessage) {
getLogger().debug("Got RETRY message: " + new String(retryMessage)
getLogger().debug("Got RETRY message: " + new String(retryMessage, Charset.forName("UTF-8"))
+ ", but not handled.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.spy.memcached.protocol.ascii;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -49,7 +50,7 @@ abstract class BaseGetOpImpl extends OperationImpl {
"NOT_FOUND", StatusCode.ERR_NOT_FOUND);
private static final OperationStatus LOCK_ERROR = new OperationStatus(false,
"LOCK_ERROR", StatusCode.ERR_TEMP_FAIL);
private static final byte[] RN_BYTES = "\r\n".getBytes();
private static final byte[] RN_BYTES = "\r\n".getBytes(Charset.forName("UTF-8"));
private final String cmd;
private final Collection<String> keys;
private String currentKey = null;
Expand Down Expand Up @@ -200,12 +201,12 @@ public final void initialize() {
size += k.length;
size++;
}
byte[] e = String.valueOf(exp).getBytes();
byte[] e = String.valueOf(exp).getBytes(Charset.forName("UTF-8"));
if (hasExp) {
size += e.length + 1;
}
ByteBuffer b = ByteBuffer.allocate(size);
b.put(cmd.getBytes());
b.put(cmd.getBytes(Charset.forName("UTF-8")));
for (byte[] k : keyBytes) {
b.put((byte) ' ');
b.put(k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.spy.memcached.protocol.ascii;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import net.spy.memcached.ops.FlushOperation;
import net.spy.memcached.ops.OperationCallback;
Expand All @@ -35,7 +36,7 @@
*/
final class FlushOperationImpl extends OperationImpl implements FlushOperation {

private static final byte[] FLUSH = "flush_all\r\n".getBytes();
private static final byte[] FLUSH = "flush_all\r\n".getBytes(Charset.forName("UTF-8"));

private static final OperationStatus OK = new OperationStatus(true, "OK",
StatusCode.SUCCESS);
Expand All @@ -61,7 +62,7 @@ public void initialize() {
b = ByteBuffer.wrap(FLUSH);
} else {
b = ByteBuffer.allocate(32);
b.put(("flush_all " + delay + "\r\n").getBytes());
b.put(("flush_all " + delay + "\r\n").getBytes(Charset.forName("UTF-8")));
b.flip();
}
setBuffer(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import net.spy.memcached.KeyUtil;
import net.spy.memcached.ops.Operation;
Expand Down Expand Up @@ -148,7 +149,7 @@ public void readFromBuffer(ByteBuffer data) throws IOException {
byteBuffer.reset();
OperationErrorType eType = classifyError(line);
if (eType != null) {
errorMsg = line.getBytes();
errorMsg = line.getBytes(Charset.forName("UTF-8"));
handleError(eType, line);
} else {
handleLine(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.spy.memcached.protocol.ascii;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

import net.spy.memcached.ops.OperationState;
Expand All @@ -38,7 +39,7 @@ final class StatsOperationImpl extends OperationImpl implements StatsOperation {
private static final OperationStatus END = new OperationStatus(true, "END",
StatusCode.SUCCESS);

private static final byte[] MSG = "stats\r\n".getBytes();
private static final byte[] MSG = "stats\r\n".getBytes(Charset.forName("UTF-8"));

private final byte[] msg;
private final StatsOperation.Callback cb;
Expand All @@ -49,7 +50,7 @@ public StatsOperationImpl(String arg, StatsOperation.Callback c) {
if (arg == null) {
msg = MSG;
} else {
msg = ("stats " + arg + "\r\n").getBytes();
msg = ("stats " + arg + "\r\n").getBytes(Charset.forName("UTF-8"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.spy.memcached.protocol.ascii;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;

Expand Down Expand Up @@ -72,7 +73,7 @@ public void initialize() {
ByteBuffer b = null;
b = ByteBuffer.allocate(KeyUtil.getKeyBytes(key).length
+ String.valueOf(exp).length() + OVERHEAD);
b.put(("touch " + key + " " + exp + "\r\n").getBytes());
b.put(("touch " + key + " " + exp + "\r\n").getBytes(Charset.forName("UTF-8")));
b.flip();
setBuffer(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.spy.memcached.protocol.ascii;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import net.spy.memcached.ops.NoopOperation;
import net.spy.memcached.ops.OperationCallback;
Expand All @@ -37,7 +38,7 @@
final class VersionOperationImpl extends OperationImpl implements
VersionOperation, NoopOperation {

private static final byte[] REQUEST = "version\r\n".getBytes();
private static final byte[] REQUEST = "version\r\n".getBytes(Charset.forName("UTF-8"));

public VersionOperationImpl(OperationCallback c) {
super(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -137,7 +138,7 @@ protected void finishedPayload(byte[] pl) throws IOException {
retryKeys.add(keys.get(responseOpaque));
} else if (errorCode != SUCCESS) {
getLogger().warn("Error on key %s: %s (%d)", keys.get(responseOpaque),
new String(pl), errorCode);
new String(pl, Charset.forName("UTF-8")), errorCode);
} else {
final int flags = decodeInt(pl, 0);
final byte[] data = new byte[pl.length - EXTRA_HDR_LEN];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicInteger;

import net.spy.memcached.CASResponse;
Expand Down Expand Up @@ -201,7 +202,7 @@ protected void finishedPayload(byte[] pl) throws IOException {
OperationStatus status = getStatusForErrorCode(errorCode, pl);

if (status == null) {
handleError(OperationErrorType.SERVER, new String(pl));
handleError(OperationErrorType.SERVER, new String(pl, Charset.forName("UTF-8")));
} else if (errorCode == SUCCESS) {
decodePayload(pl);
transitionState(OperationState.COMPLETE);
Expand Down Expand Up @@ -231,17 +232,17 @@ protected OperationStatus getStatusForErrorCode(int errCode, byte[] errPl)

switch (errCode) {
case ERR_NOT_FOUND:
return new CASOperationStatus(false, new String(errPl),
return new CASOperationStatus(false, new String(errPl, Charset.forName("UTF-8")),
CASResponse.NOT_FOUND, statusCode);
case ERR_EXISTS:
return new CASOperationStatus(false, new String(errPl),
return new CASOperationStatus(false, new String(errPl, Charset.forName("UTF-8")),
CASResponse.EXISTS, statusCode);
case ERR_NOT_STORED:
return new CASOperationStatus(false, new String(errPl),
return new CASOperationStatus(false, new String(errPl, Charset.forName("UTF-8")),
CASResponse.NOT_FOUND, statusCode);
case ERR_2BIG:
case ERR_INTERNAL:
handleError(OperationErrorType.SERVER, new String(errPl));
handleError(OperationErrorType.SERVER, new String(errPl, Charset.forName("UTF-8")));
case ERR_INVAL:
case ERR_DELTA_BADVAL:
case ERR_NOT_MY_VBUCKET:
Expand All @@ -250,7 +251,7 @@ protected OperationStatus getStatusForErrorCode(int errCode, byte[] errPl)
case ERR_NOT_SUPPORTED:
case ERR_BUSY:
case ERR_TEMP_FAIL:
return new OperationStatus(false, new String(errPl), statusCode);
return new OperationStatus(false, new String(errPl, Charset.forName("UTF-8")), statusCode);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.spy.memcached.protocol.binary;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

import javax.security.auth.callback.CallbackHandler;
Expand Down Expand Up @@ -81,13 +82,13 @@ public void initialize() {

@Override
protected void decodePayload(byte[] pl) {
getLogger().debug("Auth response: %s", new String(pl));
getLogger().debug("Auth response: %s", new String(pl, Charset.forName("UTF-8")));
}

@Override
protected void finishedPayload(byte[] pl) throws IOException {
if (errorCode == SASL_CONTINUE) {
getCallback().receivedStatus(new OperationStatus(true, new String(pl),
getCallback().receivedStatus(new OperationStatus(true, new String(pl, Charset.forName("UTF-8")),
StatusCode.SUCCESS));
transitionState(OperationState.COMPLETE);
} else if (errorCode == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package net.spy.memcached.protocol.binary;

import java.nio.charset.Charset;

import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationStatus;
import net.spy.memcached.ops.SASLMechsOperation;
Expand All @@ -43,7 +45,7 @@ public void initialize() {

@Override
protected void decodePayload(byte[] pl) {
getCallback().receivedStatus(new OperationStatus(true, new String(pl),
getCallback().receivedStatus(new OperationStatus(true, new String(pl, Charset.forName("UTF-8")),
StatusCode.SUCCESS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

package net.spy.memcached.protocol.binary;

import java.nio.charset.Charset;

import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationStatus;
import net.spy.memcached.ops.StatusCode;
Expand All @@ -43,7 +45,7 @@ public void initialize() {

@Override
protected void decodePayload(byte[] pl) {
getCallback().receivedStatus(new OperationStatus(true, new String(pl),
getCallback().receivedStatus(new OperationStatus(true, new String(pl, Charset.forName("UTF-8")),
StatusCode.SUCCESS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.spy.memcached.tapmessage;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -167,7 +168,7 @@ public ByteBuffer getBytes() {
}
bb.putInt(flag);
}
bb.put(name.getBytes());
bb.put(name.getBytes(Charset.forName("UTF-8")));
if (hasBackfill) {
bb.putLong(backfilldate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.spy.memcached.tapmessage;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import net.spy.memcached.CachedData;
Expand Down Expand Up @@ -246,7 +247,7 @@ public long getItemExpiry() {
* @return The key data.
*/
public String getKey() {
return new String(key);
return new String(key, Charset.forName("UTF-8"));
}

/**
Expand Down