mirror of https://github.com/grpc/grpc-java.git
use charset from StandardCharsets instead of 'Charset.forName' (#8779)
Co-authored-by: Penn (Dapeng) Zhang <zdapeng@google.com>
This commit is contained in:
parent
23a2202efa
commit
a74a3ad834
|
@ -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;
|
||||
|
@ -57,7 +57,7 @@ public class StatusBenchmark {
|
|||
@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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,7 +68,7 @@ public class StatusBenchmark {
|
|||
@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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,7 +88,7 @@ public class StatusBenchmark {
|
|||
@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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
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;
|
||||
|
@ -85,7 +85,7 @@ public final class DecompressorRegistry {
|
|||
|
||||
decompressors = Collections.unmodifiableMap(newDecompressors);
|
||||
advertisedDecompressors = ACCEPT_ENCODING_JOINER.join(getAdvertisedMessageEncodings())
|
||||
.getBytes(Charset.forName("US-ASCII"));
|
||||
.getBytes(StandardCharsets.US_ASCII);
|
||||
}
|
||||
|
||||
private DecompressorRegistry() {
|
||||
|
|
|
@ -20,6 +20,7 @@ import com.google.common.io.BaseEncoding;
|
|||
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
|
||||
|
@ -42,7 +43,7 @@ public final class InternalMetadata {
|
|||
* 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.
|
||||
|
|
|
@ -21,6 +21,8 @@ import static org.junit.Assert.assertSame;
|
|||
|
||||
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;
|
||||
|
@ -28,7 +30,7 @@ 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() {
|
||||
|
|
|
@ -53,7 +53,7 @@ import io.perfmark.Link;
|
|||
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;
|
||||
|
@ -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;
|
||||
|
|
|
@ -55,6 +55,7 @@ import java.net.SocketAddress;
|
|||
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;
|
||||
|
@ -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.
|
||||
|
|
|
@ -44,7 +44,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||
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;
|
||||
|
@ -408,10 +408,10 @@ class CronetClientStream extends AbstractClientStream {
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -588,8 +588,8 @@ class CronetClientStream extends AbstractClientStream {
|
|||
|
||||
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));
|
||||
|
|
|
@ -46,7 +46,7 @@ import io.grpc.testing.TestMethodDescriptors;
|
|||
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;
|
||||
|
@ -168,7 +168,7 @@ public final class CronetClientStreamTest {
|
|||
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);
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ public final class CronetClientStreamTest {
|
|||
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));
|
||||
|
@ -293,7 +293,7 @@ public final class CronetClientStreamTest {
|
|||
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));
|
||||
|
@ -312,7 +312,7 @@ public final class CronetClientStreamTest {
|
|||
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));
|
||||
|
@ -688,7 +688,7 @@ public final class CronetClientStreamTest {
|
|||
.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));
|
||||
|
|
|
@ -30,6 +30,7 @@ import java.io.InputStream;
|
|||
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:
|
||||
|
@ -58,7 +59,7 @@ final class JsonMarshaller {
|
|||
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
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.io.InputStream;
|
|||
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;
|
||||
|
||||
/**
|
||||
|
@ -58,7 +58,7 @@ public class RouteGuideUtil {
|
|||
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);
|
||||
|
|
|
@ -40,6 +40,7 @@ import io.grpc.okhttp.OkHttpChannelBuilder;
|
|||
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;
|
||||
|
@ -50,7 +51,7 @@ import javax.annotation.Nullable;
|
|||
*/
|
||||
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.
|
||||
|
|
|
@ -52,7 +52,7 @@ import io.grpc.testing.integration.Messages.SimpleRequest;
|
|||
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;
|
||||
|
@ -312,6 +312,6 @@ public class CompressionTest {
|
|||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ import io.grpc.okhttp.internal.framed.FrameWriter;
|
|||
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;
|
||||
|
@ -243,7 +243,7 @@ public class OkHttpClientStreamTest {
|
|||
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));
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.lang.reflect.Array;
|
|||
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;
|
||||
|
@ -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() {
|
||||
}
|
||||
|
|
|
@ -80,6 +80,7 @@ import java.net.InetAddress;
|
|||
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;
|
||||
|
@ -94,7 +95,7 @@ import org.mockito.ArgumentMatchers;
|
|||
/** 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();
|
||||
|
|
Loading…
Reference in New Issue