ARTEMIS-2320 Standard Charset object can be used

This commit is contained in:
Jiri Danek 2019-04-26 23:02:49 +02:00 committed by Michael Andre Pearce
parent 781e4c460d
commit f508a25566
4 changed files with 12 additions and 24 deletions

View File

@ -17,7 +17,7 @@
package org.apache.activemq.artemis.core.protocol.mqtt;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
@ -268,15 +268,11 @@ public class MQTTPublishManager {
ByteBuf payload;
switch (message.getType()) {
case Message.TEXT_TYPE:
try {
SimpleString text = message.getDataBuffer().readNullableSimpleString();
byte[] stringPayload = text.toString().getBytes("UTF-8");
payload = ByteBufAllocator.DEFAULT.buffer(stringPayload.length);
payload.writeBytes(stringPayload);
break;
} catch (UnsupportedEncodingException e) {
log.warn("Unable to send message: " + message.getMessageID() + " Cause: " + e.getMessage(), e);
}
SimpleString text = message.getDataBuffer().readNullableSimpleString();
byte[] stringPayload = text.toString().getBytes(StandardCharsets.UTF_8);
payload = ByteBufAllocator.DEFAULT.buffer(stringPayload.length);
payload.writeBytes(stringPayload);
break;
default:
ActiveMQBuffer bodyBuffer = message.getDataBuffer();
payload = ByteBufAllocator.DEFAULT.buffer(bodyBuffer.writerIndex());

View File

@ -16,7 +16,7 @@
*/
package org.apache.activemq.transport.amqp.client;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
@ -58,12 +58,7 @@ public final class AmqpTransferTagGenerator {
rc = iterator.next();
iterator.remove();
} else {
try {
rc = Long.toHexString(nextTagId++).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// This should never happen since we control the input.
throw new RuntimeException(e);
}
rc = Long.toHexString(nextTagId++).getBytes(StandardCharsets.UTF_8);
}
return rc;
}

View File

@ -19,7 +19,7 @@ package org.apache.activemq.transport.amqp.client.sasl;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.security.sasl.SaslException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -30,7 +30,6 @@ import java.security.NoSuchAlgorithmException;
*/
public class CramMD5Mechanism extends AbstractMechanism {
private static final String ASCII = "ASCII";
private static final String HMACMD5 = "HMACMD5";
private boolean sentResponse;
@ -53,7 +52,7 @@ public class CramMD5Mechanism extends AbstractMechanism {
public byte[] getChallengeResponse(byte[] challenge) throws SaslException {
if (!sentResponse && challenge != null && challenge.length != 0) {
try {
SecretKeySpec key = new SecretKeySpec(getPassword().getBytes(ASCII), HMACMD5);
SecretKeySpec key = new SecretKeySpec(getPassword().getBytes(StandardCharsets.US_ASCII), HMACMD5);
Mac mac = Mac.getInstance(HMACMD5);
mac.init(key);
@ -70,9 +69,7 @@ public class CramMD5Mechanism extends AbstractMechanism {
}
sentResponse = true;
return hash.toString().getBytes(ASCII);
} catch (UnsupportedEncodingException e) {
throw new SaslException("Unable to utilise required encoding", e);
return hash.toString().getBytes(StandardCharsets.US_ASCII);
} catch (InvalidKeyException e) {
throw new SaslException("Unable to utilise key", e);
} catch (NoSuchAlgorithmException e) {

View File

@ -261,7 +261,7 @@ public class StompTest extends StompTestBase {
byte[] mqttPayload = clientProvider.receive(10000);
clientProvider.disconnect();
assertEquals(stompPayload, new String(mqttPayload, "UTF-8"));
assertEquals(stompPayload, new String(mqttPayload, StandardCharsets.UTF_8));
clientProvider.disconnect();
}