1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 03:27:59 +01: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

@ -1,7 +1,7 @@
language: android
android:
components:
- android-16
- android-19
jdk:
- oraclejdk8
- openjdk8

View file

@ -705,15 +705,7 @@ def getGitCommit() {
}
def getAndroidRuntimeJar() {
// We set a different Android API level compared to
// smackMinAndroidSdk here. The runtime jar retrieved via this
// method is, compared to earlier Smack versions, not used to
// check for Android API compatibility. Instead it is used to for
// the eclipse classpath, for javadoc and when compiling the pure
// Android subprojects of Smack. Currently we require level 16
// here, because of the @TargetApi annotation found in
// AndroidUsingLinkProperties of minidns-android21.
def androidApiLevel = 16
def androidApiLevel = ext.smackMinAndroidSdk
def androidHome = getAndroidHome()
def androidJar = new File("$androidHome/platforms/android-${androidApiLevel}/android.jar")
if (androidJar.isFile()) {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2018 Florian Schmaus
* Copyright © 2014-2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,9 +16,8 @@
*/
package org.jivesoftware.smack.util.stringencoder.android;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
import android.util.Base64;
@ -39,21 +38,13 @@ public final class AndroidBase64UrlSafeEncoder implements StringEncoder<String>
@Override
public String encode(String string) {
try {
return Base64.encodeToString(string.getBytes(StringUtils.UTF8), BASE64_ENCODER_FLAGS);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
return Base64.encodeToString(string.getBytes(StandardCharsets.UTF_8), BASE64_ENCODER_FLAGS);
}
@Override
public String decode(String string) {
byte[] bytes = Base64.decode(string, BASE64_ENCODER_FLAGS);
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);
}
}

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);
}

View file

@ -22,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.StringUtils;
@ -53,7 +55,7 @@ public class HashElementTest extends SmackTestSuite {
assertTrue(element.equals(parsed));
HashElement other = new HashElement(HashManager.ALGORITHM.SHA_512,
"861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8".getBytes(StringUtils.UTF8));
"861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8".getBytes(StandardCharsets.UTF_8));
assertFalse(element.equals(other));
}

View file

