From fbc09c55f71f3e7de784b7cf6377b0ec2e1d4ede Mon Sep 17 00:00:00 2001 From: Andrei Arlou Date: Fri, 19 Mar 2021 13:15:50 +0300 Subject: [PATCH] Replace Charset.forName("UTF-8") on StandartCharsets.UTF_8 (#30113) --- .../PersistenceSchemaEvolutionDocTest.java | 8 +++++--- .../src/main/java/akka/protobuf/ByteString.java | 13 +++++-------- .../java/akka/protobuf/CodedOutputStream.java | 17 ++++++----------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/akka-docs/src/test/java/jdocs/persistence/PersistenceSchemaEvolutionDocTest.java b/akka-docs/src/test/java/jdocs/persistence/PersistenceSchemaEvolutionDocTest.java index d395b64be9..c6d7122625 100644 --- a/akka-docs/src/test/java/jdocs/persistence/PersistenceSchemaEvolutionDocTest.java +++ b/akka-docs/src/test/java/jdocs/persistence/PersistenceSchemaEvolutionDocTest.java @@ -9,6 +9,8 @@ import docs.persistence.proto.FlightAppModels; import java.io.NotSerializableException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + import spray.json.JsObject; import akka.persistence.journal.EventAdapter; @@ -186,7 +188,7 @@ public class PersistenceSchemaEvolutionDocTest { * to/from bytes marshalling. */ static class SimplestPossiblePersonSerializer extends SerializerWithStringManifest { - private final Charset utf8 = Charset.forName("UTF-8"); + private final Charset utf8 = StandardCharsets.UTF_8; private final String personManifest = Person.class.getName(); @@ -335,7 +337,7 @@ public class PersistenceSchemaEvolutionDocTest { public // #string-serializer-skip-deleved-event-by-manifest static class RemovedEventsAwareSerializer extends SerializerWithStringManifest { - private final Charset utf8 = Charset.forName("UTF-8"); + private final Charset utf8 = StandardCharsets.UTF_8; private final String customerBlinkedManifest = "blinked"; // unique identifier of the serializer @@ -389,7 +391,7 @@ public class PersistenceSchemaEvolutionDocTest { public // #string-serializer-handle-rename static class RenamedEventAwareSerializer extends SerializerWithStringManifest { - private final Charset utf8 = Charset.forName("UTF-8"); + private final Charset utf8 = StandardCharsets.UTF_8; // unique identifier of the serializer @Override diff --git a/akka-protobuf/src/main/java/akka/protobuf/ByteString.java b/akka-protobuf/src/main/java/akka/protobuf/ByteString.java index 0bc13ac5fd..3f5ae45531 100644 --- a/akka-protobuf/src/main/java/akka/protobuf/ByteString.java +++ b/akka-protobuf/src/main/java/akka/protobuf/ByteString.java @@ -40,6 +40,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -255,11 +256,7 @@ public abstract class ByteString implements Iterable { * @return new {@code ByteString} */ public static ByteString copyFromUtf8(String text) { - try { - return new LiteralByteString(text.getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 not supported?", e); - } + return new LiteralByteString(text.getBytes(StandardCharsets.UTF_8)); } // ================================================================= @@ -323,7 +320,7 @@ public abstract class ByteString implements Iterable { // Helper method that takes the chunk size range as a parameter. public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize) throws IOException { - Collection results = new ArrayList(); + Collection results = new ArrayList<>(); // copy the inbound bytes into a list of chunks; the chunk size // grows exponentially to support both short and long streams. @@ -408,7 +405,7 @@ public abstract class ByteString implements Iterable { public static ByteString copyFrom(Iterable byteStrings) { Collection collection; if (!(byteStrings instanceof Collection)) { - collection = new ArrayList(); + collection = new ArrayList<>(); for (ByteString byteString : byteStrings) { collection.add(byteString); } @@ -736,7 +733,7 @@ public abstract class ByteString implements Iterable { throw new IllegalArgumentException("Buffer size < 0"); } this.initialCapacity = initialCapacity; - this.flushedBuffers = new ArrayList(); + this.flushedBuffers = new ArrayList<>(); this.buffer = new byte[initialCapacity]; } diff --git a/akka-protobuf/src/main/java/akka/protobuf/CodedOutputStream.java b/akka-protobuf/src/main/java/akka/protobuf/CodedOutputStream.java index a22034c864..cecc617f19 100644 --- a/akka-protobuf/src/main/java/akka/protobuf/CodedOutputStream.java +++ b/akka-protobuf/src/main/java/akka/protobuf/CodedOutputStream.java @@ -37,7 +37,7 @@ package akka.protobuf; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * Encodes and writes protocol message fields. @@ -73,8 +73,7 @@ public final class CodedOutputStream { * CodedOutputStream. */ static int computePreferredBufferSize(int dataLength) { - if (dataLength > DEFAULT_BUFFER_SIZE) return DEFAULT_BUFFER_SIZE; - return dataLength; + return Math.min(dataLength, DEFAULT_BUFFER_SIZE); } private CodedOutputStream(final byte[] buffer, final int offset, @@ -356,7 +355,7 @@ public final class CodedOutputStream { // Unfortunately there does not appear to be any way to tell Java to encode // UTF-8 directly into our buffer, so we have to let it create its own byte // array and then copy. - final byte[] bytes = value.getBytes("UTF-8"); + final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); writeRawVarint32(bytes.length); writeRawBytes(bytes); } @@ -715,13 +714,9 @@ public final class CodedOutputStream { * {@code string} field. */ public static int computeStringSizeNoTag(final String value) { - try { - final byte[] bytes = value.getBytes("UTF-8"); - return computeRawVarint32Size(bytes.length) + - bytes.length; - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 not supported.", e); - } + final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); + return computeRawVarint32Size(bytes.length) + + bytes.length; } /**