1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 02:39:42 +02:00

Use StandardCharsets.(UTF_8|US_ASCII)

This also gets rid of a ton of UnsupportedEncodingException s.
This commit is contained in:
Florian Schmaus 2019-05-08 11:34:40 +02:00
parent 2a4d110b22
commit d2f5efcb20
33 changed files with 125 additions and 227 deletions

View file

@ -23,6 +23,7 @@ import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
@ -2044,7 +2045,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
try {
Constructor<?> c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class);
String pkcs11Config = "name = SmartCard\nlibrary = " + config.getPKCS11Library();
ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes(StringUtils.UTF8));
ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes(StandardCharsets.UTF_8));
Provider p = (Provider) c.newInstance(config);
Security.addProvider(p);
ks = KeyStore.getInstance("PKCS11",p);

View file

@ -20,6 +20,7 @@ package org.jivesoftware.smack;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
@ -45,7 +46,6 @@ import org.jivesoftware.smack.sasl.core.ScramSha1PlusMechanism;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
@ -69,7 +69,7 @@ public final class SmackInitialization {
String smackVersion;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8));
reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StandardCharsets.UTF_8));
smackVersion = reader.readLine();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Could not determine Smack version", e);

View file

@ -22,8 +22,7 @@ import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.jivesoftware.smack.util.StringUtils;
import java.nio.charset.StandardCharsets;
/**
* Socket factory for socks4 proxy.
@ -86,7 +85,7 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
}
if (user != null) {
byte[] userBytes = user.getBytes(StringUtils.UTF8);
byte[] userBytes = user.getBytes(StandardCharsets.UTF_8);
System.arraycopy(userBytes, 0, buf, index, user.length());
index += user.length();
}

View file

@ -21,10 +21,10 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.StringUtils;
/**
* Socket factory for Socks5 proxy.
@ -133,11 +133,11 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
index = 0;
buf[index++] = 1;
buf[index++] = (byte) user.length();
byte[] userBytes = user.getBytes(StringUtils.UTF8);
byte[] userBytes = user.getBytes(StandardCharsets.UTF_8);
System.arraycopy(userBytes, 0, buf, index,
user.length());
index += user.length();
byte[] passwordBytes = passwd.getBytes(StringUtils.UTF8);
byte[] passwordBytes = passwd.getBytes(StandardCharsets.UTF_8);
buf[index++] = (byte) passwordBytes.length;
System.arraycopy(passwordBytes, 0, buf, index,
passwd.length());
@ -204,7 +204,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
buf[index++] = 1; // CONNECT
buf[index++] = 0;
byte[] hostb = host.getBytes(StringUtils.UTF8);
byte[] hostb = host.getBytes(StandardCharsets.UTF_8);
int len = hostb.length;
buf[index++] = 3; // DOMAINNAME
buf[index++] = (byte) len;

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smack.sasl.core;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.Collections;
@ -114,14 +114,8 @@ public abstract class ScramMechanism extends SASLMechanism {
@Override
protected byte[] evaluateChallenge(byte[] challenge) throws SmackSaslException {
String challengeString;
try {
// TODO: Where is it specified that this is an UTF-8 encoded string?
challengeString = new String(challenge, StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
// TODO: Where is it specified that this is an UTF-8 encoded string?
String challengeString = new String(challenge, StandardCharsets.UTF_8);
switch (state) {
case AUTH_TEXT_SENT:
@ -386,14 +380,9 @@ public abstract class ScramMechanism extends SASLMechanism {
* @throws SmackSaslException if a SASL related error occurs.
*/
private byte[] hi(String normalizedPassword, byte[] salt, int iterations) throws SmackSaslException {
byte[] key;
try {
// According to RFC 5802 § 2.2, the resulting string of the normalization is also in UTF-8.
key = normalizedPassword.getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError();
}
// According to RFC 5802 § 2.2, the resulting string of the normalization is also in UTF-8.
byte[] key = normalizedPassword.getBytes(StandardCharsets.UTF_8);
// U1 := HMAC(str, salt + INT(1))
byte[] u = hmac(key, ByteUtils.concat(salt, ONE));
byte[] res = u.clone();

View file

@ -30,6 +30,7 @@ import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -90,7 +91,7 @@ public final class FileUtils {
public static boolean addLines(String uriString, Set<String> set) throws MalformedURLException, IOException {
URI uri = URI.create(uriString);
InputStream is = getStreamForUri(uri, null);
InputStreamReader sr = new InputStreamReader(is, StringUtils.UTF8);
InputStreamReader sr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(sr);
try {
String line;

View file

@ -21,7 +21,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -71,12 +71,7 @@ public class PacketParserUtils {
}
public static XmlPullParser getParserFor(InputStream inputStream) throws XmlPullParserException {
InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(inputStream, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
return SmackXmlParser.newXmlParser(inputStreamReader);
}

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smack.util;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
@ -29,7 +29,23 @@ public class StringUtils {
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA-1";
/**
* Deprecated, do not use.
*
* @deprecated use StandardCharsets.UTF_8 instead.
*/
// TODO: Remove in Smack 4.5.
@Deprecated
public static final String UTF8 = "UTF-8";
/**
* Deprecated, do not use.
*
* @deprecated use StandardCharsets.US_ASCII instead.
*/
// TODO: Remove in Smack 4.5.
@Deprecated
public static final String USASCII = "US-ASCII";
public static final String QUOTE_ENCODE = "&quot;";
@ -244,12 +260,7 @@ public class StringUtils {
}
public static byte[] toUtf8Bytes(String string) {
try {
return string.getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 encoding not supported by platform", e);
}
return string.getBytes(StandardCharsets.UTF_8);
}
/**

View file

@ -19,9 +19,7 @@ package org.jivesoftware.smack.util.stringencoder;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.StringUtils;
import java.nio.charset.StandardCharsets;
/**
* Base32 string encoding is useful for when filenames case-insensitive filesystems are encoded.
@ -55,13 +53,8 @@ public class Base32 {
public static String decode(String str) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte[] raw;
try {
raw = str.getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] raw = str.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < raw.length; i++) {
char c = (char) raw[i];
if (!Character.isWhitespace(c)) {
@ -114,24 +107,12 @@ public class Base32 {
}
}
String res;
try {
res = new String(bs.toByteArray(), StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
String res = new String(bs.toByteArray(), StandardCharsets.UTF_8);
return res;
}
public static String encode(String str) {
byte[] b;
try {
b = str.getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] b = str.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (int i = 0; i < (b.length + 4) / 5; i++) {
@ -174,13 +155,7 @@ public class Base32 {
os.write(c);
}
}
String res;
try {
res = new String(os.toByteArray(), StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
String res = new String(os.toByteArray(), StandardCharsets.UTF_8);
return res;
}

View file

@ -16,10 +16,9 @@
*/
package org.jivesoftware.smack.util.stringencoder;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
public class Base64 {
@ -31,11 +30,7 @@ public class Base64 {
}
public static final String encode(String string) {
try {
return encodeToString(string.getBytes(StringUtils.UTF8));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
return encodeToString(string.getBytes(StandardCharsets.UTF_8));
}
public static final String encodeToString(byte[] input) {
@ -56,11 +51,7 @@ public class Base64 {
public static final String decodeToString(String string) {
byte[] bytes = decode(string);
try {
return new String(bytes, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
return new String(bytes, StandardCharsets.UTF_8);
}
// TODO: We really should not mask the IllegalArgumentException. But some unit test depend on this behavior, like
@ -74,12 +65,7 @@ public class Base64 {
}
public static final byte[] decode(byte[] input) {
String string;
try {
string = new String(input, StringUtils.USASCII);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
String string = new String(input, StandardCharsets.US_ASCII);
return decode(string);
}

View file

@ -19,7 +19,7 @@ package org.jivesoftware.smack.sasl;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@ -39,14 +39,14 @@ public class DigestMd5SaslTest extends AbstractSaslTest {
super(saslMechanism);
}
protected void runTest(boolean useAuthzid) throws SmackException, InterruptedException, XmppStringprepException, UnsupportedEncodingException {
protected void runTest(boolean useAuthzid) throws SmackException, InterruptedException, XmppStringprepException {
EntityBareJid authzid = null;
if (useAuthzid) {
authzid = JidCreate.entityBareFrom("shazbat@xmpp.org");
}
saslMechanism.authenticate("florian", "irrelevant", JidCreate.domainBareFrom("xmpp.org"), "secret", authzid, null);
byte[] response = saslMechanism.evaluateChallenge(challengeBytes);
String responseString = new String(response, StringUtils.UTF8);
String responseString = new String(response, StandardCharsets.UTF_8);
String[] responseParts = responseString.split(",");
Map<String, String> responsePairs = new HashMap<String, String>();
for (String part : responseParts) {

View file

@ -21,7 +21,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
@ -72,15 +72,15 @@ public class StringUtilsTest {
}
@Test
public void testEncodeHex() throws UnsupportedEncodingException {
public void testEncodeHex() {
String input = "";
String output = "";
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StringUtils.UTF8))),
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StandardCharsets.UTF_8))),
output);
input = "foo bar 123";
output = "666f6f2062617220313233";
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StringUtils.UTF8))),
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StandardCharsets.UTF_8))),
output);
}