From 79a6ee2c92ebb19788e4d9bfe926307c3e2f6236 Mon Sep 17 00:00:00 2001 From: Thomas Kuehne Date: Sat, 21 Jun 2025 00:00:00 +0200 Subject: [PATCH] use StandardCharsets.UTF_8 instead of name based charset lookups eliminates the need to handle UnsupportedEncodingException and improves performance --- .../devices/huawei/HuaweiCrypto.java | 2 +- .../devices/qhybrid/FossilFileReader.java | 3 +- .../devices/jyou/BFH16DeviceSupport.java | 30 ++++++++----------- .../service/devices/jyou/JYouSupport.java | 30 ++++++++----------- .../devices/pebble/webview/JSInterface.java | 6 ++-- .../notification/PlayNotificationRequest.java | 4 +-- .../datastructures/WithingsStructure.java | 3 +- .../gadgetbridge/util/CryptoUtils.java | 2 +- .../gadgetbridge/util/HttpUtils.java | 5 ++-- 9 files changed, 38 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java index a46d48189a..b201ed086d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java @@ -131,7 +131,7 @@ public class HuaweiCrypto { public byte[] computeDigestHiChainLite(byte[] message, byte[] key, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException { 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(); for (int i = 0; i < digestSecret.length; i++) { digestSecret[i] = (byte) (((0xFF & hashKey[i]) ^ (digestSecret[i] & 0xFF)) & 0xFF); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/qhybrid/FossilFileReader.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/qhybrid/FossilFileReader.java index 937986f1ef..d13b42034e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/qhybrid/FossilFileReader.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/qhybrid/FossilFileReader.java @@ -33,7 +33,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.UUID; @@ -188,7 +187,7 @@ public class FossilFileReader { byte[] filenameBytes = new byte[filenameLength - 1]; buf.get(filenameBytes); buf.get(); - list.add(new String(filenameBytes, Charset.forName("UTF8"))); + list.add(new String(filenameBytes, StandardCharsets.UTF_8)); int filesize = buf.getShort(); if (cutTrailingNull) { filesize -= 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java index 4af2dd2ef4..f3009c258e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java @@ -59,8 +59,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Calendar; import java.util.UUID; @@ -516,26 +516,22 @@ public class BFH16DeviceSupport extends AbstractBTLEDeviceSupport { } private byte[] stringToUTF8Bytes(String src, int byteCount) { - try { - if (src == null) - return null; + if (src == null) + return null; - for (int i = src.length(); i > 0; i--) { - String sub = src.substring(0, i); - byte[] subUTF8 = sub.getBytes("UTF-8"); + for (int i = src.length(); i > 0; i--) { + String sub = src.substring(0, i); + byte[] subUTF8 = sub.getBytes(StandardCharsets.UTF_8); - if (subUTF8.length == byteCount) { - return subUTF8; - } + if (subUTF8.length == byteCount) { + return subUTF8; + } - if (subUTF8.length < byteCount) { - byte[] largerSubUTF8 = new byte[byteCount]; - System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length); - return largerSubUTF8; - } + if (subUTF8.length < byteCount) { + byte[] largerSubUTF8 = new byte[byteCount]; + System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length); + return largerSubUTF8; } - } catch (UnsupportedEncodingException e) { - LOG.warn(e.getMessage()); } return null; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/JYouSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/JYouSupport.java index edd37473b2..1b0d97b157 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/JYouSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/JYouSupport.java @@ -23,8 +23,8 @@ import android.widget.Toast; import org.slf4j.Logger; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Calendar; @@ -304,26 +304,22 @@ public class JYouSupport extends AbstractBTLEDeviceSupport { } private byte[] stringToUTF8Bytes(String src, int byteCount) { - try { - if (src == null) - return null; + if (src == null) + return null; - for (int i = src.length(); i > 0; i--) { - String sub = src.substring(0, i); - byte[] subUTF8 = sub.getBytes("UTF-8"); + for (int i = src.length(); i > 0; i--) { + String sub = src.substring(0, i); + byte[] subUTF8 = sub.getBytes(StandardCharsets.UTF_8); - if (subUTF8.length == byteCount) { - return subUTF8; - } + if (subUTF8.length == byteCount) { + return subUTF8; + } - if (subUTF8.length < byteCount) { - byte[] largerSubUTF8 = new byte[byteCount]; - System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length); - return largerSubUTF8; - } + if (subUTF8.length < byteCount) { + byte[] largerSubUTF8 = new byte[byteCount]; + System.arraycopy(subUTF8, 0, largerSubUTF8, 0, subUTF8.length); + return largerSubUTF8; } - } catch (UnsupportedEncodingException e) { - logger.warn(e.getMessage()); } return null; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java index 295775195b..024436a23b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java @@ -29,8 +29,8 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Iterator; @@ -197,7 +197,7 @@ public class JSInterface { String prefix = device.getAddress() + this.mUuid.toString(); try { 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); bytes = digest.digest(); final StringBuilder sb = new StringBuilder(); @@ -205,7 +205,7 @@ public class JSInterface { sb.append(String.format("%02X", aByte)); } return sb.toString().toLowerCase(); - } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { + } catch (NoSuchAlgorithmException e) { LOG.warn("Error definining local storage prefix", e); return prefix; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java index 7583e1a61b..b947b5c724 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java @@ -20,7 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fo import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; -import java.util.Arrays; +import java.nio.charset.StandardCharsets; import java.util.zip.CRC32; 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 appBundleCRCLength = (byte) 4; - Charset charsetUTF8 = Charset.forName("UTF-8"); + Charset charsetUTF8 = StandardCharsets.UTF_8; String nullTerminatedTitle = StringUtils.terminateNull(title); byte[] titleBytes = nullTerminatedTitle.getBytes(charsetUTF8); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/withingssteelhr/communication/datastructures/WithingsStructure.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/withingssteelhr/communication/datastructures/WithingsStructure.java index 97a46fc34c..041dbf3053 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/withingssteelhr/communication/datastructures/WithingsStructure.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/withingssteelhr/communication/datastructures/WithingsStructure.java @@ -17,7 +17,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.withingssteelhr.communication.datastructures; import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import nodomain.freeyourgadget.gadgetbridge.service.devices.withingssteelhr.communication.message.Message; @@ -75,7 +74,7 @@ public abstract class WithingsStructure { int stringLength = (short)(byteBuffer.get() & 255); byte[] stringBytes = new byte[stringLength]; byteBuffer.get(stringBytes); - return new String(stringBytes, Charset.forName("UTF-8")); + return new String(stringBytes, StandardCharsets.UTF_8); } protected byte[] getNextByteArray(ByteBuffer byteBuffer) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CryptoUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CryptoUtils.java index 6b99dd3b57..fde6c0bdb8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CryptoUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CryptoUtils.java @@ -151,7 +151,7 @@ public class CryptoUtils { public static byte[] pbkdf2Sha256(String key, String iv, int count, int length) throws InvalidKeySpecException, NoSuchAlgorithmException, UnsupportedEncodingException { 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(); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/HttpUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/HttpUtils.java index 4c804f8c0e..540649a512 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/HttpUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/HttpUtils.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import java.net.URL; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -27,9 +28,9 @@ public class HttpUtils { for (final String pair : pairs) { final String[] parts = pair.split("=", 2); try { - final String key = URLDecoder.decode(parts[0], "UTF-8"); + final String key = URLDecoder.decode(parts[0], StandardCharsets.UTF_8); if (parts.length == 2) { - queryParameters.put(key, URLDecoder.decode(parts[1], "UTF-8")); + queryParameters.put(key, URLDecoder.decode(parts[1], StandardCharsets.UTF_8)); } else { queryParameters.put(key, ""); }