From d8ccfbf00541eb0705bbba640d65e3df814b3d2a Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 7 Jan 2022 18:19:51 +0000 Subject: [PATCH] Revert "Transition to NIO StandardCharsets" (#9382) --- .../java/com/google/protobuf/ArrayDecoders.java | 11 +++++------ .../java/com/google/protobuf/BinaryReader.java | 3 +-- .../java/com/google/protobuf/ByteString.java | 5 ++--- .../com/google/protobuf/CodedInputStream.java | 3 +-- .../com/google/protobuf/CodedOutputStream.java | 6 +++--- .../java/com/google/protobuf/Descriptors.java | 5 ++--- .../main/java/com/google/protobuf/Internal.java | 16 ++++++++++------ .../java/com/google/protobuf/MessageSchema.java | 3 +-- .../src/main/java/com/google/protobuf/Utf8.java | 3 +-- .../google/protobuf/BoundedByteStringTest.java | 8 +++----- .../com/google/protobuf/ByteStringTest.java | 9 ++++----- .../google/protobuf/CodedOutputStreamTest.java | 7 +++---- .../com/google/protobuf/DecodeUtf8Test.java | 17 ++++++++--------- .../com/google/protobuf/DescriptorsTest.java | 4 +--- .../google/protobuf/IsValidUtf8TestUtil.java | 5 ++--- .../google/protobuf/LiteralByteStringTest.java | 11 +++++------ .../com/google/protobuf/NioByteStringTest.java | 3 +-- .../protobuf/RopeByteStringSubstringTest.java | 3 +-- .../com/google/protobuf/RopeByteStringTest.java | 7 +++---- .../com/google/protobuf/TextFormatTest.java | 3 +-- .../test/java/com/google/protobuf/Utf8Test.java | 3 +-- .../protobuf/WrappersLiteOfMethodTest.java | 4 +--- .../google/protobuf/WrappersOfMethodTest.java | 4 +--- .../test/java/com/google/protobuf/LiteTest.java | 3 +-- 24 files changed, 62 insertions(+), 84 deletions(-) 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 0471afaf1f96..1217e112e0ce 100644 --- a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java +++ b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java @@ -34,7 +34,6 @@ 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. @@ -192,7 +191,7 @@ static int decodeString(byte[] data, int position, Registers registers) registers.object1 = ""; return position; } else { - registers.object1 = new String(data, position, length, StandardCharsets.UTF_8); + registers.object1 = new String(data, position, length, Internal.UTF_8); return position + length; } } @@ -578,7 +577,7 @@ static int decodeStringList( } else if (length == 0) { output.add(""); } else { - String value = new String(data, position, length, StandardCharsets.UTF_8); + String value = new String(data, position, length, Internal.UTF_8); output.add(value); position += length; } @@ -594,7 +593,7 @@ static int decodeStringList( } else if (nextLength == 0) { output.add(""); } else { - String value = new String(data, position, nextLength, StandardCharsets.UTF_8); + String value = new String(data, position, nextLength, Internal.UTF_8); output.add(value); position += nextLength; } @@ -620,7 +619,7 @@ static int decodeStringListRequireUtf8( if (!Utf8.isValidUtf8(data, position, position + length)) { throw InvalidProtocolBufferException.invalidUtf8(); } - String value = new String(data, position, length, StandardCharsets.UTF_8); + String value = new String(data, position, length, Internal.UTF_8); output.add(value); position += length; } @@ -639,7 +638,7 @@ static int decodeStringListRequireUtf8( if (!Utf8.isValidUtf8(data, position, position + nextLength)) { throw InvalidProtocolBufferException.invalidUtf8(); } - String value = new String(data, position, nextLength, StandardCharsets.UTF_8); + String value = new String(data, position, nextLength, Internal.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 547e78c0141c..d64574c2a581 100644 --- a/java/core/src/main/java/com/google/protobuf/BinaryReader.java +++ b/java/core/src/main/java/com/google/protobuf/BinaryReader.java @@ -41,7 +41,6 @@ import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -227,7 +226,7 @@ public String readStringInternal(boolean requireUtf8) throws IOException { if (requireUtf8 && !Utf8.isValidUtf8(buffer, pos, pos + size)) { throw InvalidProtocolBufferException.invalidUtf8(); } - String result = new String(buffer, pos, size, StandardCharsets.UTF_8); + String result = new String(buffer, pos, size, Internal.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 c42b28c4f2e0..a4beaeb4cffb 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteString.java +++ b/java/core/src/main/java/com/google/protobuf/ByteString.java @@ -45,7 +45,6 @@ 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; @@ -461,7 +460,7 @@ public static ByteString copyFrom(String text, Charset charset) { * @return new {@code ByteString} */ public static ByteString copyFromUtf8(String text) { - return new LiteralByteString(text.getBytes(StandardCharsets.UTF_8)); + return new LiteralByteString(text.getBytes(Internal.UTF_8)); } // ================================================================= @@ -834,7 +833,7 @@ public final String toString(Charset charset) { * @return new string using UTF-8 encoding */ public final String toStringUtf8() { - return toString(StandardCharsets.UTF_8); + return toString(Internal.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 ad87d4e05c42..6e9c0f62091a 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java @@ -32,13 +32,12 @@ 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 65ea62704400..12f2097a8f50 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -42,7 +42,6 @@ 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; @@ -843,7 +842,7 @@ public static int computeStringSizeNoTag(final String value) { length = Utf8.encodedLength(value); } catch (UnpairedSurrogateException e) { // TODO(dweis): Consider using nio Charset methods instead. - final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); + final byte[] bytes = value.getBytes(Internal.UTF_8); length = bytes.length; } @@ -990,7 +989,8 @@ final void inefficientWriteStringNoTag(String value, UnpairedSurrogateException // 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(StandardCharsets.UTF_8); + // TODO(dweis): Consider using nio Charset methods instead. + final byte[] bytes = value.getBytes(Internal.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 c3b6dc09e0e7..f36d03332773 100644 --- a/java/core/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/core/src/main/java/com/google/protobuf/Descriptors.java @@ -51,7 +51,6 @@ 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; @@ -334,13 +333,13 @@ private static byte[] latin1Cat(final String[] strings) { // 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(StandardCharsets.ISO_8859_1); + return strings[0].getBytes(Internal.ISO_8859_1); } StringBuilder descriptorData = new StringBuilder(); for (String part : strings) { descriptorData.append(part); } - return descriptorData.toString().getBytes(StandardCharsets.ISO_8859_1); + return descriptorData.toString().getBytes(Internal.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 1c7e7049ea3b..07e8dd132257 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 @@ import java.lang.reflect.Method; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.AbstractList; import java.util.AbstractMap; import java.util.AbstractSet; @@ -54,6 +54,10 @@ 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) { @@ -93,7 +97,7 @@ static T checkNotNull(T obj, String message) { * actually want. The generated code calls this automatically. */ public static String stringDefaultValue(String bytes) { - return new String(bytes.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); + return new String(bytes.getBytes(ISO_8859_1), UTF_8); } /** @@ -104,7 +108,7 @@ public static String stringDefaultValue(String bytes) { * ISO-8859-1 encoding. */ public static ByteString bytesDefaultValue(String bytes) { - return ByteString.copyFrom(bytes.getBytes(StandardCharsets.ISO_8859_1)); + return ByteString.copyFrom(bytes.getBytes(ISO_8859_1)); } /** * Helper called by generated code to construct default values for bytes fields. @@ -112,7 +116,7 @@ public static ByteString bytesDefaultValue(String bytes) { *

This is like {@link #bytesDefaultValue}, but returns a byte array. */ public static byte[] byteArrayDefaultValue(String bytes) { - return bytes.getBytes(StandardCharsets.ISO_8859_1); + return bytes.getBytes(ISO_8859_1); } /** @@ -179,12 +183,12 @@ public static boolean isValidUtf8(byte[] byteArray) { /** Helper method to get the UTF-8 bytes of a string. */ public static byte[] toByteArray(String value) { - return value.getBytes(StandardCharsets.UTF_8); + return value.getBytes(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, StandardCharsets.UTF_8); + return new String(bytes, 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 518f647e3ede..4170f4fbe499 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageSchema.java +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -76,7 +76,6 @@ 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; @@ -4766,7 +4765,7 @@ private int parseOneofField( && !Utf8.isValidUtf8(data, position, position + length)) { throw InvalidProtocolBufferException.invalidUtf8(); } - final String value = new String(data, position, length, StandardCharsets.UTF_8); + final String value = new String(data, position, length, Internal.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 2a6023ccc0b1..7c9133e16b32 100644 --- a/java/core/src/main/java/com/google/protobuf/Utf8.java +++ b/java/core/src/main/java/com/google/protobuf/Utf8.java @@ -42,7 +42,6 @@ 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 @@ -1387,7 +1386,7 @@ String decodeUtf8(byte[] bytes, int index, int size) throws InvalidProtocolBuffe 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, StandardCharsets.US_ASCII); + return new String(bytes, index, size, Internal.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 210134f0f0ad..656d2c3ab6db 100644 --- a/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -38,8 +38,6 @@ 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; @@ -70,7 +68,7 @@ public void setUp() throws Exception { @Test public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); + ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertWithMessage("%s.substring() must have the expected type", classUnderTest) .that(classUnderTest) @@ -86,13 +84,13 @@ public void testToString() throws UnsupportedEncodingException { @Test public void testCharsetToString() { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); + ByteString unicode = ByteString.wrap(testString.getBytes(Internal.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(StandardCharsets.UTF_8); + String roundTripString = chopped.toString(Internal.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 5d1028a2e6a1..3f97e3174ffe 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java @@ -42,7 +42,6 @@ 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; @@ -199,7 +198,7 @@ public void testCopyFrom_StringEncoding() { public void testCopyFrom_Utf8() { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFromUtf8(testString); - byte[] testBytes = testString.getBytes(StandardCharsets.UTF_8); + byte[] testBytes = testString.getBytes(Internal.UTF_8); assertWithMessage("copyFromUtf8 string must respect the charset") .that(isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)) .isTrue(); @@ -517,7 +516,7 @@ public void write(int ignored) { @Test public void testToStringUtf8() { String testString = "I love unicode \u1234\u5678 characters"; - byte[] testBytes = testString.getBytes(StandardCharsets.UTF_8); + byte[] testBytes = testString.getBytes(Internal.UTF_8); ByteString byteString = ByteString.copyFrom(testBytes); assertWithMessage("copyToStringUtf8 must respect the charset") .that(testString) @@ -527,7 +526,7 @@ public void testToStringUtf8() { @Test public void testToString() { String toString = - ByteString.copyFrom("Here are some bytes: \t\u00a1".getBytes(StandardCharsets.UTF_8)).toString(); + ByteString.copyFrom("Here are some bytes: \t\u00a1".getBytes(Internal.UTF_8)).toString(); assertWithMessage(toString).that(toString.contains("size=24")).isTrue(); assertWithMessage(toString) .that(toString.contains("contents=\"Here are some bytes: \\t\\302\\241\"")) @@ -539,7 +538,7 @@ public void testToString_long() { String toString = ByteString.copyFrom( "123456789012345678901234567890123456789012345678901234567890" - .getBytes(StandardCharsets.UTF_8)) + .getBytes(Internal.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 5bc514f342de..9934ca19ff96 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -41,7 +41,6 @@ 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; @@ -418,7 +417,7 @@ public void testGetTotalBytesWritten() throws Exception { // Write some some bytes (more than the buffer can hold) and verify that totalWritten // is correct. - byte[] value = "abcde".getBytes(StandardCharsets.UTF_8); + byte[] value = "abcde".getBytes(Internal.UTF_8); for (int i = 0; i < 1024; ++i) { coder.stream().writeRawBytes(value, 0, value.length); } @@ -501,7 +500,7 @@ public void testWriteToByteBuffer() throws Exception { @Test public void testWriteByteBuffer() throws Exception { - byte[] value = "abcde".getBytes(StandardCharsets.UTF_8); + byte[] value = "abcde".getBytes(Internal.UTF_8); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); ByteBuffer byteBuffer = ByteBuffer.wrap(value, 0, 1); @@ -544,7 +543,7 @@ public void testSerializeUtf8_MultipleSmallWrites() throws Exception { for (int pos = 0; pos < source.length(); pos += 2) { String substr = source.substring(pos, pos + 2); expectedBytesStream.write(2); - expectedBytesStream.write(substr.getBytes(StandardCharsets.UTF_8)); + expectedBytesStream.write(substr.getBytes(Internal.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 690d130ed94c..5a345aa173fd 100644 --- a/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java @@ -34,7 +34,6 @@ 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; @@ -197,7 +196,7 @@ public void testBufferSlice() throws Exception { } public void testInvalidBufferSlice() throws Exception { - byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(StandardCharsets.UTF_8); + byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(Internal.UTF_8); assertInvalidSlice(bytes, bytes.length - 3, 4); assertInvalidSlice(bytes, bytes.length, 1); assertInvalidSlice(bytes, bytes.length + 1, 0); @@ -302,35 +301,35 @@ private void assertRoundTrips(String str) throws Exception { } private void assertRoundTrips(String str, int index, int size) throws Exception { - byte[] bytes = str.getBytes(StandardCharsets.UTF_8); + byte[] bytes = str.getBytes(Internal.UTF_8); if (size == -1) { size = bytes.length; } assertDecode( - new String(bytes, index, size, StandardCharsets.UTF_8), + new String(bytes, index, size, Internal.UTF_8), UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size)); assertDecode( - new String(bytes, index, size, StandardCharsets.UTF_8), + new String(bytes, index, size, Internal.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, StandardCharsets.UTF_8), + new String(bytes, index, size, Internal.UTF_8), UNSAFE_PROCESSOR.decodeUtf8(direct, index, size)); assertDecode( - new String(bytes, index, size, StandardCharsets.UTF_8), + new String(bytes, index, size, Internal.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, StandardCharsets.UTF_8), + new String(bytes, index, size, Internal.UTF_8), UNSAFE_PROCESSOR.decodeUtf8(heap, index, size)); assertDecode( - new String(bytes, index, size, StandardCharsets.UTF_8), + new String(bytes, index, size, Internal.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 9e93b9f86cbb..6cb0baebedb5 100644 --- a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -63,8 +63,6 @@ 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; @@ -283,7 +281,7 @@ public void testFieldDescriptorDefault() throws Exception { assertThat(d.findFieldByName("escaped_bytes").getDefaultValue()) .isEqualTo( ByteString.copyFrom( - "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(StandardCharsets.ISO_8859_1))); + "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.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 0c24207fcd72..f1a671164d18 100644 --- a/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -35,7 +35,6 @@ 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; @@ -277,8 +276,8 @@ static void testBytes( } ByteString bs = factory.newByteString(bytes); boolean isRoundTrippable = bs.isValidUtf8(); - String s = new String(bytes, StandardCharsets.UTF_8); - byte[] bytesReencoded = s.getBytes(StandardCharsets.UTF_8); + String s = new String(bytes, Internal.UTF_8); + byte[] bytesReencoded = s.getBytes(Internal.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 573af2f01690..5ec4a937bb40 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -43,7 +43,6 @@ 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; @@ -469,7 +468,7 @@ public void testNewOutput() throws IOException { @Test public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); + ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); String roundTripString = unicode.toString(UTF_8); assertWithMessage("%s unicode must match", classUnderTest) .that(testString) @@ -479,8 +478,8 @@ public void testToString() throws UnsupportedEncodingException { @Test public void testCharsetToString() { String testString = "I love unicode \u1234\u5678 characters"; - ByteString unicode = ByteString.wrap(testString.getBytes(StandardCharsets.UTF_8)); - String roundTripString = unicode.toString(StandardCharsets.UTF_8); + ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); + String roundTripString = unicode.toString(Internal.UTF_8); assertWithMessage("%s unicode must match", classUnderTest) .that(testString) .isEqualTo(roundTripString); @@ -489,8 +488,8 @@ public void testCharsetToString() { @Test public void testToString_returnsCanonicalEmptyString() { assertWithMessage("%s must be the same string references", classUnderTest) - .that(ByteString.EMPTY.toString(StandardCharsets.UTF_8)) - .isSameInstanceAs(ByteString.wrap(new byte[] {}).toString(StandardCharsets.UTF_8)); + .that(ByteString.EMPTY.toString(Internal.UTF_8)) + .isSameInstanceAs(ByteString.wrap(new byte[] {}).toString(Internal.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 d645b8935929..1f1427142bb2 100644 --- a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java @@ -32,8 +32,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; - -import static java.nio.charset.StandardCharsets.UTF_8; +import static com.google.protobuf.Internal.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 445dbe229d09..0bfc0beaa355 100644 --- a/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -33,7 +33,6 @@ 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; @@ -133,7 +132,7 @@ public void testCharsetToString() { assertWithMessage("%s from string must have the expected type", classUnderTest) .that(classUnderTest) .isEqualTo(getActualClassName(unicode)); - String roundTripString = unicode.toString(StandardCharsets.UTF_8); + String roundTripString = unicode.toString(Internal.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 000c374556a9..bdb813202051 100644 --- a/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -39,7 +39,6 @@ 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; @@ -176,7 +175,7 @@ public void testCharsetToString() { assertWithMessage("%s from string must have the expected type", classUnderTest) .that(classUnderTest) .isEqualTo(getActualClassName(unicode)); - String roundTripString = unicode.toString(StandardCharsets.UTF_8); + String roundTripString = unicode.toString(Internal.UTF_8); assertWithMessage("%s unicode bytes must match", classUnderTest) .that(testString) .isEqualTo(roundTripString); @@ -195,8 +194,8 @@ public void testToString_returnsCanonicalEmptyString() { RopeByteString ropeByteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); assertWithMessage("%s must be the same string references", classUnderTest) - .that(ByteString.EMPTY.toString(StandardCharsets.UTF_8)) - .isSameInstanceAs(ropeByteString.toString(StandardCharsets.UTF_8)); + .that(ByteString.EMPTY.toString(Internal.UTF_8)) + .isSameInstanceAs(ropeByteString.toString(Internal.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 c4fb5916760c..f9b7da46b7cd 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -59,7 +59,6 @@ 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; @@ -278,7 +277,7 @@ public void testPrintField() throws Exception { * are converted directly to bytes, *not* encoded using UTF-8. */ private ByteString bytes(String str) { - return ByteString.copyFrom(str.getBytes(StandardCharsets.ISO_8859_1)); + return ByteString.copyFrom(str.getBytes(Internal.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 787cb11cb93f..44f7cf136d46 100644 --- a/java/core/src/test/java/com/google/protobuf/Utf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/Utf8Test.java @@ -34,7 +34,6 @@ 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; @@ -134,7 +133,7 @@ private static void assertIsValid(byte[] data, boolean valid) { } private static void assertEncoding(String message) { - byte[] expected = message.getBytes(StandardCharsets.UTF_8); + byte[] expected = message.getBytes(Internal.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 3f416263f9f4..fc1e09f10ef0 100644 --- a/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java +++ b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java @@ -32,8 +32,6 @@ 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; @@ -53,7 +51,7 @@ public void testOf() throws Exception { builder.setFieldUint64(UInt64Value.of(23333333333333L)); builder.setFieldBool(BoolValue.of(true)); builder.setFieldString(StringValue.of("23333")); - builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(StandardCharsets.UTF_8)))); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.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 010698721519..44c4ad6e6e58 100644 --- a/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java +++ b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java @@ -32,8 +32,6 @@ 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; @@ -53,7 +51,7 @@ public void testOf() throws Exception { builder.setFieldUint64(UInt64Value.of(23333333333333L)); builder.setFieldBool(BoolValue.of(true)); builder.setFieldString(StringValue.of("23333")); - builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(StandardCharsets.UTF_8)))); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.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 a42ab31c27d1..b0972111acd8 100644 --- a/java/lite/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/lite/src/test/java/com/google/protobuf/LiteTest.java @@ -68,7 +68,6 @@ 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; @@ -1721,7 +1720,7 @@ public void testMergeFromStream_repeatedField() throws Exception { public void testMergeFromStream_invalidBytes() throws Exception { TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder().setDefaultBool(true); try { - builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(StandardCharsets.UTF_8))); + builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(Internal.UTF_8))); assertWithMessage("expected exception").fail(); } catch (InvalidProtocolBufferException expected) { }