Revert "Transition to NIO StandardCharsets" (#9382)

pull/9386/head
Elliotte Rusty Harold 3 years ago committed by GitHub
parent ee648b76a7
commit d8ccfbf005
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      java/core/src/main/java/com/google/protobuf/ArrayDecoders.java
  2. 3
      java/core/src/main/java/com/google/protobuf/BinaryReader.java
  3. 5
      java/core/src/main/java/com/google/protobuf/ByteString.java
  4. 3
      java/core/src/main/java/com/google/protobuf/CodedInputStream.java
  5. 6
      java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
  6. 5
      java/core/src/main/java/com/google/protobuf/Descriptors.java
  7. 16
      java/core/src/main/java/com/google/protobuf/Internal.java
  8. 3
      java/core/src/main/java/com/google/protobuf/MessageSchema.java
  9. 3
      java/core/src/main/java/com/google/protobuf/Utf8.java
  10. 8
      java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java
  11. 9
      java/core/src/test/java/com/google/protobuf/ByteStringTest.java
  12. 7
      java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java
  13. 17
      java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java
  14. 4
      java/core/src/test/java/com/google/protobuf/DescriptorsTest.java
  15. 5
      java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java
  16. 11
      java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java
  17. 3
      java/core/src/test/java/com/google/protobuf/NioByteStringTest.java
  18. 3
      java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java
  19. 7
      java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java
  20. 3
      java/core/src/test/java/com/google/protobuf/TextFormatTest.java
  21. 3
      java/core/src/test/java/com/google/protobuf/Utf8Test.java
  22. 4
      java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java
  23. 4
      java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java
  24. 3
      java/lite/src/test/java/com/google/protobuf/LiteTest.java

@ -34,7 +34,6 @@ import static com.google.protobuf.MessageSchema.getMutableUnknownFields;
import com.google.protobuf.Internal.ProtobufList; import com.google.protobuf.Internal.ProtobufList;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
/** /**
* Helper functions to decode protobuf wire format from a byte array. * Helper functions to decode protobuf wire format from a byte array.
@ -192,7 +191,7 @@ final class ArrayDecoders {
registers.object1 = ""; registers.object1 = "";
return position; return position;
} else { } else {
registers.object1 = new String(data, position, length, StandardCharsets.UTF_8); registers.object1 = new String(data, position, length, Internal.UTF_8);
return position + length; return position + length;
} }
} }
@ -578,7 +577,7 @@ final class ArrayDecoders {
} else if (length == 0) { } else if (length == 0) {
output.add(""); output.add("");
} else { } else {
String value = new String(data, position, length, StandardCharsets.UTF_8); String value = new String(data, position, length, Internal.UTF_8);
output.add(value); output.add(value);
position += length; position += length;
} }
@ -594,7 +593,7 @@ final class ArrayDecoders {
} else if (nextLength == 0) { } else if (nextLength == 0) {
output.add(""); output.add("");
} else { } else {
String value = new String(data, position, nextLength, StandardCharsets.UTF_8); String value = new String(data, position, nextLength, Internal.UTF_8);
output.add(value); output.add(value);
position += nextLength; position += nextLength;
} }
@ -620,7 +619,7 @@ final class ArrayDecoders {
if (!Utf8.isValidUtf8(data, position, position + length)) { if (!Utf8.isValidUtf8(data, position, position + length)) {
throw InvalidProtocolBufferException.invalidUtf8(); 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); output.add(value);
position += length; position += length;
} }
@ -639,7 +638,7 @@ final class ArrayDecoders {
if (!Utf8.isValidUtf8(data, position, position + nextLength)) { if (!Utf8.isValidUtf8(data, position, position + nextLength)) {
throw InvalidProtocolBufferException.invalidUtf8(); 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); output.add(value);
position += nextLength; position += nextLength;
} }

@ -41,7 +41,6 @@ import static com.google.protobuf.WireFormat.WIRETYPE_VARINT;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -227,7 +226,7 @@ abstract class BinaryReader implements Reader {
if (requireUtf8 && !Utf8.isValidUtf8(buffer, pos, pos + size)) { if (requireUtf8 && !Utf8.isValidUtf8(buffer, pos, pos + size)) {
throw InvalidProtocolBufferException.invalidUtf8(); 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; pos += size;
return result; return result;
} }

@ -45,7 +45,6 @@ import java.io.Serializable;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -461,7 +460,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
* @return new {@code ByteString} * @return new {@code ByteString}
*/ */
public static ByteString copyFromUtf8(String text) { 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 abstract class ByteString implements Iterable<Byte>, Serializable {
* @return new string using UTF-8 encoding * @return new string using UTF-8 encoding
*/ */
public final String toStringUtf8() { public final String toStringUtf8() {
return toString(StandardCharsets.UTF_8); return toString(Internal.UTF_8);
} }
/** /**

@ -32,13 +32,12 @@ package com.google.protobuf;
import static com.google.protobuf.Internal.EMPTY_BYTE_ARRAY; import static com.google.protobuf.Internal.EMPTY_BYTE_ARRAY;
import static com.google.protobuf.Internal.EMPTY_BYTE_BUFFER; 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.Internal.checkNotNull;
import static com.google.protobuf.WireFormat.FIXED32_SIZE; import static com.google.protobuf.WireFormat.FIXED32_SIZE;
import static com.google.protobuf.WireFormat.FIXED64_SIZE; import static com.google.protobuf.WireFormat.FIXED64_SIZE;
import static com.google.protobuf.WireFormat.MAX_VARINT_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.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;

@ -42,7 +42,6 @@ import java.io.OutputStream;
import java.nio.BufferOverflowException; import java.nio.BufferOverflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -843,7 +842,7 @@ public abstract class CodedOutputStream extends ByteOutput {
length = Utf8.encodedLength(value); length = Utf8.encodedLength(value);
} catch (UnpairedSurrogateException e) { } catch (UnpairedSurrogateException e) {
// TODO(dweis): Consider using nio Charset methods instead. // 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; length = bytes.length;
} }
@ -990,7 +989,8 @@ public abstract class CodedOutputStream extends ByteOutput {
// 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(StandardCharsets.UTF_8); // TODO(dweis): Consider using nio Charset methods instead.
final byte[] bytes = value.getBytes(Internal.UTF_8);
try { try {
writeUInt32NoTag(bytes.length); writeUInt32NoTag(bytes.length);
writeLazy(bytes, 0, bytes.length); writeLazy(bytes, 0, bytes.length);

@ -51,7 +51,6 @@ import com.google.protobuf.DescriptorProtos.ServiceOptions;
import com.google.protobuf.Descriptors.FileDescriptor.Syntax; import com.google.protobuf.Descriptors.FileDescriptor.Syntax;
import java.lang.ref.ReferenceQueue; import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -334,13 +333,13 @@ public final class Descriptors {
// should get the original bytes that we want. // should get the original bytes that we want.
// Literal strings are limited to 64k, so it may be split into multiple strings. // Literal strings are limited to 64k, so it may be split into multiple strings.
if (strings.length == 1) { if (strings.length == 1) {
return strings[0].getBytes(StandardCharsets.ISO_8859_1); return strings[0].getBytes(Internal.ISO_8859_1);
} }
StringBuilder descriptorData = new StringBuilder(); StringBuilder descriptorData = new StringBuilder();
for (String part : strings) { for (String part : strings) {
descriptorData.append(part); descriptorData.append(part);
} }
return descriptorData.toString().getBytes(StandardCharsets.ISO_8859_1); return descriptorData.toString().getBytes(Internal.ISO_8859_1);
} }
private static FileDescriptor[] findDescriptors( private static FileDescriptor[] findDescriptors(

@ -32,7 +32,7 @@ package com.google.protobuf;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.Charset;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.AbstractSet; import java.util.AbstractSet;
@ -54,6 +54,10 @@ public final class Internal {
private 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}. */ /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */
static <T> T checkNotNull(T obj) { static <T> T checkNotNull(T obj) {
if (obj == null) { if (obj == null) {
@ -93,7 +97,7 @@ public final class Internal {
* actually want. The generated code calls this automatically. * actually want. The generated code calls this automatically.
*/ */
public static String stringDefaultValue(String bytes) { 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 final class Internal {
* ISO-8859-1 encoding. * ISO-8859-1 encoding.
*/ */
public static ByteString bytesDefaultValue(String bytes) { 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. * Helper called by generated code to construct default values for bytes fields.
@ -112,7 +116,7 @@ public final class Internal {
* <p>This is like {@link #bytesDefaultValue}, but returns a byte array. * <p>This is like {@link #bytesDefaultValue}, but returns a byte array.
*/ */
public static byte[] byteArrayDefaultValue(String bytes) { public static byte[] byteArrayDefaultValue(String bytes) {
return bytes.getBytes(StandardCharsets.ISO_8859_1); return bytes.getBytes(ISO_8859_1);
} }
/** /**
@ -179,12 +183,12 @@ public final class Internal {
/** Helper method to get the UTF-8 bytes of a string. */ /** Helper method to get the UTF-8 bytes of a string. */
public static byte[] toByteArray(String value) { 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. */ /** Helper method to convert a byte array to a string using UTF-8 encoding. */
public static String toStringUtf8(byte[] bytes) { public static String toStringUtf8(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8); return new String(bytes, UTF_8);
} }
/** /**

@ -76,7 +76,6 @@ import com.google.protobuf.Internal.ProtobufList;
import com.google.protobuf.MapEntryLite.Metadata; import com.google.protobuf.MapEntryLite.Metadata;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -4766,7 +4765,7 @@ final class MessageSchema<T> implements Schema<T> {
&& !Utf8.isValidUtf8(data, position, position + length)) { && !Utf8.isValidUtf8(data, position, position + length)) {
throw InvalidProtocolBufferException.invalidUtf8(); 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); unsafe.putObject(message, fieldOffset, value);
position += length; position += length;
} }

@ -42,7 +42,6 @@ import static java.lang.Character.isSurrogatePair;
import static java.lang.Character.toCodePoint; import static java.lang.Character.toCodePoint;
import java.nio.ByteBuffer; 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 * A set of low-level, high-performance static utility methods related to the UTF-8 character
@ -1387,7 +1386,7 @@ final class Utf8 {
if (offset == limit) { if (offset == limit) {
// The entire byte sequence is ASCII. Don't bother copying to a char[], JVMs using // 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[]. // 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 // It's not all ASCII, at this point. This may over-allocate, but we will truncate in the

@ -38,8 +38,6 @@ import java.io.InputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -70,7 +68,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Test @Test
public void testToString() throws UnsupportedEncodingException { public void testToString() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters"; 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); ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertWithMessage("%s.substring() must have the expected type", classUnderTest) assertWithMessage("%s.substring() must have the expected type", classUnderTest)
.that(classUnderTest) .that(classUnderTest)
@ -86,13 +84,13 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Test @Test
public void testCharsetToString() { public void testCharsetToString() {
String testString = "I love unicode \u1234\u5678 characters"; 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); ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertWithMessage("%s.substring() must have the expected type", classUnderTest) assertWithMessage("%s.substring() must have the expected type", classUnderTest)
.that(classUnderTest) .that(classUnderTest)
.isEqualTo(getActualClassName(chopped)); .isEqualTo(getActualClassName(chopped));
String roundTripString = chopped.toString(StandardCharsets.UTF_8); String roundTripString = chopped.toString(Internal.UTF_8);
assertWithMessage("%s unicode bytes must match", classUnderTest) assertWithMessage("%s unicode bytes must match", classUnderTest)
.that(testString.substring(2, testString.length() - 6)) .that(testString.substring(2, testString.length() - 6))
.isEqualTo(roundTripString); .isEqualTo(roundTripString);

@ -42,7 +42,6 @@ import java.io.OutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -199,7 +198,7 @@ public class ByteStringTest {
public void testCopyFrom_Utf8() { public void testCopyFrom_Utf8() {
String testString = "I love unicode \u1234\u5678 characters"; String testString = "I love unicode \u1234\u5678 characters";
ByteString byteString = ByteString.copyFromUtf8(testString); 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") assertWithMessage("copyFromUtf8 string must respect the charset")
.that(isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)) .that(isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length))
.isTrue(); .isTrue();
@ -517,7 +516,7 @@ public class ByteStringTest {
@Test @Test
public void testToStringUtf8() { public void testToStringUtf8() {
String testString = "I love unicode \u1234\u5678 characters"; 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); ByteString byteString = ByteString.copyFrom(testBytes);
assertWithMessage("copyToStringUtf8 must respect the charset") assertWithMessage("copyToStringUtf8 must respect the charset")
.that(testString) .that(testString)
@ -527,7 +526,7 @@ public class ByteStringTest {
@Test @Test
public void testToString() { public void testToString() {
String toString = 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("size=24")).isTrue();
assertWithMessage(toString) assertWithMessage(toString)
.that(toString.contains("contents=\"Here are some bytes: \\t\\302\\241\"")) .that(toString.contains("contents=\"Here are some bytes: \\t\\302\\241\""))
@ -539,7 +538,7 @@ public class ByteStringTest {
String toString = String toString =
ByteString.copyFrom( ByteString.copyFrom(
"123456789012345678901234567890123456789012345678901234567890" "123456789012345678901234567890123456789012345678901234567890"
.getBytes(StandardCharsets.UTF_8)) .getBytes(Internal.UTF_8))
.toString(); .toString();
assertWithMessage(toString).that(toString.contains("size=60")).isTrue(); assertWithMessage(toString).that(toString.contains("size=60")).isTrue();
assertWithMessage(toString) assertWithMessage(toString)

@ -41,7 +41,6 @@ import protobuf_unittest.UnittestProto.TestSparseEnum;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -418,7 +417,7 @@ public class CodedOutputStreamTest {
// Write some some bytes (more than the buffer can hold) and verify that totalWritten // Write some some bytes (more than the buffer can hold) and verify that totalWritten
// is correct. // is correct.
byte[] value = "abcde".getBytes(StandardCharsets.UTF_8); byte[] value = "abcde".getBytes(Internal.UTF_8);
for (int i = 0; i < 1024; ++i) { for (int i = 0; i < 1024; ++i) {
coder.stream().writeRawBytes(value, 0, value.length); coder.stream().writeRawBytes(value, 0, value.length);
} }
@ -501,7 +500,7 @@ public class CodedOutputStreamTest {
@Test @Test
public void testWriteByteBuffer() throws Exception { public void testWriteByteBuffer() throws Exception {
byte[] value = "abcde".getBytes(StandardCharsets.UTF_8); byte[] value = "abcde".getBytes(Internal.UTF_8);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream);
ByteBuffer byteBuffer = ByteBuffer.wrap(value, 0, 1); ByteBuffer byteBuffer = ByteBuffer.wrap(value, 0, 1);
@ -544,7 +543,7 @@ public class CodedOutputStreamTest {
for (int pos = 0; pos < source.length(); pos += 2) { for (int pos = 0; pos < source.length(); pos += 2) {
String substr = source.substring(pos, pos + 2); String substr = source.substring(pos, pos + 2);
expectedBytesStream.write(2); expectedBytesStream.write(2);
expectedBytesStream.write(substr.getBytes(StandardCharsets.UTF_8)); expectedBytesStream.write(substr.getBytes(Internal.UTF_8));
} }
final byte[] expectedBytes = expectedBytesStream.toByteArray(); final byte[] expectedBytes = expectedBytesStream.toByteArray();

@ -34,7 +34,6 @@ import com.google.protobuf.Utf8.Processor;
import com.google.protobuf.Utf8.SafeProcessor; import com.google.protobuf.Utf8.SafeProcessor;
import com.google.protobuf.Utf8.UnsafeProcessor; import com.google.protobuf.Utf8.UnsafeProcessor;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -197,7 +196,7 @@ public class DecodeUtf8Test extends TestCase {
} }
public void testInvalidBufferSlice() 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 - 3, 4);
assertInvalidSlice(bytes, bytes.length, 1); assertInvalidSlice(bytes, bytes.length, 1);
assertInvalidSlice(bytes, bytes.length + 1, 0); assertInvalidSlice(bytes, bytes.length + 1, 0);
@ -302,35 +301,35 @@ public class DecodeUtf8Test extends TestCase {
} }
private void assertRoundTrips(String str, int index, int size) 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) { if (size == -1) {
size = bytes.length; size = bytes.length;
} }
assertDecode( assertDecode(
new String(bytes, index, size, StandardCharsets.UTF_8), new String(bytes, index, size, Internal.UTF_8),
UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size)); UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size));
assertDecode( assertDecode(
new String(bytes, index, size, StandardCharsets.UTF_8), new String(bytes, index, size, Internal.UTF_8),
SAFE_PROCESSOR.decodeUtf8(bytes, index, size)); SAFE_PROCESSOR.decodeUtf8(bytes, index, size));
ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length); ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length);
direct.put(bytes); direct.put(bytes);
direct.flip(); direct.flip();
assertDecode( assertDecode(
new String(bytes, index, size, StandardCharsets.UTF_8), new String(bytes, index, size, Internal.UTF_8),
UNSAFE_PROCESSOR.decodeUtf8(direct, index, size)); UNSAFE_PROCESSOR.decodeUtf8(direct, index, size));
assertDecode( assertDecode(
new String(bytes, index, size, StandardCharsets.UTF_8), new String(bytes, index, size, Internal.UTF_8),
SAFE_PROCESSOR.decodeUtf8(direct, index, size)); SAFE_PROCESSOR.decodeUtf8(direct, index, size));
ByteBuffer heap = ByteBuffer.allocate(bytes.length); ByteBuffer heap = ByteBuffer.allocate(bytes.length);
heap.put(bytes); heap.put(bytes);
heap.flip(); heap.flip();
assertDecode( assertDecode(
new String(bytes, index, size, StandardCharsets.UTF_8), new String(bytes, index, size, Internal.UTF_8),
UNSAFE_PROCESSOR.decodeUtf8(heap, index, size)); UNSAFE_PROCESSOR.decodeUtf8(heap, index, size));
assertDecode( assertDecode(
new String(bytes, index, size, StandardCharsets.UTF_8), new String(bytes, index, size, Internal.UTF_8),
SAFE_PROCESSOR.decodeUtf8(heap, index, size)); SAFE_PROCESSOR.decodeUtf8(heap, index, size));
} }

@ -63,8 +63,6 @@ import protobuf_unittest.UnittestProto.TestMultipleExtensionRanges;
import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestRequired;
import protobuf_unittest.UnittestProto.TestReservedFields; import protobuf_unittest.UnittestProto.TestReservedFields;
import protobuf_unittest.UnittestProto.TestService; import protobuf_unittest.UnittestProto.TestService;
import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
@ -283,7 +281,7 @@ public class DescriptorsTest {
assertThat(d.findFieldByName("escaped_bytes").getDefaultValue()) assertThat(d.findFieldByName("escaped_bytes").getDefaultValue())
.isEqualTo( .isEqualTo(
ByteString.copyFrom( 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_uint32").getDefaultValue()).isEqualTo(-1);
assertThat(d.findFieldByName("large_uint64").getDefaultValue()).isEqualTo(-1L); assertThat(d.findFieldByName("large_uint64").getDefaultValue()).isEqualTo(-1L);
} }

@ -35,7 +35,6 @@ import static com.google.common.truth.Truth.assertWithMessage;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -277,8 +276,8 @@ final class IsValidUtf8TestUtil {
} }
ByteString bs = factory.newByteString(bytes); ByteString bs = factory.newByteString(bytes);
boolean isRoundTrippable = bs.isValidUtf8(); boolean isRoundTrippable = bs.isValidUtf8();
String s = new String(bytes, StandardCharsets.UTF_8); String s = new String(bytes, Internal.UTF_8);
byte[] bytesReencoded = s.getBytes(StandardCharsets.UTF_8); byte[] bytesReencoded = s.getBytes(Internal.UTF_8);
boolean bytesEqual = Arrays.equals(bytes, bytesReencoded); boolean bytesEqual = Arrays.equals(bytes, bytesReencoded);
if (bytesEqual != isRoundTrippable) { if (bytesEqual != isRoundTrippable) {

@ -43,7 +43,6 @@ import java.io.ObjectOutputStream;
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.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -469,7 +468,7 @@ public class LiteralByteStringTest {
@Test @Test
public void testToString() throws UnsupportedEncodingException { public void testToString() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters"; 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); String roundTripString = unicode.toString(UTF_8);
assertWithMessage("%s unicode must match", classUnderTest) assertWithMessage("%s unicode must match", classUnderTest)
.that(testString) .that(testString)
@ -479,8 +478,8 @@ public class LiteralByteStringTest {
@Test @Test
public void testCharsetToString() { public void testCharsetToString() {
String testString = "I love unicode \u1234\u5678 characters"; 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(StandardCharsets.UTF_8); String roundTripString = unicode.toString(Internal.UTF_8);
assertWithMessage("%s unicode must match", classUnderTest) assertWithMessage("%s unicode must match", classUnderTest)
.that(testString) .that(testString)
.isEqualTo(roundTripString); .isEqualTo(roundTripString);
@ -489,8 +488,8 @@ public class LiteralByteStringTest {
@Test @Test
public void testToString_returnsCanonicalEmptyString() { public void testToString_returnsCanonicalEmptyString() {
assertWithMessage("%s must be the same string references", classUnderTest) assertWithMessage("%s must be the same string references", classUnderTest)
.that(ByteString.EMPTY.toString(StandardCharsets.UTF_8)) .that(ByteString.EMPTY.toString(Internal.UTF_8))
.isSameInstanceAs(ByteString.wrap(new byte[] {}).toString(StandardCharsets.UTF_8)); .isSameInstanceAs(ByteString.wrap(new byte[] {}).toString(Internal.UTF_8));
} }
@Test @Test

@ -32,8 +32,7 @@ package com.google.protobuf;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage; 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;

@ -33,7 +33,6 @@ package com.google.protobuf;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator; import java.util.Iterator;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -133,7 +132,7 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest {
assertWithMessage("%s from string must have the expected type", classUnderTest) assertWithMessage("%s from string must have the expected type", classUnderTest)
.that(classUnderTest) .that(classUnderTest)
.isEqualTo(getActualClassName(unicode)); .isEqualTo(getActualClassName(unicode));
String roundTripString = unicode.toString(StandardCharsets.UTF_8); String roundTripString = unicode.toString(Internal.UTF_8);
assertWithMessage("%s unicode bytes must match", classUnderTest) assertWithMessage("%s unicode bytes must match", classUnderTest)
.that(testString) .that(testString)
.isEqualTo(roundTripString); .isEqualTo(roundTripString);

@ -39,7 +39,6 @@ import java.io.InputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import org.junit.Before; import org.junit.Before;
@ -176,7 +175,7 @@ public class RopeByteStringTest extends LiteralByteStringTest {
assertWithMessage("%s from string must have the expected type", classUnderTest) assertWithMessage("%s from string must have the expected type", classUnderTest)
.that(classUnderTest) .that(classUnderTest)
.isEqualTo(getActualClassName(unicode)); .isEqualTo(getActualClassName(unicode));
String roundTripString = unicode.toString(StandardCharsets.UTF_8); String roundTripString = unicode.toString(Internal.UTF_8);
assertWithMessage("%s unicode bytes must match", classUnderTest) assertWithMessage("%s unicode bytes must match", classUnderTest)
.that(testString) .that(testString)
.isEqualTo(roundTripString); .isEqualTo(roundTripString);
@ -195,8 +194,8 @@ public class RopeByteStringTest extends LiteralByteStringTest {
RopeByteString ropeByteString = RopeByteString ropeByteString =
RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY);
assertWithMessage("%s must be the same string references", classUnderTest) assertWithMessage("%s must be the same string references", classUnderTest)
.that(ByteString.EMPTY.toString(StandardCharsets.UTF_8)) .that(ByteString.EMPTY.toString(Internal.UTF_8))
.isSameInstanceAs(ropeByteString.toString(StandardCharsets.UTF_8)); .isSameInstanceAs(ropeByteString.toString(Internal.UTF_8));
} }
@Override @Override

@ -59,7 +59,6 @@ import protobuf_unittest.UnittestProto.TestOneof2;
import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestRequired;
import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet;
import java.io.StringReader; import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -278,7 +277,7 @@ public class TextFormatTest {
* are converted directly to bytes, *not* encoded using UTF-8. * are converted directly to bytes, *not* encoded using UTF-8.
*/ */
private ByteString bytes(String str) { private ByteString bytes(String str) {
return ByteString.copyFrom(str.getBytes(StandardCharsets.ISO_8859_1)); return ByteString.copyFrom(str.getBytes(Internal.ISO_8859_1));
} }
/** /**

@ -34,7 +34,6 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Random; import java.util.Random;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -134,7 +133,7 @@ public class Utf8Test {
} }
private static void assertEncoding(String message) { 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); byte[] output = encodeToByteArray(message, expected.length, safeProcessor);
assertWithMessage("encodeUtf8[ARRAY]") assertWithMessage("encodeUtf8[ARRAY]")
.that(output).isEqualTo(expected); .that(output).isEqualTo(expected);

@ -32,8 +32,6 @@ package com.google.protobuf;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import java.nio.charset.StandardCharsets;
import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -53,7 +51,7 @@ public class WrappersLiteOfMethodTest {
builder.setFieldUint64(UInt64Value.of(23333333333333L)); builder.setFieldUint64(UInt64Value.of(23333333333333L));
builder.setFieldBool(BoolValue.of(true)); builder.setFieldBool(BoolValue.of(true));
builder.setFieldString(StringValue.of("23333")); 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(); TopLevelMessage message = builder.build();
assertThat(message.getFieldDouble().getValue()).isEqualTo(2.333); assertThat(message.getFieldDouble().getValue()).isEqualTo(2.333);

@ -32,8 +32,6 @@ package com.google.protobuf;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import java.nio.charset.StandardCharsets;
import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -53,7 +51,7 @@ public class WrappersOfMethodTest {
builder.setFieldUint64(UInt64Value.of(23333333333333L)); builder.setFieldUint64(UInt64Value.of(23333333333333L));
builder.setFieldBool(BoolValue.of(true)); builder.setFieldBool(BoolValue.of(true));
builder.setFieldString(StringValue.of("23333")); 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(); TopLevelMessage message = builder.build();
assertThat(message.getFieldDouble().getValue()).isEqualTo(2.333); assertThat(message.getFieldDouble().getValue()).isEqualTo(2.333);

@ -68,7 +68,6 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
@ -1721,7 +1720,7 @@ public class LiteTest {
public void testMergeFromStream_invalidBytes() throws Exception { public void testMergeFromStream_invalidBytes() throws Exception {
TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder().setDefaultBool(true); TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder().setDefaultBool(true);
try { 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(); assertWithMessage("expected exception").fail();
} catch (InvalidProtocolBufferException expected) { } catch (InvalidProtocolBufferException expected) {
} }

Loading…
Cancel
Save