Skip to content

Commit c52ff80

Browse files
committed
Prefer StandardCharsets.UTF_8 to Charset.forName("UTF-8")
1 parent 0984c28 commit c52ff80

File tree

9 files changed

+23
-26
lines changed

9 files changed

+23
-26
lines changed

bson/src/main/org/bson/io/ByteBufferBsonInput.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.bson.types.ObjectId;
2222

2323
import java.nio.ByteOrder;
24-
import java.nio.charset.Charset;
24+
import java.nio.charset.StandardCharsets;
2525

2626
import static java.lang.String.format;
2727

@@ -31,7 +31,6 @@
3131
* @since 3.0
3232
*/
3333
public class ByteBufferBsonInput implements BsonInput {
34-
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
3534

3635
private static final String[] ONE_BYTE_ASCII_STRINGS = new String[Byte.MAX_VALUE + 1];
3736

@@ -143,7 +142,7 @@ private String readString(final int size) {
143142
throw new BsonSerializationException("Found a BSON string that is not null-terminated");
144143
}
145144
if (asciiByte < 0) {
146-
return UTF8_CHARSET.newDecoder().replacement();
145+
return StandardCharsets.UTF_8.newDecoder().replacement();
147146
}
148147
return ONE_BYTE_ASCII_STRINGS[asciiByte]; // this will throw if asciiByte is negative
149148
} else {
@@ -153,7 +152,7 @@ private String readString(final int size) {
153152
if (nullByte != 0) {
154153
throw new BsonSerializationException("Found a BSON string that is not null-terminated");
155154
}
156-
return new String(bytes, UTF8_CHARSET);
155+
return new String(bytes, StandardCharsets.UTF_8);
157156
}
158157
}
159158

bson/src/test/unit/org/bson/io/BasicOutputBufferTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.ByteArrayOutputStream;
2222
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
2324
import java.util.Arrays;
2425

2526
import static org.junit.Assert.assertArrayEquals;
@@ -42,7 +43,7 @@ public void shouldEncodeAllCodePointsThatAreLettersOrDigits() throws IOException
4243

4344
// then
4445
byte[] bytes = getBytes(bsonOutput);
45-
assertArrayEquals("failed with code point " + codePoint, str.getBytes("UTF-8"), Arrays.copyOfRange(bytes, 0, bytes.length - 1));
46+
assertArrayEquals("failed with code point " + codePoint, str.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(bytes, 0, bytes.length - 1));
4647
}
4748
}
4849

