diff --git a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java index 1217e112e0..0471afaf1f 100644 --- a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java +++ b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java @@ -34,6 +34,7 @@ import static com.google.protobuf.MessageSchema.getMutableUnknownFields; import com.google.protobuf.Internal.ProtobufList; import java.io.IOException; +import java.nio.charset.StandardCharsets; /** * Helper functions to decode protobuf wire format from a byte array. @@ -191,7 +192,7 @@ final class ArrayDecoders { registers.object1 = ""; return position; } else { - registers.object1 = new String(data, position, length, Internal.UTF_8); + registers.object1 = new String(data, position, length, StandardCharsets.UTF_8); return position + length; } } @@ -577,7 +578,7 @@ final class ArrayDecoders { } else if (length == 0) { output.add(""); } else { - String value = new String(data, position, length, Internal.UTF_8); + String value = new String(data, position, length, StandardCharsets.UTF_8); output.add(value); position += length; } @@ -593,7 +594,7 @@ final class ArrayDecoders { } else if (nextLength == 0) { output.add(""); } else { - String value = new String(data, position, nextLength, Internal.UTF_8); + String value = new String(data, position, nextLength, StandardCharsets.UTF_8); output.add(value); position += nextLength; } @@ -619,7 +620,7 @@ final class ArrayDecoders { if (!Utf8.isValidUtf8(data, position, position + length)) { throw InvalidProtocolBufferException.invalidUtf8(); } - String value = new String(data, position, length, Internal.UTF_8); + String value = new String(data, position, length, StandardCharsets.UTF_8); output.add(value); position += length; } @@ -638,7 +639,7 @@ final class ArrayDecoders { if (!Utf8.isValidUtf8(data, position, position + nextLength)) { throw InvalidProtocolBufferException.invalidUtf8(); } - String value = new String(data, position, nextLength, Internal.UTF_8); + String value = new String(data, position, nextLength, StandardCharsets.UTF_8); output.add(value); position += nextLength; } diff --git a/java/core/src/main/java/com/google/protobuf/BinaryReader.java b/java/core/src/main/java/com/google/protobuf/BinaryReader.java index d64574c2a5..547e78c014 100644 --- a/java/core/src/main/java/com/google/protobuf/BinaryReader.java +++ b/java/core/src/main/java/com/google/protobuf/BinaryReader.java @@ -41,6 +41,7 @@ import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -226,7 +227,7 @@ abstract class BinaryReader implements Reader { if (requireUtf8 && !Utf8.isValidUtf8(buffer, pos, pos + size)) { throw InvalidProtocolBufferException.invalidUtf8(); } - String result = new String(buffer, pos, size, Internal.UTF_8); + String result = new String(buffer, pos, size, StandardCharsets.UTF_8); pos += size; return result; } diff --git a/java/core/src/main/java/com/google/protobuf/ByteString.java b/java/core/src/main/java/com/google/protobuf/ByteString.java index a4beaeb4cf..c42b28c4f2 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteString.java +++ b/java/core/src/main/java/com/google/protobuf/ByteString.java @@ -45,6 +45,7 @@ import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Arrays; @@ -460,7 +461,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new {@code ByteString} */ public static ByteString copyFromUtf8(String text) { - return new LiteralByteString(text.getBytes(Internal.UTF_8)); + return new LiteralByteString(text.getBytes(StandardCharsets.UTF_8)); } // ================================================================= @@ -833,7 +834,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new string using UTF-8 encoding */ public final String toStringUtf8() { - return toString(Internal.UTF_8); + return toString(StandardCharsets.UTF_8); } /** diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java index 6e9c0f6209..ad87d4e05c 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java @@ -32,12 +32,13 @@ package com.google.protobuf; import static com.google.protobuf.Internal.EMPTY_BYTE_ARRAY; import static com.google.protobuf.Internal.EMPTY_BYTE_BUFFER; -import static com.google.protobuf.Internal.UTF_8; import static com.google.protobuf.Internal.checkNotNull; import static com.google.protobuf.WireFormat.FIXED32_SIZE; import static com.google.protobuf.WireFormat.FIXED64_SIZE; import static com.google.protobuf.WireFormat.MAX_VARINT_SIZE; +import static java.nio.charset.StandardCharsets.UTF_8; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 12f2097a8f..65ea627044 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -42,6 +42,7 @@ import java.io.OutputStream; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.Logger; @@ -842,7 +843,7 @@ public abstract class CodedOutputStream extends ByteOutput { length = Utf8.encodedLength(value); } catch (UnpairedSurrogateException e) { // TODO(dweis): Consider using nio Charset methods instead. - final byte[] bytes = value.getBytes(Internal.UTF_8); + final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); length = bytes.length; } @@ -989,8 +990,7 @@ public abstract class CodedOutputStream extends ByteOutput { // 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. - // TODO(dweis): Consider using nio Charset methods instead. - final byte[] bytes = value.getBytes(Internal.UTF_8); + final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); try { writeUInt32NoTag(bytes.length); writeLazy(bytes, 0, bytes.length); diff --git a/java/core/src/main/java/com/google/protobuf/Descriptors.java b/java/core/src/main/java/com/google/protobuf/Descriptors.java index f36d033327..c3b6dc09e0 100644 --- a/java/core/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/core/src/main/java/com/google/protobuf/Descriptors.java @@ -51,6 +51,7 @@ import com.google.protobuf.DescriptorProtos.ServiceOptions; import com.google.protobuf.Descriptors.FileDescriptor.Syntax; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -333,13 +334,13 @@ public final class Descriptors { // should get the original bytes that we want. // Literal strings are limited to 64k, so it may be split into multiple strings. if (strings.length == 1) { - return strings[0].getBytes(Internal.ISO_8859_1); + return strings[0].getBytes(StandardCharsets.ISO_8859_1); } StringBuilder descriptorData = new StringBuilder(); for (String part : strings) { descriptorData.append(part); } - return descriptorData.toString().getBytes(Internal.ISO_8859_1); + return descriptorData.toString().getBytes(StandardCharsets.ISO_8859_1); } private static FileDescriptor[] findDescriptors( diff --git a/java/core/src/main/java/com/google/protobuf/Internal.java b/java/core/src/main/java/com/google/protobuf/Internal.java index 07e8dd1322..1c7e7049ea 100644 --- a/java/core/src/main/java/com/google/protobuf/Internal.java +++ b/java/core/src/main/java/com/google/protobuf/Internal.java @@ -32,7 +32,7 @@ package com.google.protobuf; import java.lang.reflect.Method; import java.nio.ByteBuffer; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.AbstractList; import java.util.AbstractMap; import java.util.AbstractSet; @@ -54,10 +54,6 @@ public final class Internal { private Internal() {} - static final Charset US_ASCII = Charset.forName("US-ASCII"); - static final Charset UTF_8 = Charset.forName("UTF-8"); - static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); - /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */ static T checkNotNull(T obj) { if (obj == null) { @@ -97,7 +93,7 @@ public final class Internal { * actually want. The generated code calls this automatically. */ public static String stringDefaultValue(String bytes) { - return new String(bytes.getBytes(ISO_8859_1), UTF_8); + return new String(bytes.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); } /** @@ -108,7 +104,7 @@ public final class Internal { * ISO-8859-1 encoding. */ public static ByteString bytesDefaultValue(String bytes) { - return ByteString.copyFrom(bytes.getBytes(ISO_8859_1)); + return ByteString.copyFrom(bytes.getBytes(StandardCharsets.ISO_8859_1)); } /** * Helper called by generated code to construct default values for bytes fields. @@ -116,7 +112,7 @@ public final class Internal { *

