Skip to content

Commit 0013609

Browse files
klueverejona86
authored andcommitted
Migrate from the deprecated Charsets constants (in Guava) to the StandardCharsets constants (in the JDK).
cl/658546708
1 parent 1f9d502 commit 0013609

File tree

9 files changed

+40
-39
lines changed

9 files changed

+40
-39
lines changed

core/src/main/java/io/grpc/internal/Http2ClientStreamTransportState.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
package io.grpc.internal;
1818

19-
import com.google.common.base.Charsets;
2019
import com.google.common.base.Preconditions;
2120
import io.grpc.CallOptions;
2221
import io.grpc.InternalMetadata;
2322
import io.grpc.InternalStatus;
2423
import io.grpc.Metadata;
2524
import io.grpc.Status;
2625
import java.nio.charset.Charset;
26+
import java.nio.charset.StandardCharsets;
2727
import javax.annotation.Nullable;
2828

2929
/**
@@ -62,7 +62,7 @@ public Integer parseAsciiString(byte[] serialized) {
6262
/** When non-{@code null}, {@link #transportErrorMetadata} must also be non-{@code null}. */
6363
private Status transportError;
6464
private Metadata transportErrorMetadata;
65-
private Charset errorCharset = Charsets.UTF_8;
65+
private Charset errorCharset = StandardCharsets.UTF_8;
6666
private boolean headersReceived;
6767

6868
protected Http2ClientStreamTransportState(
@@ -241,7 +241,7 @@ private static Charset extractCharset(Metadata headers) {
241241
// Ignore and assume UTF-8
242242
}
243243
}
244-
return Charsets.UTF_8;
244+
return StandardCharsets.UTF_8;
245245
}
246246