bson/src/test/unit/org/bson/json/JsonReaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.io.ByteArrayInputStream;
3333
import java.io.InputStreamReader;
3434
import java.io.Reader;
35-
import java.nio.charset.Charset;
35+
import java.nio.charset.StandardCharsets;
3636
import java.text.ParsePosition;
3737
import java.text.SimpleDateFormat;
3838
import java.util.Date;
@@ -1270,8 +1270,8 @@ private void testStringAndStream(final String json, final Function<AbstractBsonR
12701270
assertEquals(exClass, e.getClass());
12711271
}
12721272
try {
1273-
testFunc.apply(new JsonReader(new InputStreamReader(new ByteArrayInputStream(json.getBytes(Charset.forName("UTF-8"))),
1274-
Charset.forName("UTF-8"))));
1273+
testFunc.apply(new JsonReader(new InputStreamReader(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)),
1274+
StandardCharsets.UTF_8)));
12751275
} catch (final RuntimeException e) {
12761276
if (exClass == null) {
12771277
throw e;

bson/src/test/unit/util/JsonPoweredTestHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.io.InputStreamReader;
2929
import java.net.URISyntaxException;
3030
import java.net.URL;
31-
import java.nio.charset.Charset;
31+
import java.nio.charset.StandardCharsets;
3232
import java.nio.file.FileSystems;
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
@@ -95,7 +95,7 @@ private static String getFileAsString(final File file) throws IOException {
9595
StringBuilder stringBuilder = new StringBuilder();
9696
String line;
9797
String ls = System.getProperty("line.separator");
98-
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")));
98+
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
9999
try {
100100
while ((line = reader.readLine()) != null) {
101101
stringBuilder.append(line);

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.UnsupportedEncodingException;
2828
import java.net.URLDecoder;
29+
import java.nio.charset.StandardCharsets;
2930
import java.util.ArrayList;
3031
import java.util.Collections;
3132
import java.util.HashMap;
@@ -249,8 +250,6 @@ public class ConnectionString {
249250
private static final String MONGODB_SRV_PREFIX = "mongodb+srv://";
250251
private static final Set<String> ALLOWED_OPTIONS_IN_TXT_RECORD =
251252
new HashSet<String>(asList("authsource", "replicaset", "loadbalanced"));
252-
private static final String UTF_8 = "UTF-8";
253-
254253
private static final Logger LOGGER = Loggers.getLogger("uri");
255254

256255
private final MongoCredential credential;
@@ -1096,7 +1095,7 @@ private String urldecode(final String input) {
10961095

10971096
private String urldecode(final String input, final boolean password) {
10981097
try {
1099-
return URLDecoder.decode(input, UTF_8);
1098+
return URLDecoder.decode(input, StandardCharsets.UTF_8.name());
11001099
} catch (UnsupportedEncodingException e) {
11011100
if (password) {
11021101
throw new IllegalArgumentException("The connection string contained unsupported characters in the password.");

driver-core/src/main/com/mongodb/MongoClientSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.bson.codecs.configuration.CodecRegistry;
4040
import org.bson.codecs.jsr310.Jsr310CodecProvider;
4141

42-
import java.nio.charset.Charset;
42+
import java.nio.charset.StandardCharsets;
4343
import java.util.ArrayList;
4444
import java.util.Collections;
4545
import java.util.List;
@@ -448,7 +448,7 @@ public Builder commandListenerList(final List<CommandListener> commandListeners)
448448
public Builder applicationName(@Nullable final String applicationName) {
449449
if (applicationName != null) {
450450
isTrueArgument("applicationName UTF-8 encoding length <= 128",
451-
applicationName.getBytes(Charset.forName("UTF-8")).length <= 128);
451+
applicationName.getBytes(StandardCharsets.UTF_8).length <= 128);
452452
}
453453
this.applicationName = applicationName;
454454
return this;

driver-core/src/main/com/mongodb/internal/authentication/NativeAuthenticationHelper.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.io.ByteArrayOutputStream;
2424
import java.io.IOException;
25-
import java.nio.charset.Charset;
25+
import java.nio.charset.StandardCharsets;
2626

2727
import static com.mongodb.internal.HexUtils.hexMD5;
2828

@@ -33,8 +33,6 @@
3333
*/
3434
public final class NativeAuthenticationHelper {
3535

36-
private static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
37-
3836
/**
3937
* Creates a hash of the given user name and password, which is the hex encoding of
4038
* {@code MD5( <userName> + ":mongo:" + <password> )}.
@@ -47,9 +45,9 @@ public final class NativeAuthenticationHelper {
4745
public static String createAuthenticationHash(final String userName, final char[] password) {
4846
ByteArrayOutputStream bout = new ByteArrayOutputStream(userName.length() + 20 + password.length);
4947
try {
50-
bout.write(userName.getBytes(UTF_8_CHARSET));
51-
bout.write(":mongo:".getBytes(UTF_8_CHARSET));
52-
bout.write(new String(password).getBytes(UTF_8_CHARSET));
48+
bout.write(userName.getBytes(StandardCharsets.UTF_8));
49+
bout.write(":mongo:".getBytes(StandardCharsets.UTF_8));
50+
bout.write(new String(password).getBytes(StandardCharsets.UTF_8));
5351
} catch (IOException ioe) {
5452
throw new RuntimeException("impossible", ioe);
5553
}
@@ -68,7 +66,7 @@ public static BsonDocument getAuthCommand(final String userName, final String au
6866
cmd.put("authenticate", new BsonInt32(1));
6967
cmd.put("user", new BsonString(userName));
7068
cmd.put("nonce", new BsonString(nonce));
71-
cmd.put("key", new BsonString(hexMD5(key.getBytes(UTF_8_CHARSET))));
69+
cmd.put("key", new BsonString(hexMD5(key.getBytes(StandardCharsets.UTF_8))));
7270

7371
return cmd;
7472
}

driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.bson.codecs.EncoderContext;
2626
import org.bson.io.BasicOutputBuffer;
2727

28-
import java.nio.charset.Charset;
28+
import java.nio.charset.StandardCharsets;
2929
import java.util.List;
3030

3131
import static com.mongodb.assertions.Assertions.isTrueArgument;
@@ -111,7 +111,7 @@ static BsonDocument createClientMetadataDocument(final String applicationName, f
111111
final BsonDocument templateDocument) {
112112
if (applicationName != null) {
113113
isTrueArgument("applicationName UTF-8 encoding length <= 128",
114-
applicationName.getBytes(Charset.forName("UTF-8")).length <= 128);
114+
applicationName.getBytes(StandardCharsets.UTF_8).length <= 128);
115115
}
116116

117117
BsonDocument document = templateDocument.clone();

driver-reactive-streams/src/test/functional/com/mongodb/reactivestreams/client/JsonPoweredTestHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.io.IOException;
3030
import java.io.InputStreamReader;
3131
import java.net.URISyntaxException;
32-
import java.nio.charset.Charset;
32+
import java.nio.charset.StandardCharsets;
3333
import java.util.ArrayList;
3434
import java.util.List;
3535

@@ -49,7 +49,7 @@ private static String getFileAsString(final File file) throws IOException {
4949
StringBuilder stringBuilder = new StringBuilder();
5050
String line;
5151
String ls = System.getProperty("line.separator");
52-
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")));
52+
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
5353
try {
5454
while ((line = reader.readLine()) != null) {
5555
stringBuilder.append(line);

0 commit comments

Comments
 (0)