This is like {@link #bytesDefaultValue}, but returns a byte array. */ public static byte[] byteArrayDefaultValue(String bytes) { - return bytes.getBytes(ISO_8859_1); + return bytes.getBytes(StandardCharsets.ISO_8859_1); } /** @@ -183,12 +179,12 @@ public final class Internal { /** Helper method to get the UTF-8 bytes of a string. */ public static byte[] toByteArray(String value) { - return value.getBytes(UTF_8); + return value.getBytes(StandardCharsets.UTF_8); } /** Helper method to convert a byte array to a string using UTF-8 encoding. */ public static String toStringUtf8(byte[] bytes) { - return new String(bytes, UTF_8); + return new String(bytes, StandardCharsets.UTF_8); } /** diff --git a/java/core/src/main/java/com/google/protobuf/MessageSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSchema.java index 4170f4fbe4..518f647e3e 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageSchema.java +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -76,6 +76,7 @@ import com.google.protobuf.Internal.ProtobufList; import com.google.protobuf.MapEntryLite.Metadata; import java.io.IOException; import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -4765,7 +4766,7 @@ final class MessageSchema implements Schema { && !Utf8.isValidUtf8(data, position, position + length)) { throw InvalidProtocolBufferException.invalidUtf8(); } - final String value = new String(data, position, length, Internal.UTF_8); + final String value = new String(data, position, length, StandardCharsets.UTF_8); unsafe.putObject(message, fieldOffset, value); position += length; } diff --git a/java/core/src/main/java/com/google/protobuf/Utf8.java b/java/core/src/main/java/com/google/protobuf/Utf8.java index 7c9133e16b..2a6023ccc0 100644 --- a/java/core/src/main/java/com/google/protobuf/Utf8.java +++ b/java/core/src/main/java/com/google/protobuf/Utf8.java @@ -42,6 +42,7 @@ import static java.lang.Character.isSurrogatePair; import static java.lang.Character.toCodePoint; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; /** * A set of low-level, high-performance static utility methods related to the UTF-8 character @@ -1386,7 +1387,7 @@ final class Utf8 { if (offset == limit) { // The entire byte sequence is ASCII. Don't bother copying to a char[], JVMs using // compact strings will just turn it back into the same byte[]. - return new String(bytes, index, size, Internal.US_ASCII); + return new String(bytes, index, size, StandardCharsets.US_ASCII); } // It's not all ASCII, at this point. This may over-allocate, but we will truncate in the diff --git a/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java index 656d2c3ab6..210134f0f0 100644 --- a/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -38,6 +38,8 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -68,7 +70,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Test public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); + ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertWithMessage("%s.substring() must have the expected type", classUnderTest) .that(classUnderTest) @@ -84,13 +86,13 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Test public void testCharsetToString() { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); + ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertWithMessage("%s.substring() must have the expected type", classUnderTest) .that(classUnderTest) .isEqualTo(getActualClassName(chopped)); - String roundTripString = chopped.toString(Internal.UTF_8); + String roundTripString = chopped.toString(StandardCharsets.UTF_8); assertWithMessage("%s unicode bytes must match", classUnderTest) .that(testString.substring(2, testString.length() - 6)) .isEqualTo(roundTripString); diff --git a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java index 3f97e3174f..5d1028a2e6 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java @@ -42,6 +42,7 @@ import java.io.OutputStream; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -198,7 +199,7 @@ public class ByteStringTest { public void testCopyFrom_Utf8() { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFromUtf8(testString); - byte[] testBytes = testString.getBytes(Internal.UTF_8); + byte[] testBytes = testString.getBytes(StandardCharsets.UTF_8); assertWithMessage("copyFromUtf8 string must respect the charset") .that(isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)) .isTrue(); @@ -516,7 +517,7 @@ public class ByteStringTest { @Test public void testToStringUtf8() { String testString = "I love unicode \u1234\u5678 characters"; - byte[] testBytes = testString.getBytes(Internal.UTF_8); + byte[] testBytes = testString.getBytes(StandardCharsets.UTF_8); ByteString byteString = ByteString.copyFrom(testBytes); assertWithMessage("copyToStringUtf8 must respect the charset") .that(testString) @@ -526,7 +527,7 @@ public class ByteStringTest { @Test public void testToString() { String toString = - ByteString.copyFrom("Here are some bytes: \t\u00a1".getBytes(Internal.UTF_8)).toString(); + ByteString.copyFrom("Here are some bytes: \t\u00a1".getBytes(StandardCharsets.UTF_8)).toString(); assertWithMessage(toString).that(toString.contains("size=24")).isTrue(); assertWithMessage(toString) .that(toString.contains("contents=\"Here are some bytes: \\t\\302\\241\"")) @@ -538,7 +539,7 @@ public class ByteStringTest { String toString = ByteString.copyFrom( "123456789012345678901234567890123456789012345678901234567890" - .getBytes(Internal.UTF_8)) + .getBytes(StandardCharsets.UTF_8)) .toString(); assertWithMessage(toString).that(toString.contains("size=60")).isTrue(); assertWithMessage(toString) diff --git a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index 9934ca19ff..5bc514f342 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -41,6 +41,7 @@ import protobuf_unittest.UnittestProto.TestSparseEnum; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -417,7 +418,7 @@ public class CodedOutputStreamTest { // Write some some bytes (more than the buffer can hold) and verify that totalWritten // is correct. - byte[] value = "abcde".getBytes(Internal.UTF_8); + byte[] value = "abcde".getBytes(StandardCharsets.UTF_8); for (int i = 0; i < 1024; ++i) { coder.stream().writeRawBytes(value, 0, value.length); } @@ -500,7 +501,7 @@ public class CodedOutputStreamTest { @Test public void testWriteByteBuffer() throws Exception { - byte[] value = "abcde".getBytes(Internal.UTF_8); + byte[] value = "abcde".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); ByteBuffer byteBuffer = ByteBuffer.wrap(value, 0, 1); @@ -543,7 +544,7 @@ public class CodedOutputStreamTest { for (int pos = 0; pos < source.length(); pos += 2) { String substr = source.substring(pos, pos + 2); expectedBytesStream.write(2); - expectedBytesStream.write(substr.getBytes(Internal.UTF_8)); + expectedBytesStream.write(substr.getBytes(StandardCharsets.UTF_8)); } final byte[] expectedBytes = expectedBytesStream.toByteArray(); diff --git a/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java b/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java index 5a345aa173..690d130ed9 100644 --- a/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java @@ -34,6 +34,7 @@ import com.google.protobuf.Utf8.Processor; import com.google.protobuf.Utf8.SafeProcessor; import com.google.protobuf.Utf8.UnsafeProcessor; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; @@ -196,7 +197,7 @@ public class DecodeUtf8Test extends TestCase { } public void testInvalidBufferSlice() throws Exception { - byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(Internal.UTF_8); + byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(StandardCharsets.UTF_8); assertInvalidSlice(bytes, bytes.length - 3, 4); assertInvalidSlice(bytes, bytes.length, 1); assertInvalidSlice(bytes, bytes.length + 1, 0); @@ -301,35 +302,35 @@ public class DecodeUtf8Test extends TestCase { } private void assertRoundTrips(String str, int index, int size) throws Exception { - byte[] bytes = str.getBytes(Internal.UTF_8); + byte[] bytes = str.getBytes(StandardCharsets.UTF_8); if (size == -1) { size = bytes.length; } assertDecode( - new String(bytes, index, size, Internal.UTF_8), + new String(bytes, index, size, StandardCharsets.UTF_8), UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size)); assertDecode( - new String(bytes, index, size, Internal.UTF_8), + new String(bytes, index, size, StandardCharsets.UTF_8), SAFE_PROCESSOR.decodeUtf8(bytes, index, size)); ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length); direct.put(bytes); direct.flip(); assertDecode( - new String(bytes, index, size, Internal.UTF_8), + new String(bytes, index, size, StandardCharsets.UTF_8), UNSAFE_PROCESSOR.decodeUtf8(direct, index, size)); assertDecode( - new String(bytes, index, size, Internal.UTF_8), + new String(bytes, index, size, StandardCharsets.UTF_8), SAFE_PROCESSOR.decodeUtf8(direct, index, size)); ByteBuffer heap = ByteBuffer.allocate(bytes.length); heap.put(bytes); heap.flip(); assertDecode( - new String(bytes, index, size, Internal.UTF_8), + new String(bytes, index, size, StandardCharsets.UTF_8), UNSAFE_PROCESSOR.decodeUtf8(heap, index, size)); assertDecode( - new String(bytes, index, size, Internal.UTF_8), + new String(bytes, index, size, StandardCharsets.UTF_8), SAFE_PROCESSOR.decodeUtf8(heap, index, size)); } diff --git a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java index 6cb0baebed..9e93b9f86c 100644 --- a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -63,6 +63,8 @@ import protobuf_unittest.UnittestProto.TestMultipleExtensionRanges; import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestReservedFields; import protobuf_unittest.UnittestProto.TestService; + +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import org.junit.Test; @@ -281,7 +283,7 @@ public class DescriptorsTest { assertThat(d.findFieldByName("escaped_bytes").getDefaultValue()) .isEqualTo( ByteString.copyFrom( - "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.ISO_8859_1))); + "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(StandardCharsets.ISO_8859_1))); assertThat(d.findFieldByName("large_uint32").getDefaultValue()).isEqualTo(-1); assertThat(d.findFieldByName("large_uint64").getDefaultValue()).isEqualTo(-1L); } diff --git a/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java index f1a671164d..0c24207fcd 100644 --- a/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -35,6 +35,7 @@ import static com.google.common.truth.Truth.assertWithMessage; import java.lang.ref.SoftReference; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -276,8 +277,8 @@ final class IsValidUtf8TestUtil { } ByteString bs = factory.newByteString(bytes); boolean isRoundTrippable = bs.isValidUtf8(); - String s = new String(bytes, Internal.UTF_8); - byte[] bytesReencoded = s.getBytes(Internal.UTF_8); + String s = new String(bytes, StandardCharsets.UTF_8); + byte[] bytesReencoded = s.getBytes(StandardCharsets.UTF_8); boolean bytesEqual = Arrays.equals(bytes, bytesReencoded); if (bytesEqual != isRoundTrippable) { diff --git a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 5ec4a937bb..573af2f016 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -43,6 +43,7 @@ import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; @@ -468,7 +469,7 @@ public class LiteralByteStringTest { @Test public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); + ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); String roundTripString = unicode.toString(UTF_8); assertWithMessage("%s unicode must match", classUnderTest) .that(testString) @@ -478,8 +479,8 @@ public class LiteralByteStringTest { @Test public void testCharsetToString() { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); - String roundTripString = unicode.toString(Internal.UTF_8); + ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); + String roundTripString = unicode.toString(StandardCharsets.UTF_8); assertWithMessage("%s unicode must match", classUnderTest) .that(testString) .isEqualTo(roundTripString); @@ -488,8 +489,8 @@ public class LiteralByteStringTest { @Test public void testToString_returnsCanonicalEmptyString() { assertWithMessage("%s must be the same string references", classUnderTest) - .that(ByteString.EMPTY.toString(Internal.UTF_8)) - .isSameInstanceAs(ByteString.wrap(new byte[] {}).toString(Internal.UTF_8)); + .that(ByteString.EMPTY.toString(StandardCharsets.UTF_8)) + .isSameInstanceAs(ByteString.wrap(new byte[] {}).toString(StandardCharsets.UTF_8)); } @Test diff --git a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java index 1f1427142b..d645b89359 100644 --- a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java @@ -32,7 +32,8 @@ package com.google.protobuf; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; -import static com.google.protobuf.Internal.UTF_8; + +import static java.nio.charset.StandardCharsets.UTF_8; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; diff --git a/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java b/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java index 0bfc0beaa3..445dbe229d 100644 --- a/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -33,6 +33,7 @@ package com.google.protobuf; import static com.google.common.truth.Truth.assertWithMessage; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import org.junit.Before; import org.junit.Test; @@ -132,7 +133,7 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { assertWithMessage("%s from string must have the expected type", classUnderTest) .that(classUnderTest) .isEqualTo(getActualClassName(unicode)); - String roundTripString = unicode.toString(Internal.UTF_8); + String roundTripString = unicode.toString(StandardCharsets.UTF_8); assertWithMessage("%s unicode bytes must match", classUnderTest) .that(testString) .isEqualTo(roundTripString); diff --git a/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java index bdb8132020..000c374556 100644 --- a/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -39,6 +39,7 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Iterator; import org.junit.Before; @@ -175,7 +176,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { assertWithMessage("%s from string must have the expected type", classUnderTest) .that(classUnderTest) .isEqualTo(getActualClassName(unicode)); - String roundTripString = unicode.toString(Internal.UTF_8); + String roundTripString = unicode.toString(StandardCharsets.UTF_8); assertWithMessage("%s unicode bytes must match", classUnderTest) .that(testString) .isEqualTo(roundTripString); @@ -194,8 +195,8 @@ public class RopeByteStringTest extends LiteralByteStringTest { RopeByteString ropeByteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); assertWithMessage("%s must be the same string references", classUnderTest) - .that(ByteString.EMPTY.toString(Internal.UTF_8)) - .isSameInstanceAs(ropeByteString.toString(Internal.UTF_8)); + .that(ByteString.EMPTY.toString(StandardCharsets.UTF_8)) + .isSameInstanceAs(ropeByteString.toString(StandardCharsets.UTF_8)); } @Override diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java index f9b7da46b7..c4fb591676 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -59,6 +59,7 @@ import protobuf_unittest.UnittestProto.TestOneof2; import protobuf_unittest.UnittestProto.TestRequired; import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.logging.Logger; @@ -277,7 +278,7 @@ public class TextFormatTest { * are converted directly to bytes, *not* encoded using UTF-8. */ private ByteString bytes(String str) { - return ByteString.copyFrom(str.getBytes(Internal.ISO_8859_1)); + return ByteString.copyFrom(str.getBytes(StandardCharsets.ISO_8859_1)); } /** diff --git a/java/core/src/test/java/com/google/protobuf/Utf8Test.java b/java/core/src/test/java/com/google/protobuf/Utf8Test.java index 89f080ef36..5fdc61b5e5 100644 --- a/java/core/src/test/java/com/google/protobuf/Utf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/Utf8Test.java @@ -34,6 +34,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Random; import org.junit.Test; import org.junit.runner.RunWith; @@ -133,7 +134,7 @@ public class Utf8Test { } private static void assertEncoding(String message) { - byte[] expected = message.getBytes(Internal.UTF_8); + byte[] expected = message.getBytes(StandardCharsets.UTF_8); byte[] output = encodeToByteArray(message, expected.length, safeProcessor); assertWithMessage("encodeUtf8[ARRAY]") .that(output).isEqualTo(expected); diff --git a/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java index fc1e09f10e..3f416263f9 100644 --- a/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java +++ b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java @@ -32,6 +32,8 @@ package com.google.protobuf; import static com.google.common.truth.Truth.assertThat; +import java.nio.charset.StandardCharsets; + import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,7 +53,7 @@ public class WrappersLiteOfMethodTest { builder.setFieldUint64(UInt64Value.of(23333333333333L)); builder.setFieldBool(BoolValue.of(true)); builder.setFieldString(StringValue.of("23333")); - builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.UTF_8)))); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(StandardCharsets.UTF_8)))); TopLevelMessage message = builder.build(); assertThat(message.getFieldDouble().getValue()).isEqualTo(2.333); diff --git a/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java index 44c4ad6e6e..0106987215 100644 --- a/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java +++ b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java @@ -32,6 +32,8 @@ package com.google.protobuf; import static com.google.common.truth.Truth.assertThat; +import java.nio.charset.StandardCharsets; + import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,7 +53,7 @@ public class WrappersOfMethodTest { builder.setFieldUint64(UInt64Value.of(23333333333333L)); builder.setFieldBool(BoolValue.of(true)); builder.setFieldString(StringValue.of("23333")); - builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.UTF_8)))); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(StandardCharsets.UTF_8)))); TopLevelMessage message = builder.build(); assertThat(message.getFieldDouble().getValue()).isEqualTo(2.333); diff --git a/java/lite/src/test/java/com/google/protobuf/LiteTest.java b/java/lite/src/test/java/com/google/protobuf/LiteTest.java index b0972111ac..a42ab31c27 100644 --- a/java/lite/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/lite/src/test/java/com/google/protobuf/LiteTest.java @@ -68,6 +68,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -1720,7 +1721,7 @@ public class LiteTest { public void testMergeFromStream_invalidBytes() throws Exception { TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder().setDefaultBool(true); try { - builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(Internal.UTF_8))); + builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(StandardCharsets.UTF_8))); assertWithMessage("expected exception").fail(); } catch (InvalidProtocolBufferException expected) { }