Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use charset from StandardCharsets instead of 'Charset.forName' #8779

Merged
merged 3 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/src/jmh/java/io/grpc/StatusBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.grpc;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand Down Expand Up @@ -57,7 +57,7 @@ public byte[] messageEncodeEscape() {
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public String messageDecodePlain() {
return Status.MESSAGE_KEY.parseBytes(
"Unexpected RST in stream".getBytes(Charset.forName("US-ASCII")));
"Unexpected RST in stream".getBytes(StandardCharsets.US_ASCII));
}

/**
Expand All @@ -68,7 +68,7 @@ public String messageDecodePlain() {
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public String messageDecodeEscape() {
return Status.MESSAGE_KEY.parseBytes(
"Some Error%10Wasabi and Horseradish are the same".getBytes(Charset.forName("US-ASCII")));
"Some Error%10Wasabi and Horseradish are the same".getBytes(StandardCharsets.US_ASCII));
}

/**
Expand All @@ -88,7 +88,7 @@ public byte[] codeEncode() {
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Status codeDecode() {
return Status.CODE_KEY.parseBytes("15".getBytes(Charset.forName("US-ASCII")));
return Status.CODE_KEY.parseBytes("15".getBytes(StandardCharsets.US_ASCII));
}
}

4 changes: 2 additions & 2 deletions api/src/main/java/io/grpc/DecompressorRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Joiner;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -85,7 +85,7 @@ private DecompressorRegistry(Decompressor d, boolean advertised, DecompressorReg

decompressors = Collections.unmodifiableMap(newDecompressors);
advertisedDecompressors = ACCEPT_ENCODING_JOINER.join(getAdvertisedMessageEncodings())
.getBytes(Charset.forName("US-ASCII"));
.getBytes(StandardCharsets.US_ASCII);
}

private DecompressorRegistry() {
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/io/grpc/InternalMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.grpc.Metadata.AsciiMarshaller;
import io.grpc.Metadata.BinaryStreamMarshaller;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Internal {@link Metadata} accessor. This is intended for use by io.grpc.internal, and the
Expand All @@ -42,7 +43,7 @@ public interface TrustedAsciiMarshaller<T> extends Metadata.TrustedAsciiMarshall
* Copy of StandardCharsets, which is only available on Java 1.7 and above.
*/
@Internal
public static final Charset US_ASCII = Charset.forName("US-ASCII");
public static final Charset US_ASCII = StandardCharsets.US_ASCII;

