mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
use StandardCharsets.UTF_8 instead of name based charset lookups
eliminates the need to handle UnsupportedEncodingException and improves performance
This commit is contained in:
+1
-1
@@ -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);
|
||||
|
||||
+1
-2
@@ -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;
|
||||
|
||||
+13
-17
@@ -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;
|
||||
}
|
||||
|
||||
+13
-17
@@ -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;
|
||||
}
|
||||
|
||||
+3
-3
@@ -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;
|
||||
}
|
||||
|
||||
+2
-2
@@ -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);
|
||||
|
||||
+1
-2
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user