247247
/**

core/src/test/java/io/grpc/internal/MessageDeframerTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static org.mockito.Mockito.verify;
3232
import static org.mockito.Mockito.verifyNoMoreInteractions;
3333

34-
import com.google.common.base.Charsets;
3534
import com.google.common.io.ByteStreams;
3635
import com.google.common.primitives.Bytes;
3736
import io.grpc.Codec;
@@ -46,6 +45,7 @@
4645
import java.io.IOException;
4746
import java.io.InputStream;
4847
import java.io.OutputStream;
48+
import java.nio.charset.StandardCharsets;
4949
import java.util.Arrays;
5050
import java.util.Collection;
5151
import java.util.List;
@@ -347,7 +347,7 @@ public static class SizeEnforcingInputStreamTests {
347347

348348
@Test
349349
public void sizeEnforcingInputStream_readByteBelowLimit() throws IOException {
350-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
350+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
351351
SizeEnforcingInputStream stream =
352352
new MessageDeframer.SizeEnforcingInputStream(in, 4, statsTraceCtx);
353353

@@ -360,7 +360,7 @@ public void sizeEnforcingInputStream_readByteBelowLimit() throws IOException {
360360

361361
@Test
362362
public void sizeEnforcingInputStream_readByteAtLimit() throws IOException {
363-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
363+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
364364
SizeEnforcingInputStream stream =
365365
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
366366

@@ -373,7 +373,7 @@ public void sizeEnforcingInputStream_readByteAtLimit() throws IOException {
373373

374374
@Test
375375
public void sizeEnforcingInputStream_readByteAboveLimit() throws IOException {
376-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
376+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
377377
SizeEnforcingInputStream stream =
378378
new MessageDeframer.SizeEnforcingInputStream(in, 2, statsTraceCtx);
379379

@@ -390,7 +390,7 @@ public void sizeEnforcingInputStream_readByteAboveLimit() throws IOException {
390390

391391
@Test
392392
public void sizeEnforcingInputStream_readBelowLimit() throws IOException {
393-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
393+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
394394
SizeEnforcingInputStream stream =
395395
new MessageDeframer.SizeEnforcingInputStream(in, 4, statsTraceCtx);
396396
byte[] buf = new byte[10];
@@ -404,7 +404,7 @@ public void sizeEnforcingInputStream_readBelowLimit() throws IOException {
404404

405405
@Test
406406
public void sizeEnforcingInputStream_readAtLimit() throws IOException {
407-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
407+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
408408
SizeEnforcingInputStream stream =
409409
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
410410
byte[] buf = new byte[10];
@@ -418,7 +418,7 @@ public void sizeEnforcingInputStream_readAtLimit() throws IOException {
418418

419419
@Test
420420
public void sizeEnforcingInputStream_readAboveLimit() throws IOException {
421-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
421+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
422422
SizeEnforcingInputStream stream =
423423
new MessageDeframer.SizeEnforcingInputStream(in, 2, statsTraceCtx);
424424
byte[] buf = new byte[10];
@@ -435,7 +435,7 @@ public void sizeEnforcingInputStream_readAboveLimit() throws IOException {
435435

436436
@Test
437437
public void sizeEnforcingInputStream_skipBelowLimit() throws IOException {
438-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
438+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
439439
SizeEnforcingInputStream stream =
440440
new MessageDeframer.SizeEnforcingInputStream(in, 4, statsTraceCtx);
441441

@@ -449,7 +449,7 @@ public void sizeEnforcingInputStream_skipBelowLimit() throws IOException {
449449

450450
@Test
451451
public void sizeEnforcingInputStream_skipAtLimit() throws IOException {
452-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
452+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
453453
SizeEnforcingInputStream stream =
454454
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
455455

@@ -462,7 +462,7 @@ public void sizeEnforcingInputStream_skipAtLimit() throws IOException {
462462

463463
@Test
464464
public void sizeEnforcingInputStream_skipAboveLimit() throws IOException {
465-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
465+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
466466
SizeEnforcingInputStream stream =
467467
new MessageDeframer.SizeEnforcingInputStream(in, 2, statsTraceCtx);
468468

@@ -478,7 +478,7 @@ public void sizeEnforcingInputStream_skipAboveLimit() throws IOException {
478478

479479
@Test
480480
public void sizeEnforcingInputStream_markReset() throws IOException {
481-
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
481+
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
482482
SizeEnforcingInputStream stream =
483483
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
484484
// stream currently looks like: |foo

gcp-observability/src/main/java/io/grpc/gcp/observability/ObservabilityConfigImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020

2121
import com.google.cloud.ServiceOptions;
22-
import com.google.common.base.Charsets;
2322
import com.google.common.collect.ImmutableList;
2423
import com.google.common.collect.ImmutableMap;
2524
import com.google.common.collect.ImmutableSet;
@@ -28,6 +27,7 @@
2827
import io.opencensus.trace.Sampler;
2928
import io.opencensus.trace.samplers.Samplers;
3029
import java.io.IOException;
30+
import java.nio.charset.StandardCharsets;
3131
import java.nio.file.Files;
3232
import java.nio.file.Paths;
3333
import java.util.Collections;
@@ -75,7 +75,7 @@ static ObservabilityConfigImpl getInstance() throws IOException {
7575

7676
void parseFile(String configFile) throws IOException {
7777
String configFileContent =
78-
new String(Files.readAllBytes(Paths.get(configFile)), Charsets.UTF_8);
78+
new String(Files.readAllBytes(Paths.get(configFile)), StandardCharsets.UTF_8);
7979
checkArgument(!configFileContent.isEmpty(), CONFIG_FILE_ENV_VAR_NAME + " is empty!");
8080
parse(configFileContent);
8181
}

gcp-observability/src/test/java/io/grpc/gcp/observability/ObservabilityConfigImplTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import static org.junit.Assert.assertTrue;
2222
import static org.junit.Assert.fail;
2323

24-
import com.google.common.base.Charsets;
2524
import io.grpc.gcp.observability.ObservabilityConfig.LogFilter;
2625
import io.opencensus.trace.Sampler;
2726
import io.opencensus.trace.samplers.Samplers;
2827
import java.io.File;
2928
import java.io.IOException;
29+
import java.nio.charset.StandardCharsets;
3030
import java.nio.file.Files;
3131
import java.nio.file.Paths;
3232
import java.util.Collections;
@@ -401,7 +401,8 @@ public void badProbabilisticSampler_error() throws IOException {
401401
public void configFileLogFilters() throws Exception {
402402
File configFile = tempFolder.newFile();
403403
Files.write(
404-
Paths.get(configFile.getAbsolutePath()), CLIENT_LOG_FILTERS.getBytes(Charsets.US_ASCII));
404+
Paths.get(configFile.getAbsolutePath()),
405+
CLIENT_LOG_FILTERS.getBytes(StandardCharsets.US_ASCII));
405406
observabilityConfig.parseFile(configFile.getAbsolutePath());
406407
assertTrue(observabilityConfig.isEnableCloudLogging());
407408
assertThat(observabilityConfig.getProjectId()).isEqualTo("grpc-testing");

googleapis/src/main/java/io/grpc/googleapis/GoogleCloudToProdNameResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

2121
import com.google.common.annotations.VisibleForTesting;
22-
import com.google.common.base.Charsets;
2322
import com.google.common.base.Preconditions;
2423
import com.google.common.base.Strings;
2524
import com.google.common.collect.ImmutableList;
@@ -41,6 +40,7 @@
4140
import java.net.URI;
4241
import java.net.URISyntaxException;
4342
import java.net.URL;
43+
import java.nio.charset.StandardCharsets;
4444
import java.util.Map;
4545
import java.util.Random;
4646
import java.util.concurrent.Executor;
@@ -263,7 +263,7 @@ private String queryZoneMetadata(String url) throws IOException {
263263
if (con.getResponseCode() != 200) {
264264
return "";
265265
}
266-
try (Reader reader = new InputStreamReader(con.getInputStream(), Charsets.UTF_8)) {
266+
try (Reader reader = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) {
267267
respBody = CharStreams.toString(reader);
268268
}
269269
} finally {

okhttp/src/test/java/io/grpc/okhttp/AsyncSinkTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
import static org.mockito.Mockito.timeout;
3131
import static org.mockito.Mockito.verify;
3232

33-
import com.google.common.base.Charsets;
3433
import io.grpc.internal.SerializingExecutor;
3534
import io.grpc.okhttp.ExceptionHandlingFrameWriter.TransportExceptionHandler;
3635
import java.io.IOException;
3736
import java.net.Socket;
37+
import java.nio.charset.StandardCharsets;
3838
import java.util.Queue;
3939
import java.util.concurrent.ConcurrentLinkedQueue;
4040
import java.util.concurrent.Executor;
@@ -73,8 +73,8 @@ public void noCoalesceRequired() throws IOException {
7373

7474
@Test
7575
public void flushCoalescing_shouldNotMergeTwoDistinctFlushes() throws IOException {
76-
byte[] firstData = "a string".getBytes(Charsets.UTF_8);
77-
byte[] secondData = "a longer string".getBytes(Charsets.UTF_8);
76+
byte[] firstData = "a string".getBytes(StandardCharsets.UTF_8);
77+
byte[] secondData = "a longer string".getBytes(StandardCharsets.UTF_8);
7878

7979
sink.becomeConnected(mockedSink, socket);
8080
Buffer buffer = new Buffer();
@@ -95,8 +95,8 @@ public void flushCoalescing_shouldNotMergeTwoDistinctFlushes() throws IOExceptio
9595

9696
@Test
9797
public void flushCoalescing_shouldMergeTwoQueuedFlushesAndWrites() throws IOException {
98-
byte[] firstData = "a string".getBytes(Charsets.UTF_8);
99-
byte[] secondData = "a longer string".getBytes(Charsets.UTF_8);
98+
byte[] firstData = "a string".getBytes(StandardCharsets.UTF_8);
99+
byte[] secondData = "a longer string".getBytes(StandardCharsets.UTF_8);
100100
Buffer buffer = new Buffer().write(firstData);
101101
sink.becomeConnected(mockedSink, socket);
102102
sink.write(buffer, buffer.size());
@@ -115,8 +115,8 @@ public void flushCoalescing_shouldMergeTwoQueuedFlushesAndWrites() throws IOExce
115115

116116
@Test
117117
public void flushCoalescing_shouldMergeWrites() throws IOException {
118-
byte[] firstData = "a string".getBytes(Charsets.UTF_8);
119-
byte[] secondData = "a longer string".getBytes(Charsets.UTF_8);
118+
byte[] firstData = "a string".getBytes(StandardCharsets.UTF_8);
119+
byte[] secondData = "a longer string".getBytes(StandardCharsets.UTF_8);
120120
Buffer buffer = new Buffer();
121121
sink.becomeConnected(mockedSink, socket);
122122
sink.write(buffer.write(firstData), buffer.size());

services/src/main/java/io/grpc/protobuf/services/BinlogHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static io.grpc.protobuf.services.BinaryLogProvider.BYTEARRAY_MARSHALLER;
2323

2424
import com.google.common.annotations.VisibleForTesting;
25-
import com.google.common.base.Charsets;
2625
import com.google.common.base.Preconditions;
2726
import com.google.common.base.Splitter;
2827
import com.google.protobuf.ByteString;
@@ -59,6 +58,7 @@
5958
import java.net.InetAddress;
6059
import java.net.InetSocketAddress;
6160
import java.net.SocketAddress;
61+
import java.nio.charset.StandardCharsets;
6262
import java.util.Collections;
6363
import java.util.HashMap;
6464
import java.util.HashSet;
@@ -841,7 +841,7 @@ static MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> createMetadataProto
841841
if (serialized != null) {
842842
int curBytes = 0;
843843
for (int i = 0; i < serialized.length; i += 2) {
844-
String key = new String(serialized[i], Charsets.UTF_8);
844+
String key = new String(serialized[i], StandardCharsets.UTF_8);
845845
byte[] value = serialized[i + 1];
846846
if (NEVER_INCLUDED_METADATA.contains(key)) {
847847
continue;

services/src/test/java/io/grpc/protobuf/services/ChannelzProtoUtilTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.mockito.Mockito.mock;
2323
import static org.mockito.Mockito.when;
2424

25-
import com.google.common.base.Charsets;
2625
import com.google.common.collect.ImmutableList;
2726
import com.google.common.collect.ImmutableMap;
2827
import com.google.protobuf.Any;
@@ -82,6 +81,7 @@
8281
import java.net.Inet4Address;
8382
import java.net.InetSocketAddress;
8483
import java.net.SocketAddress;
84+
import java.nio.charset.StandardCharsets;
8585
import java.security.cert.Certificate;
8686
import java.util.Arrays;
8787
import java.util.Collections;
@@ -437,17 +437,17 @@ public void toSocketData() throws Exception {
437437
public void socketSecurityTls() throws Exception {
438438
Certificate local = mock(Certificate.class);
439439
Certificate remote = mock(Certificate.class);
440-
when(local.getEncoded()).thenReturn("localcert".getBytes(Charsets.UTF_8));
441-
when(remote.getEncoded()).thenReturn("remotecert".getBytes(Charsets.UTF_8));
440+
when(local.getEncoded()).thenReturn("localcert".getBytes(StandardCharsets.UTF_8));
441+
when(remote.getEncoded()).thenReturn("remotecert".getBytes(StandardCharsets.UTF_8));
442442

443443
socket.security = new InternalChannelz.Security(
444444
new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", local, remote));
445445
assertEquals(
446446
Security.newBuilder().setTls(
447447
Tls.newBuilder()
448448
.setStandardName("TLS_NULL_WITH_NULL_NULL")
449-
.setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8))
450-
.setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8)))
449+
.setLocalCertificate(ByteString.copyFrom("localcert", StandardCharsets.UTF_8))
450+
.setRemoteCertificate(ByteString.copyFrom("remotecert", StandardCharsets.UTF_8)))
451451
.build(),
452452
ChannelzProtoUtil.toSocket(socket).getSecurity());
453453

@@ -457,7 +457,7 @@ public void socketSecurityTls() throws Exception {
457457
Security.newBuilder().setTls(
458458
Tls.newBuilder()
459459
.setStandardName("TLS_NULL_WITH_NULL_NULL")
460-
.setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8)))
460+
.setRemoteCertificate(ByteString.copyFrom("remotecert", StandardCharsets.UTF_8)))
461461
.build(),
462462
ChannelzProtoUtil.toSocket(socket).getSecurity());
463463

@@ -467,7 +467,7 @@ public void socketSecurityTls() throws Exception {
467467
Security.newBuilder().setTls(
468468
Tls.newBuilder()
469469
.setStandardName("TLS_NULL_WITH_NULL_NULL")
470-
.setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8)))
470+
.setLocalCertificate(ByteString.copyFrom("localcert", StandardCharsets.UTF_8)))
471471
.build(),
472472
ChannelzProtoUtil.toSocket(socket).getSecurity());
473473
}

util/src/test/java/io/grpc/util/CertificateUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import com.google.common.base.Charsets;
2221
import io.grpc.internal.testing.TestUtils;
2322
import java.io.ByteArrayInputStream;
2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.math.BigInteger;
26+
import java.nio.charset.StandardCharsets;
2727
import java.security.PrivateKey;
2828
import java.security.cert.CertificateException;
2929
import java.security.cert.X509Certificate;
@@ -80,7 +80,7 @@ public void readCaPemFile() throws CertificateException, IOException {
8080

8181
@Test
8282
public void readBadFormatKeyFile() throws Exception {
83-
InputStream in = new ByteArrayInputStream(BAD_PEM_FORMAT.getBytes(Charsets.UTF_8));
83+
InputStream in = new ByteArrayInputStream(BAD_PEM_FORMAT.getBytes(StandardCharsets.UTF_8));
8484
try {
8585
CertificateUtils.getPrivateKey(in);
8686
Assert.fail("no exception thrown");
@@ -92,7 +92,7 @@ public void readBadFormatKeyFile() throws Exception {
9292

9393
@Test
9494
public void readBadContentKeyFile() {
95-
InputStream in = new ByteArrayInputStream(BAD_PEM_CONTENT.getBytes(Charsets.UTF_8));
95+
InputStream in = new ByteArrayInputStream(BAD_PEM_CONTENT.getBytes(StandardCharsets.UTF_8));
9696
try {
9797
CertificateUtils.getPrivateKey(in);
9898
Assert.fail("no exception thrown");

0 commit comments

Comments
 (0)