/**
* An instance of base64 encoder that omits padding.
Expand Down
4 changes: 3 additions & 1 deletion api/src/test/java/io/grpc/StatusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

import io.grpc.Status.Code;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link Status}. */
@RunWith(JUnit4.class)
public class StatusTest {
private final Charset ascii = Charset.forName("US-ASCII");
private final Charset ascii = StandardCharsets.US_ASCII;

@Test
public void verifyExceptionMessage() {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/grpc/internal/ClientCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import io.perfmark.PerfMark;
import io.perfmark.Tag;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
Expand All @@ -71,7 +71,7 @@ final class ClientCallImpl<ReqT, RespT> extends ClientCall<ReqT, RespT> {

private static final Logger log = Logger.getLogger(ClientCallImpl.class.getName());
private static final byte[] FULL_STREAM_DECOMPRESSION_ENCODINGS
= "gzip".getBytes(Charset.forName("US-ASCII"));
= "gzip".getBytes(StandardCharsets.US_ASCII);

private final MethodDescriptor<ReqT, RespT> method;
private final Tag tag;
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/grpc/internal/GrpcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;
Expand All @@ -75,7 +76,7 @@ public final class GrpcUtil {

private static final Logger log = Logger.getLogger(GrpcUtil.class.getName());

public static final Charset US_ASCII = Charset.forName("US-ASCII");
public static final Charset US_ASCII = StandardCharsets.US_ASCII;

/**
* {@link io.grpc.Metadata.Key} for the timeout header.
Expand Down
10 changes: 5 additions & 5 deletions cronet/src/main/java/io/grpc/cronet/CronetClientStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -408,10 +408,10 @@ private void setGrpcHeaders(BidirectionalStream.Builder builder) {
// String and byte array.
byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
for (int i = 0; i < serializedHeaders.length; i += 2) {
String key = new String(serializedHeaders[i], Charset.forName("UTF-8"));
String key = new String(serializedHeaders[i], StandardCharsets.UTF_8);
// TODO(ericgribkoff): log an error or throw an exception
if (isApplicationHeader(key)) {
String value = new String(serializedHeaders[i + 1], Charset.forName("UTF-8"));
String value = new String(serializedHeaders[i + 1], StandardCharsets.UTF_8);
builder.addHeader(key, value);
}
}
Expand Down Expand Up @@ -588,8 +588,8 @@ private void reportHeaders(List<Map.Entry<String, String>> headers, boolean endO

byte[][] headerValues = new byte[headerList.size()][];
for (int i = 0; i < headerList.size(); i += 2) {
headerValues[i] = headerList.get(i).getBytes(Charset.forName("UTF-8"));
headerValues[i + 1] = headerList.get(i + 1).getBytes(Charset.forName("UTF-8"));
headerValues[i] = headerList.get(i).getBytes(StandardCharsets.UTF_8);
headerValues[i + 1] = headerList.get(i + 1).getBytes(StandardCharsets.UTF_8);
}
Metadata metadata =
InternalMetadata.newMetadata(TransportFrameUtil.toRawSerializedHeaders(headerValues));
Expand Down
12 changes: 6 additions & 6 deletions cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.io.ByteArrayInputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -168,7 +168,7 @@ public void write() {
for (int i = 0; i < 5; ++i) {
requests[i] = new String("request" + String.valueOf(i));
buffers[i] = allocator.allocate(requests[i].length());
buffers[i].write(requests[i].getBytes(Charset.forName("UTF-8")), 0, requests[i].length());
buffers[i].write(requests[i].getBytes(StandardCharsets.UTF_8), 0, requests[i].length());
// The 3rd and 5th writeFrame calls have flush=true.
clientStream.abstractClientStreamSink().writeFrame(buffers[i], false, i == 2 || i == 4, 1);
}
Expand Down Expand Up @@ -261,7 +261,7 @@ public void read() {
callback.onReadCompleted(
cronetStream,
info,
createMessageFrame(new String("response1").getBytes(Charset.forName("UTF-8"))),
createMessageFrame(new String("response1").getBytes(StandardCharsets.UTF_8)),
false);
// Haven't request any message, so no callback is called here.
verify(clientListener, times(0)).messagesAvailable(isA(MessageProducer.class));
Expand Down Expand Up @@ -293,7 +293,7 @@ public void streamSucceeded() {
CronetWritableBufferAllocator allocator = new CronetWritableBufferAllocator();
String request = new String("request");
WritableBuffer writableBuffer = allocator.allocate(request.length());
writableBuffer.write(request.getBytes(Charset.forName("UTF-8")), 0, request.length());
writableBuffer.write(request.getBytes(StandardCharsets.UTF_8), 0, request.length());
clientStream.abstractClientStreamSink().writeFrame(writableBuffer, false, true, 1);
ArgumentCaptor<ByteBuffer> bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
verify(cronetStream, times(1)).write(bufferCaptor.capture(), isA(Boolean.class));
Expand All @@ -312,7 +312,7 @@ public void streamSucceeded() {
callback.onReadCompleted(
cronetStream,
info,
createMessageFrame(new String("response").getBytes(Charset.forName("UTF-8"))),
createMessageFrame(new String("response").getBytes(StandardCharsets.UTF_8)),
false);
verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
Expand Down Expand Up @@ -688,7 +688,7 @@ public void getUnaryRequest() {
.newBidirectionalStreamBuilder(
isA(String.class), isA(BidirectionalStream.Callback.class), isA(Executor.class));

byte[] msg = "request".getBytes(Charset.forName("UTF-8"));
byte[] msg = "request".getBytes(StandardCharsets.UTF_8);
stream.writeMessage(new ByteArrayInputStream(msg));
// We still haven't built the stream or sent anything.
verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* A {@link Marshaller} for JSON. This marshals in the Protobuf 3 format described here:
Expand Down Expand Up @@ -58,7 +59,7 @@ public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultIn
public static <T extends Message> Marshaller<T> jsonMarshaller(
final T defaultInstance, final Parser parser, final Printer printer) {

final Charset charset = Charset.forName("UTF-8");
final Charset charset = StandardCharsets.UTF_8;

return new Marshaller<T>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ public static URL getDefaultFeaturesFile() {
public static List<Feature> parseFeatures(URL file) throws IOException {
InputStream input = file.openStream();
try {
Reader reader = new InputStreamReader(input, Charset.forName("UTF-8"));
Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8);
try {
FeatureDatabase.Builder database = FeatureDatabase.newBuilder();
JsonFormat.parser().merge(reader, database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
Expand All @@ -50,7 +51,7 @@
*/
public class TestServiceClient {

private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset UTF_8 = StandardCharsets.UTF_8;

/**
* The main application allowing this client to be launched from the command line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import io.grpc.testing.integration.Messages.SimpleResponse;
import io.grpc.testing.integration.TestServiceGrpc.TestServiceBlockingStub;
import io.grpc.testing.integration.TransportCompressionTest.Fzip;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -312,6 +312,6 @@ public void onHeaders(Metadata headers) {
}

private static void assertEqualsString(String expected, byte[] actual) {
assertEquals(expected, new String(actual, Charset.forName("US-ASCII")));
assertEquals(expected, new String(actual, StandardCharsets.US_ASCII));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import io.grpc.okhttp.internal.framed.Header;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before;
Expand Down Expand Up @@ -243,7 +243,7 @@ public void getUnaryRequest() throws IOException {
eq(false), eq(false), eq(3), eq(0), headersCaptor.capture());
verify(transport, times(0)).streamReadyToStart(isA(OkHttpClientStream.class));

byte[] msg = "request".getBytes(Charset.forName("UTF-8"));
byte[] msg = "request".getBytes(StandardCharsets.UTF_8);
stream.writeMessage(new ByteArrayInputStream(msg));
stream.halfClose();
verify(transport).streamReadyToStart(eq(stream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand All @@ -47,7 +48,7 @@ public final class Util {
public static final String[] EMPTY_STRING_ARRAY = new String[0];

/** A cheap and type-safe constant for the UTF-8 Charset. */
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset UTF_8 = StandardCharsets.UTF_8;

private Util() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -94,7 +95,7 @@
/** Tests for {@link BinlogHelper}. */
@RunWith(JUnit4.class)
public final class BinlogHelperTest {
private static final Charset US_ASCII = Charset.forName("US-ASCII");
private static final Charset US_ASCII = StandardCharsets.US_ASCII;
private static final BinlogHelper HEADER_FULL = new Builder().header(Integer.MAX_VALUE).build();
private static final BinlogHelper HEADER_256 = new Builder().header(256).build();
private static final BinlogHelper MSG_FULL = new Builder().msg(Integer.MAX_VALUE).build();
Expand Down