Skip to content

Commit fe9e454

Browse files
committed
StandardCharsets can be used
Reports methods and constructors where constant charset String literal is used (like "UTF-8") which could be replaced with a predefined Charset object like StandardCharsets.UTF_8. This may work a little bit faster, because charset lookup becomes unnecessary. Also catching UnsupportedEncodingException may become unnecessary as well. In this case the catch block will be removed automatically.
1 parent fb39d9c commit fe9e454

3 files changed

Lines changed: 6 additions & 18 deletions

File tree

crypto/src/main/java/org/springframework/security/crypto/bcrypt/BCrypt.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,7 @@ private byte[] crypt_raw(byte password[], byte salt[], int log_rounds,
733733
public static String hashpw(String password, String salt) {
734734
byte passwordb[];
735735

736-
try {
737-
passwordb = password.getBytes("UTF-8");
738-
} catch (UnsupportedEncodingException uee) {
739-
throw new AssertionError("UTF-8 is not supported");
740-
}
736+
passwordb = password.getBytes(StandardCharsets.UTF_8);
741737

742738
return hashpw(passwordb, salt);
743739
}

crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.CharBuffer;
2020
import java.nio.charset.CharacterCodingException;
2121
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2223

2324
/**
2425
* UTF-8 Charset encoder/decoder.
@@ -28,7 +29,7 @@
2829
* @author Luke Taylor
2930
*/
3031
public final class Utf8 {
31-
private static final Charset CHARSET = Charset.forName("UTF-8");
32+
private static final Charset CHARSET = StandardCharsets.UTF_8;
3233

3334
/**
3435
* Get the bytes of the String in UTF-8 encoded form.

test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.io.UnsupportedEncodingException;
21+
import java.nio.charset.StandardCharsets;
2122
import java.security.cert.CertificateException;
2223
import java.security.cert.CertificateFactory;
2324
import java.security.cert.X509Certificate;
@@ -638,12 +639,7 @@ static String encodePasswordInA1Format(String username, String realm,
638639
}
639640

640641
private static String md5Hex(String a2) {
641-
try {
642-
return DigestUtils.md5DigestAsHex(a2.getBytes("UTF-8"));
643-
}
644-
catch (UnsupportedEncodingException e) {
645-
throw new RuntimeException(e);
646-
}
642+
return DigestUtils.md5DigestAsHex(a2.getBytes(StandardCharsets.UTF_8));
647643
}
648644
}
649645

@@ -985,12 +981,7 @@ private static class HttpBasicRequestPostProcessor implements RequestPostProcess
985981

986982
private HttpBasicRequestPostProcessor(String username, String password) {
987983
byte[] toEncode;
988-
try {
989-
toEncode = (username + ":" + password).getBytes("UTF-8");
990-
}
991-
catch (UnsupportedEncodingException e) {
992-
throw new RuntimeException(e);
993-
}
984+
toEncode = (username + ":" + password).getBytes(StandardCharsets.UTF_8);
994985
this.headerValue = "Basic " + new String(Base64.getEncoder().encode(toEncode));
995986
}
996987

0 commit comments

Comments
 (0)