@ -19,10 +19,10 @@ package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
@ -37,7 +37,6 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.SmackMessageException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
@ -202,13 +201,7 @@ public class Socks5Client {
* @return SOCKS5 connection request message
*/
private byte[] createSocks5ConnectRequest() {
byte[] addr;
try {
addr = digest.getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] addr = digest.getBytes(StandardCharsets.UTF_8);
byte[] data = new byte[7 + addr.length];
data[0] = (byte) 0x05; // version (SOCKS5)

View file

@ -24,6 +24,7 @@ import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
@ -39,7 +40,6 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.StringUtils;
/**
* The Socks5Proxy class represents a local SOCKS5 proxy server. It can be enabled/disabled by
@ -468,7 +468,7 @@ public final class Socks5Proxy {
byte[] connectionRequest = Socks5Utils.receiveSocks5Message(in);
// extract digest
String responseDigest = new String(connectionRequest, 5, connectionRequest[4], StringUtils.UTF8);
String responseDigest = new String(connectionRequest, 5, connectionRequest[4], StandardCharsets.UTF_8);
// return error if digest is not allowed
if (!Socks5Proxy.this.allowedConnections.contains(responseDigest)) {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
* Copyright © 2009 Jonas Ådahl, 2011-2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
*/
package org.jivesoftware.smackx.caps;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
@ -749,13 +749,7 @@ public final class EntityCapsManager extends Manager {
// encoded using Base64 as specified in Section 4 of RFC 4648
// (note: the Base64 output MUST NOT include whitespace and MUST set
// padding bits to zero).
byte[] bytes;
try {
bytes = sb.toString().getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
byte[] digest;
synchronized (md) {
digest = md.digest(bytes);

View file

@ -18,11 +18,12 @@ package org.jivesoftware.smackx.bob;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bob.element.BoBIQ;
@ -55,7 +56,7 @@ public class BoBIQTest extends SmackTestSuite {
BoBIQ bobIQ = PacketParserUtils.parseStanza(sampleBoBIQResponse);
BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1");
BoBData bobData = new BoBData("image/png", "sarasade2354j2".getBytes(StringUtils.UTF8), 86400);
BoBData bobData = new BoBData("image/png", "sarasade2354j2".getBytes(StandardCharsets.UTF_8), 86400);
BoBIQ createdBoBIQ = new BoBIQ(bobHash, bobData);
createdBoBIQ.setStanzaId("sarasa");

View file

@ -20,11 +20,9 @@ import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.InitExtensions;
import com.jamesmurty.utils.XMLBuilder;
@ -71,13 +69,13 @@ public class DataPacketExtensionTest extends InitExtensions {
}
@Test
public void shouldReturnNullIfDataIsInvalid() throws UnsupportedEncodingException {
public void shouldReturnNullIfDataIsInvalid() {
// pad character is not at end of data
DataPacketExtension data = new DataPacketExtension("sessionID", 0, "BBBB=CCC");
assertNull(data.getDecodedData());
// invalid Base64 character
data = new DataPacketExtension("sessionID", 0, new String(new byte[] { 123 }, StringUtils.UTF8));
data = new DataPacketExtension("sessionID", 0, new String(new byte[] { 123 }, StandardCharsets.UTF_8));
assertNull(data.getDecodedData());
}

View file

@ -25,10 +25,10 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.util.NetworkUtil;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
@ -238,7 +238,7 @@ public class Socks5ClientTest {
// reply with full SOCKS5 message with an error code (01 = general SOCKS server
// failure)
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00, (byte) 0x03 });
byte[] address = digest.getBytes(StringUtils.UTF8);
byte[] address = digest.getBytes(StandardCharsets.UTF_8);
out.write(address.length);
out.write(address);
out.write(new byte[] { (byte) 0x00, (byte) 0x00 });
@ -297,7 +297,7 @@ public class Socks5ClientTest {
out.write(new byte[] { (byte) 0x05, (byte) 0x00 });
out.flush();
byte[] address = digest.getBytes(StringUtils.UTF8);
byte[] address = digest.getBytes(StandardCharsets.UTF_8);
assertEquals((byte) 0x05, (byte) in.read()); // version
assertEquals((byte) 0x01, (byte) in.read()); // connect request

View file

@ -29,13 +29,12 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import org.jivesoftware.smack.util.StringUtils;
import org.junit.After;
import org.junit.Test;
@ -281,7 +280,7 @@ public class Socks5ProxyTest {
proxy.start();
assertTrue(proxy.isRunning());
String digest = new String(new byte[] { (byte) 0xAA }, StringUtils.UTF8);
String digest = new String(new byte[] { (byte) 0xAA }, StandardCharsets.UTF_8);
// add digest to allow connection
proxy.addTransfer(digest);

View file

@ -24,13 +24,13 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.util.StringUtils;
/**
* Simple SOCKS5 proxy for testing purposes. It is almost the same as the Socks5Proxy class but the
@ -297,7 +297,7 @@ public final class Socks5TestProxy {
byte[] connectionRequest = Socks5Utils.receiveSocks5Message(in);
// extract digest
String responseDigest = new String(connectionRequest, 5, connectionRequest[4], StringUtils.UTF8);
String responseDigest = new String(connectionRequest, 5, connectionRequest[4], StandardCharsets.UTF_8);
connectionRequest[1] = (byte) 0x00; // set return status to 0 (success)
out.write(connectionRequest);

View file

@ -20,7 +20,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.jivesoftware.smack.SmackException;
@ -48,12 +48,7 @@ public class FileTransferIntegrationTest extends AbstractSmackIntegrationTest {
private static final byte[] dataToSend;
static {
try {
dataToSend = StringUtils.insecureRandomString(1024 * 4 * 5).getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
dataToSend = StringUtils.insecureRandomString(1024 * 4 * 5).getBytes(StandardCharsets.UTF_8);
}
@SmackIntegrationTest

View file

@ -16,10 +16,9 @@
*/
package org.jivesoftware.smack.util.stringencoder.java7;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
@ -52,25 +51,14 @@ public final class Java7Base64UrlSafeEncoder implements StringEncoder<String> {
@Override
public String encode(String s) {
byte[] bytes;
try {
bytes = s.getBytes(StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
return encoder.encodeToString(bytes);
}
@Override
public String decode(String s) {
byte[] bytes = decoder.decode(s);
try {
return new String(bytes, StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return new String(bytes, StandardCharsets.UTF_8);
}
}

View file

@ -23,12 +23,11 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.StringUtils;
/**
* A very Simple HTTP Server.
*/
@ -88,7 +87,7 @@ public class HttpServer {
this.socket = socket;
this.input = socket.getInputStream();
this.output = socket.getOutputStream();
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StringUtils.UTF8));
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
}
@Override
@ -122,16 +121,16 @@ public class HttpServer {
+ entityBody.length() + CRLF;
contentTypeLine = "text/html";
output.write(statusLine.getBytes(StringUtils.UTF8));
output.write(statusLine.getBytes(StandardCharsets.UTF_8));
output.write(serverLine.getBytes(StringUtils.UTF8));
output.write(serverLine.getBytes(StandardCharsets.UTF_8));
output.write(contentTypeLine.getBytes(StringUtils.UTF8));
output.write(contentLengthLine.getBytes(StringUtils.UTF8));
output.write(contentTypeLine.getBytes(StandardCharsets.UTF_8));
output.write(contentLengthLine.getBytes(StandardCharsets.UTF_8));
output.write(CRLF.getBytes(StringUtils.UTF8));
output.write(CRLF.getBytes(StandardCharsets.UTF_8));
output.write(entityBody.getBytes(StringUtils.UTF8));
output.write(entityBody.getBytes(StandardCharsets.UTF_8));
}
}

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smackx.omemo;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
@ -25,8 +25,6 @@ import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.omemo.element.OmemoElement;
import org.jivesoftware.smackx.omemo.element.OmemoKeyElement;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
@ -169,10 +167,9 @@ public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
byte[] encryptedBody = payloadAndAuthTag(element, cipherAndAuthTag.getAuthTag());
try {
String plaintext = new String(cipherAndAuthTag.getCipher().doFinal(encryptedBody), StringUtils.UTF8);
String plaintext = new String(cipherAndAuthTag.getCipher().doFinal(encryptedBody), StandardCharsets.UTF_8);
return plaintext;
} catch (UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new CryptoFailedException("decryptMessageElement could not decipher message body: "
+ e.getMessage());
}

View file

@ -19,7 +19,6 @@ package org.jivesoftware.smackx.omemo;
import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYLENGTH;
import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYTYPE;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -309,7 +308,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
try {
builder = new OmemoMessageBuilder<>(userDevice, gullibleTrustCallback, getOmemoRatchet(manager),
messageKey, iv, null);
} catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | UnsupportedEncodingException | IllegalBlockSizeException e) {
} catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
throw new CryptoFailedException(e);
}
@ -371,7 +370,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
try {
builder = new OmemoMessageBuilder<>(
userDevice, manager.getTrustCallback(), getOmemoRatchet(managerGuard.get()), messageKey, iv, message);
} catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException |
} catch (BadPaddingException | IllegalBlockSizeException |
NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) {
throw new CryptoFailedException(e);
}

View file

@ -21,6 +21,7 @@ import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYLENGTH
import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYTYPE;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -37,8 +38,6 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.omemo.OmemoRatchet;
import org.jivesoftware.smackx.omemo.OmemoService;
import org.jivesoftware.smackx.omemo.element.OmemoElement;
@ -96,7 +95,6 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
* @throws InvalidAlgorithmParameterException
*/
public OmemoMessageBuilder(OmemoDevice userDevice,
@ -107,7 +105,7 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
String message)
throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException,
IllegalBlockSizeException,
UnsupportedEncodingException, InvalidAlgorithmParameterException {
InvalidAlgorithmParameterException {
this.userDevice = userDevice;
this.trustCallback = callback;
this.ratchet = ratchet;
@ -152,11 +150,12 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
* @throws NoSuchProviderException
* @throws InvalidAlgorithmParameterException
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
private void setMessage(String message) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
private void setMessage(String message)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
if (message == null) {
return;
}
@ -170,7 +169,7 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
byte[] body;
byte[] ciphertext;
body = message.getBytes(StringUtils.UTF8);
body = message.getBytes(StandardCharsets.UTF_8);
ciphertext = cipher.doFinal(body);
byte[] clearKeyWithAuthTag = new byte[messageKey.length + 16];

View file

@ -19,12 +19,12 @@ package org.jivesoftware.smackx.omemo;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertTrue;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smack.xml.XmlPullParser;
@ -41,12 +41,12 @@ public class OmemoBundleVAxolotlElementTest extends SmackTestSuite {
@Test
public void serializationTest() throws Exception {
int signedPreKeyId = 420;
String signedPreKeyB64 = Base64.encodeToString("SignedPreKey".getBytes(StringUtils.UTF8));
String signedPreKeySigB64 = Base64.encodeToString("SignedPreKeySignature".getBytes(StringUtils.UTF8));
String identityKeyB64 = Base64.encodeToString("IdentityKey".getBytes(StringUtils.UTF8));
String signedPreKeyB64 = Base64.encodeToString("SignedPreKey".getBytes(StandardCharsets.UTF_8));
String signedPreKeySigB64 = Base64.encodeToString("SignedPreKeySignature".getBytes(StandardCharsets.UTF_8));
String identityKeyB64 = Base64.encodeToString("IdentityKey".getBytes(StandardCharsets.UTF_8));
int preKeyId1 = 220, preKeyId2 = 284;
String preKey1B64 = Base64.encodeToString("FirstPreKey".getBytes(StringUtils.UTF8)),
preKey2B64 = Base64.encodeToString("SecondPreKey".getBytes(StringUtils.UTF8));
String preKey1B64 = Base64.encodeToString("FirstPreKey".getBytes(StandardCharsets.UTF_8)),
preKey2B64 = Base64.encodeToString("SecondPreKey".getBytes(StandardCharsets.UTF_8));
HashMap<Integer, String> preKeysB64 = new HashMap<>();
preKeysB64.put(preKeyId1, preKey1B64);
preKeysB64.put(preKeyId2, preKey2B64);
@ -80,11 +80,11 @@ public class OmemoBundleVAxolotlElementTest extends SmackTestSuite {
String actual = bundle.toXML().toString();
assertEquals("Bundles XML must match.", expected, actual);
byte[] signedPreKey = "SignedPreKey".getBytes(StringUtils.UTF8);
byte[] signedPreKeySig = "SignedPreKeySignature".getBytes(StringUtils.UTF8);
byte[] identityKey = "IdentityKey".getBytes(StringUtils.UTF8);
byte[] firstPreKey = "FirstPreKey".getBytes(StringUtils.UTF8);
byte[] secondPreKey = "SecondPreKey".getBytes(StringUtils.UTF8);
byte[] signedPreKey = "SignedPreKey".getBytes(StandardCharsets.UTF_8);
byte[] signedPreKeySig = "SignedPreKeySignature".getBytes(StandardCharsets.UTF_8);
byte[] identityKey = "IdentityKey".getBytes(StandardCharsets.UTF_8);
byte[] firstPreKey = "FirstPreKey".getBytes(StandardCharsets.UTF_8);
byte[] secondPreKey = "SecondPreKey".getBytes(StandardCharsets.UTF_8);
OmemoBundleElement_VAxolotl parsed = new OmemoBundleVAxolotlProvider().parse(TestUtils.getParser(actual));

View file

@ -18,11 +18,11 @@ package org.jivesoftware.smackx.omemo;
import static org.junit.Assert.assertEquals;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl;
@ -40,11 +40,11 @@ public class OmemoVAxolotlElementTest extends SmackTestSuite {
@Test
public void serializationTest() throws Exception {
byte[] payload = "This is payload.".getBytes(StringUtils.UTF8);
byte[] payload = "This is payload.".getBytes(StandardCharsets.UTF_8);
int keyId1 = 8;
int keyId2 = 33333;
byte[] keyData1 = "KEYDATA".getBytes(StringUtils.UTF8);
byte[] keyData2 = "DATAKEY".getBytes(StringUtils.UTF8);
byte[] keyData1 = "KEYDATA".getBytes(StandardCharsets.UTF_8);
byte[] keyData2 = "DATAKEY".getBytes(StandardCharsets.UTF_8);
int sid = 12131415;
byte[] iv = OmemoMessageBuilder.generateIv();

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smack.sasl.provided;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import javax.security.auth.callback.CallbackHandler;
@ -103,13 +103,7 @@ public class SASLDigestMD5Mechanism extends SASLMechanism {
if (challenge.length == 0) {
throw new SmackSaslException("Initial challenge has zero length");
}
String challengeString;
try {
challengeString = new String(challenge, StringUtils.UTF8);
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
String challengeString = new String(challenge, StandardCharsets.UTF_8);
String[] challengeParts = challengeString.split(",");
byte[] response = null;
switch (state) {