use StandardCharsets.UTF_8 instead of name based charset lookups

eliminates the need to handle UnsupportedEncodingException and improves performance
This commit is contained in:
Thomas Kuehne
2025-06-21 20:51:22 +02:00
parent 5c439be493
commit 79a6ee2c92
9 changed files with 38 additions and 47 deletions
@@ -131,7 +131,7 @@ public class HuaweiCrypto {
public byte[] computeDigestHiChainLite(byte[] message, byte[] key, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException { public byte[] computeDigestHiChainLite(byte[] message, byte[] key, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException {
byte[] digestStep1; byte[] digestStep1;
byte[] hashKey = CryptoUtils.digest(GB.hexdump(key).getBytes("UTF-8")); byte[] hashKey = CryptoUtils.digest(GB.hexdump(key).getBytes(StandardCharsets.UTF_8));
byte[] digestSecret = getDigestSecret(); byte[] digestSecret = getDigestSecret();
for (int i = 0; i < digestSecret.length; i++) { for (int i = 0; i < digestSecret.length; i++) {
digestSecret[i] = (byte) (((0xFF & hashKey[i]) ^ (digestSecret[i] & 0xFF)) & 0xFF); digestSecret[i] = (byte) (((0xFF & hashKey[i]) ^ (digestSecret[i] & 0xFF)) & 0xFF);
@@ -33,7 +33,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
@@ -188,7 +187,7 @@ public class FossilFileReader {
byte[] filenameBytes = new byte[filenameLength - 1]; byte[] filenameBytes = new byte[filenameLength - 1];
buf.get(filenameBytes); buf.get(filenameBytes);
buf.get(); buf.get();
list.add(new String(filenameBytes, Charset.forName("UTF8"))); list.add(new String(filenameBytes, StandardCharsets.UTF_8));
int filesize = buf.getShort(); int filesize = buf.getShort();
if (cutTrailingNull) { if (cutTrailingNull) {
filesize -= 1; filesize -= 1;
@@ -59,8 +59,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.UUID; import java.util.UUID;
@@ -516,26 +516,22 @@ public class BFH16DeviceSupport extends AbstractBTLEDeviceSupport {
} }
private byte[] stringToUTF8Bytes(String src, int byteCount) { private byte[] stringToUTF8Bytes(String src, int byteCount) {
try { if (src == null)
if (src == null) return null;
return null;
for (int i = src.length(); i > 0; i--) { for (int i = src.length(); i > 0; i--) {
String sub = src.substring(0, i); String sub = src.substring(0, i);
byte[] subUTF8 = sub.getBytes("UTF-8"); byte[] subUTF8 = sub.getBytes(StandardCharsets.UTF_8);
if (subUTF8.length == byteCount) { if (subUTF8.length == byteCount) {
return subUTF8; return subUTF8;
} }
if (subUTF8.length < byteCount) { if (subUTF8.length < byteCount) {
byte[] largerSubUTF8 = new byte[byteCount]; byte[] largerSubUTF8 = new byte[byteCount];
System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length); System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length);
return largerSubUTF8; return largerSubUTF8;
}
} }
} catch (UnsupportedEncodingException e) {
LOG.warn(e.getMessage());
} }
return null; return null;
} }
@@ -23,8 +23,8 @@ import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@@ -304,26 +304,22 @@ public class JYouSupport extends AbstractBTLEDeviceSupport {
} }
private byte[] stringToUTF8Bytes(String src, int byteCount) { private byte[] stringToUTF8Bytes(String src, int byteCount) {
try { if (src == null)
if (src == null) return null;
return null;
for (int i = src.length(); i > 0; i--) { for (int i = src.length(); i > 0; i--) {
String sub = src.substring(0, i); String sub = src.substring(0, i);
byte[] subUTF8 = sub.getBytes("UTF-8"); byte[] subUTF8 = sub.getBytes(StandardCharsets.UTF_8);
if (subUTF8.length == byteCount) { if (subUTF8.length == byteCount) {
return subUTF8; return subUTF8;
} }
if (subUTF8.length < byteCount) { if (subUTF8.length < byteCount) {
byte[] largerSubUTF8 = new byte[byteCount]; byte[] largerSubUTF8 = new byte[byteCount];
System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length); System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length);
return largerSubUTF8; return largerSubUTF8;
}
} }
} catch (UnsupportedEncodingException e) {
logger.warn(e.getMessage());
} }
return null; return null;
} }
@@ -29,8 +29,8 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Iterator; import java.util.Iterator;
@@ -197,7 +197,7 @@ public class JSInterface {
String prefix = device.getAddress() + this.mUuid.toString(); String prefix = device.getAddress() + this.mUuid.toString();
try { try {
MessageDigest digest = MessageDigest.getInstance("SHA-1"); MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] bytes = prefix.getBytes("UTF-8"); byte[] bytes = prefix.getBytes(StandardCharsets.UTF_8);
digest.update(bytes, 0, bytes.length); digest.update(bytes, 0, bytes.length);
bytes = digest.digest(); bytes = digest.digest();
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
@@ -205,7 +205,7 @@ public class JSInterface {
sb.append(String.format("%02X", aByte)); sb.append(String.format("%02X", aByte));
} }
return sb.toString().toLowerCase(); return sb.toString().toLowerCase();
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { } catch (NoSuchAlgorithmException e) {
LOG.warn("Error definining local storage prefix", e); LOG.warn("Error definining local storage prefix", e);
return prefix; return prefix;
} }
@@ -20,7 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fo
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
@@ -56,7 +56,7 @@ public abstract class PlayNotificationRequest extends FilePutRequest {
byte uidLength = (byte) 4; byte uidLength = (byte) 4;
byte appBundleCRCLength = (byte) 4; byte appBundleCRCLength = (byte) 4;
Charset charsetUTF8 = Charset.forName("UTF-8"); Charset charsetUTF8 = StandardCharsets.UTF_8;
String nullTerminatedTitle = StringUtils.terminateNull(title); String nullTerminatedTitle = StringUtils.terminateNull(title);
byte[] titleBytes = nullTerminatedTitle.getBytes(charsetUTF8); byte[] titleBytes = nullTerminatedTitle.getBytes(charsetUTF8);
@@ -17,7 +17,6 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.withingssteelhr.communication.datastructures; package nodomain.freeyourgadget.gadgetbridge.service.devices.withingssteelhr.communication.datastructures;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import nodomain.freeyourgadget.gadgetbridge.service.devices.withingssteelhr.communication.message.Message; import nodomain.freeyourgadget.gadgetbridge.service.devices.withingssteelhr.communication.message.Message;
@@ -75,7 +74,7 @@ public abstract class WithingsStructure {
int stringLength = (short)(byteBuffer.get() & 255); int stringLength = (short)(byteBuffer.get() & 255);
byte[] stringBytes = new byte[stringLength]; byte[] stringBytes = new byte[stringLength];
byteBuffer.get(stringBytes); byteBuffer.get(stringBytes);
return new String(stringBytes, Charset.forName("UTF-8")); return new String(stringBytes, StandardCharsets.UTF_8);
} }
protected byte[] getNextByteArray(ByteBuffer byteBuffer) { protected byte[] getNextByteArray(ByteBuffer byteBuffer) {
@@ -151,7 +151,7 @@ public class CryptoUtils {
public static byte[] pbkdf2Sha256(String key, String iv, int count, int length) throws InvalidKeySpecException, NoSuchAlgorithmException, UnsupportedEncodingException { public static byte[] pbkdf2Sha256(String key, String iv, int count, int length) throws InvalidKeySpecException, NoSuchAlgorithmException, UnsupportedEncodingException {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray(), iv.getBytes("utf-8"), count, length); PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray(), iv.getBytes(StandardCharsets.UTF_8), count, length);
return secretKeyFactory.generateSecret(keySpec).getEncoded(); return secretKeyFactory.generateSecret(keySpec).getEncoded();
} }
} }
@@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@@ -27,9 +28,9 @@ public class HttpUtils {
for (final String pair : pairs) { for (final String pair : pairs) {
final String[] parts = pair.split("=", 2); final String[] parts = pair.split("=", 2);
try { try {
final String key = URLDecoder.decode(parts[0], "UTF-8"); final String key = URLDecoder.decode(parts[0], StandardCharsets.UTF_8);
if (parts.length == 2) { if (parts.length == 2) {
queryParameters.put(key, URLDecoder.decode(parts[1], "UTF-8")); queryParameters.put(key, URLDecoder.decode(parts[1], StandardCharsets.UTF_8));
} else { } else {
queryParameters.put(key, ""); queryParameters.put(key, "");
} }