Migrate from the deprecated Charsets constants (in Guava) to the StandardCharsets constants (in the JDK).

cl/658546708
This commit is contained in:
Kurt Alfred Kluever 2024-08-02 07:42:09 -07:00 committed by Eric Anderson
parent 1f9d502261
commit 00136096ed
9 changed files with 40 additions and 39 deletions

View File

@ -16,7 +16,6 @@
package io.grpc.internal;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import io.grpc.CallOptions;
import io.grpc.InternalMetadata;
@ -24,6 +23,7 @@ import io.grpc.InternalStatus;
import io.grpc.Metadata;
import io.grpc.Status;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;
/**
@ -62,7 +62,7 @@ public abstract class Http2ClientStreamTransportState extends AbstractClientStre
/** When non-{@code null}, {@link #transportErrorMetadata} must also be non-{@code null}. */
private Status transportError;
private Metadata transportErrorMetadata;
private Charset errorCharset = Charsets.UTF_8;
private Charset errorCharset = StandardCharsets.UTF_8;
private boolean headersReceived;
protected Http2ClientStreamTransportState(
@ -241,7 +241,7 @@ public abstract class Http2ClientStreamTransportState extends AbstractClientStre
// Ignore and assume UTF-8
}
}
return Charsets.UTF_8;
return StandardCharsets.UTF_8;
}
/**

View File

@ -31,7 +31,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.primitives.Bytes;
import io.grpc.Codec;
@ -46,6 +45,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@ -347,7 +347,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_readByteBelowLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 4, statsTraceCtx);
@ -360,7 +360,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_readByteAtLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
@ -373,7 +373,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_readByteAboveLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 2, statsTraceCtx);
@ -390,7 +390,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_readBelowLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 4, statsTraceCtx);
byte[] buf = new byte[10];
@ -404,7 +404,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_readAtLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
byte[] buf = new byte[10];
@ -418,7 +418,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_readAboveLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 2, statsTraceCtx);
byte[] buf = new byte[10];
@ -435,7 +435,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_skipBelowLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 4, statsTraceCtx);
@ -449,7 +449,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_skipAtLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
@ -462,7 +462,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_skipAboveLimit() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 2, statsTraceCtx);
@ -478,7 +478,7 @@ public class MessageDeframerTest {
@Test
public void sizeEnforcingInputStream_markReset() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(Charsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8));
SizeEnforcingInputStream stream =
new MessageDeframer.SizeEnforcingInputStream(in, 3, statsTraceCtx);
// stream currently looks like: |foo

View File

@ -19,7 +19,6 @@ package io.grpc.gcp.observability;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.cloud.ServiceOptions;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -28,6 +27,7 @@ import io.grpc.internal.JsonUtil;
import io.opencensus.trace.Sampler;
import io.opencensus.trace.samplers.Samplers;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
@ -75,7 +75,7 @@ final class ObservabilityConfigImpl implements ObservabilityConfig {
void parseFile(String configFile) throws IOException {
String configFileContent =
new String(Files.readAllBytes(Paths.get(configFile)), Charsets.UTF_8);
new String(Files.readAllBytes(Paths.get(configFile)), StandardCharsets.UTF_8);
checkArgument(!configFileContent.isEmpty(), CONFIG_FILE_ENV_VAR_NAME + " is empty!");
parse(configFileContent);
}

View File

@ -21,12 +21,12 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.common.base.Charsets;
import io.grpc.gcp.observability.ObservabilityConfig.LogFilter;
import io.opencensus.trace.Sampler;
import io.opencensus.trace.samplers.Samplers;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
@ -401,7 +401,8 @@ public class ObservabilityConfigImplTest {
public void configFileLogFilters() throws Exception {
File configFile = tempFolder.newFile();
Files.write(
Paths.get(configFile.getAbsolutePath()), CLIENT_LOG_FILTERS.getBytes(Charsets.US_ASCII));
Paths.get(configFile.getAbsolutePath()),
CLIENT_LOG_FILTERS.getBytes(StandardCharsets.US_ASCII));
observabilityConfig.parseFile(configFile.getAbsolutePath());
assertTrue(observabilityConfig.isEnableCloudLogging());
assertThat(observabilityConfig.getProjectId()).isEqualTo("grpc-testing");

View File

@ -19,7 +19,6 @@ package io.grpc.googleapis;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
@ -41,6 +40,7 @@ import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;
@ -263,7 +263,7 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
if (con.getResponseCode() != 200) {
return "";
}
try (Reader reader = new InputStreamReader(con.getInputStream(), Charsets.UTF_8)) {
try (Reader reader = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) {
respBody = CharStreams.toString(reader);
}
} finally {

View File

@ -30,11 +30,11 @@ import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import com.google.common.base.Charsets;
import io.grpc.internal.SerializingExecutor;
import io.grpc.okhttp.ExceptionHandlingFrameWriter.TransportExceptionHandler;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
@ -73,8 +73,8 @@ public class AsyncSinkTest {
@Test
public void flushCoalescing_shouldNotMergeTwoDistinctFlushes() throws IOException {
byte[] firstData = "a string".getBytes(Charsets.UTF_8);
byte[] secondData = "a longer string".getBytes(Charsets.UTF_8);
byte[] firstData = "a string".getBytes(StandardCharsets.UTF_8);
byte[] secondData = "a longer string".getBytes(StandardCharsets.UTF_8);
sink.becomeConnected(mockedSink, socket);
Buffer buffer = new Buffer();
@ -95,8 +95,8 @@ public class AsyncSinkTest {
@Test
public void flushCoalescing_shouldMergeTwoQueuedFlushesAndWrites() throws IOException {
byte[] firstData = "a string".getBytes(Charsets.UTF_8);
byte[] secondData = "a longer string".getBytes(Charsets.UTF_8);
byte[] firstData = "a string".getBytes(StandardCharsets.UTF_8);
byte[] secondData = "a longer string".getBytes(StandardCharsets.UTF_8);
Buffer buffer = new Buffer().write(firstData);
sink.becomeConnected(mockedSink, socket);
sink.write(buffer, buffer.size());
@ -115,8 +115,8 @@ public class AsyncSinkTest {
@Test
public void flushCoalescing_shouldMergeWrites() throws IOException {
byte[] firstData = "a string".getBytes(Charsets.UTF_8);
byte[] secondData = "a longer string".getBytes(Charsets.UTF_8);
byte[] firstData = "a string".getBytes(StandardCharsets.UTF_8);
byte[] secondData = "a longer string".getBytes(StandardCharsets.UTF_8);
Buffer buffer = new Buffer();
sink.becomeConnected(mockedSink, socket);
sink.write(buffer.write(firstData), buffer.size());

View File

@ -22,7 +22,6 @@ import static com.google.common.base.Preconditions.checkState;
import static io.grpc.protobuf.services.BinaryLogProvider.BYTEARRAY_MARSHALLER;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.protobuf.ByteString;
@ -59,6 +58,7 @@ import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -841,7 +841,7 @@ final class BinlogHelper {
if (serialized != null) {
int curBytes = 0;
for (int i = 0; i < serialized.length; i += 2) {
String key = new String(serialized[i], Charsets.UTF_8);
String key = new String(serialized[i], StandardCharsets.UTF_8);
byte[] value = serialized[i + 1];
if (NEVER_INCLUDED_METADATA.contains(key)) {
continue;

View File

@ -22,7 +22,6 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Any;
@ -82,6 +81,7 @@ import io.netty.channel.unix.DomainSocketAddress;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.security.cert.Certificate;
import java.util.Arrays;
import java.util.Collections;
@ -437,8 +437,8 @@ public final class ChannelzProtoUtilTest {
public void socketSecurityTls() throws Exception {
Certificate local = mock(Certificate.class);
Certificate remote = mock(Certificate.class);
when(local.getEncoded()).thenReturn("localcert".getBytes(Charsets.UTF_8));
when(remote.getEncoded()).thenReturn("remotecert".getBytes(Charsets.UTF_8));
when(local.getEncoded()).thenReturn("localcert".getBytes(StandardCharsets.UTF_8));
when(remote.getEncoded()).thenReturn("remotecert".getBytes(StandardCharsets.UTF_8));
socket.security = new InternalChannelz.Security(
new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", local, remote));
@ -446,8 +446,8 @@ public final class ChannelzProtoUtilTest {
Security.newBuilder().setTls(
Tls.newBuilder()
.setStandardName("TLS_NULL_WITH_NULL_NULL")
.setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8))
.setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8)))
.setLocalCertificate(ByteString.copyFrom("localcert", StandardCharsets.UTF_8))
.setRemoteCertificate(ByteString.copyFrom("remotecert", StandardCharsets.UTF_8)))
.build(),
ChannelzProtoUtil.toSocket(socket).getSecurity());
@ -457,7 +457,7 @@ public final class ChannelzProtoUtilTest {
Security.newBuilder().setTls(
Tls.newBuilder()
.setStandardName("TLS_NULL_WITH_NULL_NULL")
.setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8)))
.setRemoteCertificate(ByteString.copyFrom("remotecert", StandardCharsets.UTF_8)))
.build(),
ChannelzProtoUtil.toSocket(socket).getSecurity());
@ -467,7 +467,7 @@ public final class ChannelzProtoUtilTest {
Security.newBuilder().setTls(
Tls.newBuilder()
.setStandardName("TLS_NULL_WITH_NULL_NULL")
.setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8)))
.setLocalCertificate(ByteString.copyFrom("localcert", StandardCharsets.UTF_8)))
.build(),
ChannelzProtoUtil.toSocket(socket).getSecurity());
}

View File

@ -18,12 +18,12 @@ package io.grpc.util;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.base.Charsets;
import io.grpc.internal.testing.TestUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
@ -80,7 +80,7 @@ public class CertificateUtilsTest {
@Test
public void readBadFormatKeyFile() throws Exception {
InputStream in = new ByteArrayInputStream(BAD_PEM_FORMAT.getBytes(Charsets.UTF_8));
InputStream in = new ByteArrayInputStream(BAD_PEM_FORMAT.getBytes(StandardCharsets.UTF_8));
try {
CertificateUtils.getPrivateKey(in);
Assert.fail("no exception thrown");
@ -92,7 +92,7 @@ public class CertificateUtilsTest {
@Test
public void readBadContentKeyFile() {
InputStream in = new ByteArrayInputStream(BAD_PEM_CONTENT.getBytes(Charsets.UTF_8));
InputStream in = new ByteArrayInputStream(BAD_PEM_CONTENT.getBytes(StandardCharsets.UTF_8));
try {
CertificateUtils.getPrivateKey(in);
Assert.fail("no exception thrown");