Replace Charset.forName("UTF-8") on StandartCharsets.UTF_8 (#30113)

This commit is contained in:
Andrei Arlou 2021-03-19 13:15:50 +03:00 committed by GitHub
parent c737f1a8e6
commit fbc09c55f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 22 deletions

View file

@ -9,6 +9,8 @@ import docs.persistence.proto.FlightAppModels;
import java.io.NotSerializableException; import java.io.NotSerializableException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import spray.json.JsObject; import spray.json.JsObject;
import akka.persistence.journal.EventAdapter; import akka.persistence.journal.EventAdapter;
@ -186,7 +188,7 @@ public class PersistenceSchemaEvolutionDocTest {
* to/from bytes marshalling. * to/from bytes marshalling.
*/ */
static class SimplestPossiblePersonSerializer extends SerializerWithStringManifest { 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(); private final String personManifest = Person.class.getName();
@ -335,7 +337,7 @@ public class PersistenceSchemaEvolutionDocTest {
public public
// #string-serializer-skip-deleved-event-by-manifest // #string-serializer-skip-deleved-event-by-manifest
static class RemovedEventsAwareSerializer extends SerializerWithStringManifest { 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"; private final String customerBlinkedManifest = "blinked";
// unique identifier of the serializer // unique identifier of the serializer
@ -389,7 +391,7 @@ public class PersistenceSchemaEvolutionDocTest {
public public
// #string-serializer-handle-rename // #string-serializer-handle-rename
static class RenamedEventAwareSerializer extends SerializerWithStringManifest { 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 // unique identifier of the serializer
@Override @Override

View file

@ -40,6 +40,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
@ -255,11 +256,7 @@ public abstract class ByteString implements Iterable<Byte> {
* @return new {@code ByteString} * @return new {@code ByteString}
*/ */
public static ByteString copyFromUtf8(String text) { public static ByteString copyFromUtf8(String text) {
try { return new LiteralByteString(text.getBytes(StandardCharsets.UTF_8));
return new LiteralByteString(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?", e);
}
} }
// ================================================================= // =================================================================
@ -323,7 +320,7 @@ public abstract class ByteString implements Iterable<Byte> {
// Helper method that takes the chunk size range as a parameter. // Helper method that takes the chunk size range as a parameter.
public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, public static ByteString readFrom(InputStream streamToDrain, int minChunkSize,
int maxChunkSize) throws IOException { int maxChunkSize) throws IOException {
Collection<ByteString> results = new ArrayList<ByteString>(); Collection<ByteString> results = new ArrayList<>();
// copy the inbound bytes into a list of chunks; the chunk size // copy the inbound bytes into a list of chunks; the chunk size
// grows exponentially to support both short and long streams. // grows exponentially to support both short and long streams.
@ -408,7 +405,7 @@ public abstract class ByteString implements Iterable<Byte> {
public static ByteString copyFrom(Iterable<ByteString> byteStrings) { public static ByteString copyFrom(Iterable<ByteString> byteStrings) {
Collection<ByteString> collection; Collection<ByteString> collection;
if (!(byteStrings instanceof Collection)) { if (!(byteStrings instanceof Collection)) {
collection = new ArrayList<ByteString>(); collection = new ArrayList<>();
for (ByteString byteString : byteStrings) { for (ByteString byteString : byteStrings) {
collection.add(byteString); collection.add(byteString);
} }
@ -736,7 +733,7 @@ public abstract class ByteString implements Iterable<Byte> {
throw new IllegalArgumentException("Buffer size < 0"); throw new IllegalArgumentException("Buffer size < 0");
} }
this.initialCapacity = initialCapacity; this.initialCapacity = initialCapacity;
this.flushedBuffers = new ArrayList<ByteString>(); this.flushedBuffers = new ArrayList<>();
this.buffer = new byte[initialCapacity]; this.buffer = new byte[initialCapacity];
} }

View file

@ -37,7 +37,7 @@ package akka.protobuf;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets;
/** /**
* Encodes and writes protocol message fields. * Encodes and writes protocol message fields.
@ -73,8 +73,7 @@ public final class CodedOutputStream {
* CodedOutputStream. * CodedOutputStream.
*/ */
static int computePreferredBufferSize(int dataLength) { static int computePreferredBufferSize(int dataLength) {
if (dataLength > DEFAULT_BUFFER_SIZE) return DEFAULT_BUFFER_SIZE; return Math.min(dataLength, DEFAULT_BUFFER_SIZE);
return dataLength;
} }
private CodedOutputStream(final byte[] buffer, final int offset, 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 // 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 // UTF-8 directly into our buffer, so we have to let it create its own byte
// array and then copy. // array and then copy.
final byte[] bytes = value.getBytes("UTF-8"); final byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
writeRawVarint32(bytes.length); writeRawVarint32(bytes.length);
writeRawBytes(bytes); writeRawBytes(bytes);
} }
@ -715,13 +714,9 @@ public final class CodedOutputStream {
* {@code string} field. * {@code string} field.
*/ */
public static int computeStringSizeNoTag(final String value) { public static int computeStringSizeNoTag(final String value) {
try { final byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
final byte[] bytes = value.getBytes("UTF-8");
return computeRawVarint32Size(bytes.length) + return computeRawVarint32Size(bytes.length) +
bytes.length; bytes.length;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported.", e);
}
} }
/** /**