From 77e59b29dd5328fdb8166e22b6968e3ea834ed8c Mon Sep 17 00:00:00 2001 From: Ashok Varma Date: Sun, 7 Apr 2024 07:10:53 -0700 Subject: [PATCH] cronet: Update to StandardCharsets and assertNotNull API's --- .../java/io/grpc/cronet/CronetClientStream.java | 10 +++++----- .../io/grpc/cronet/CronetClientStreamTest.java | 17 +++++++++-------- .../grpc/cronet/CronetClientTransportTest.java | 4 ++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cronet/src/main/java/io/grpc/cronet/CronetClientStream.java b/cronet/src/main/java/io/grpc/cronet/CronetClientStream.java index f25d880a94..9ae9765231 100644 --- a/cronet/src/main/java/io/grpc/cronet/CronetClientStream.java +++ b/cronet/src/main/java/io/grpc/cronet/CronetClientStream.java @@ -42,7 +42,7 @@ import io.grpc.internal.TransportTracer; import io.grpc.internal.WritableBuffer; 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; @@ -372,10 +372,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); } } @@ -552,8 +552,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)); diff --git a/cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java b/cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java index e1ff27b8c4..e2b0e0b26c 100644 --- a/cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java +++ b/cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java @@ -18,6 +18,7 @@ package io.grpc.cronet; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -46,7 +47,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; @@ -102,7 +103,7 @@ public final class CronetClientStreamTest { @Override @SuppressWarnings("GuardedBy") public void run() { - assertTrue(stream != null); + assertNotNull(stream); stream.transportState().start(factory); } } @@ -168,7 +169,7 @@ public final class CronetClientStreamTest { for (int i = 0; i < 5; ++i) { requests[i] = "request" + 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); } @@ -253,7 +254,7 @@ public final class CronetClientStreamTest { callback.onReadCompleted( cronetStream, info, - createMessageFrame(new String("response1").getBytes(Charset.forName("UTF-8"))), + createMessageFrame("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)); @@ -283,9 +284,9 @@ public final class CronetClientStreamTest { verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class)); // Send the first data frame. CronetWritableBufferAllocator allocator = new CronetWritableBufferAllocator(); - String request = new String("request"); + String request = "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 bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class); verify(cronetStream, times(1)).write(bufferCaptor.capture(), isA(Boolean.class)); @@ -304,7 +305,7 @@ public final class CronetClientStreamTest { callback.onReadCompleted( cronetStream, info, - createMessageFrame(new String("response").getBytes(Charset.forName("UTF-8"))), + createMessageFrame("response".getBytes(StandardCharsets.UTF_8)), false); verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class)); verify(cronetStream, times(2)).read(isA(ByteBuffer.class)); @@ -680,7 +681,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)); diff --git a/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java b/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java index 4a99f2b12a..03c31f9332 100644 --- a/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java +++ b/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java @@ -17,7 +17,7 @@ package io.grpc.cronet; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -91,7 +91,7 @@ public final class CronetClientTransportTest { false, false); Runnable callback = transport.start(clientTransportListener); - assertTrue(callback != null); + assertNotNull(callback); callback.run(); verify(clientTransportListener).transportReady(); }