Migrate from `ByteString.copyFrom(string.getBytes())` to `ByteString.copyFromUtf8(string)`.
Note: This may be behavior changing! Previously, the string was encoded into a `byte[]` using the platform's default charset (which is dangerous as it can vary from device to device or platform to platform). This change explicitly chooses encoding.
@@ -113,7 +113,7 @@ public class CompatibilityTest extends ProtobufTest {
}
public void testSetAndGetByteString() throws Exception {
- ByteString bstr = ByteString.copyFrom("foo".getBytes());
+ ByteString bstr = ByteString.copyFromUtf8("foo");
TypicalData data = TypicalData.newBuilder().setMyBytes(bstr).build();
assertEquals("foo", new String(data.getMyBytes().toByteArray()));
}
@@ -183,12 +183,13 @@ public class CompatibilityTest extends ProtobufTest {
public void testSetAndGetRepeatedBytes() throws Exception {
List<ByteString> list = new ArrayList<ByteString>();
- list.add(ByteString.copyFrom("def".getBytes()));
- list.add(ByteString.copyFrom("ghi".getBytes()));
- TypicalData data = TypicalData.newBuilder()
- .addRepeatedBytes(ByteString.copyFrom("abc".getBytes()))
+ list.add(ByteString.copyFromUtf8("def"));
+ list.add(ByteString.copyFromUtf8("ghi"));
+ TypicalData data =
+ TypicalData.newBuilder()
+ .addRepeatedBytes(ByteString.copyFromUtf8("abc"))
.addAllRepeatedBytes(list)
- .setRepeatedBytes(2, ByteString.copyFrom("jkl".getBytes()))
+ .setRepeatedBytes(2, ByteString.copyFromUtf8("jkl"))
.build();
assertEquals(3, data.getRepeatedBytesCount());
assertEquals("abc", new String(data.getRepeatedBytes(0).toByteArray()));
@@ -632,7 +633,7 @@ public class CompatibilityTest extends ProtobufTest {
TypicalData.Builder dataBuilder = TypicalData.newBuilder();
dataBuilder.setField(fields[1], new Integer(42));
- dataBuilder.setField(fields[2], ByteString.copyFrom("foo".getBytes()));
+ dataBuilder.setField(fields[2], ByteString.copyFromUtf8("foo"));
dataBuilder.setField(fields[3], TypicalData.EnumType.VALUE9.getValueDescriptor());
dataBuilder.setField(fields[11], TypicalDataMessage.newBuilder().build());
dataBuilder.setField(fields[12], Boolean.TRUE);
@@ -938,14 +939,15 @@ public class CompatibilityTest extends ProtobufTest {
List<TypicalDataMessage> repeatedData = new ArrayList<TypicalDataMessage>();
repeatedData.add(TypicalDataMessage.newBuilder().setMyMessageInt(432).build());
repeatedData.add(TypicalDataMessage.newBuilder().setMyMessageInt(543).build());
- TypicalData.Builder dataBuilder = TypicalData.newBuilder()
+ TypicalData.Builder dataBuilder =
+ TypicalData.newBuilder()
.setExtension(Typical.myPrimitiveExtension, 123)
.setExtension(Typical.myExtension, extensionMessage)
.setExtension(Typical.myRepeatedPrimitiveExtension, repeatedInts)
.addExtension(Typical.myRepeatedPrimitiveExtension, 3)
.setExtension(Typical.myRepeatedExtension, repeatedData)
.setExtension(Typical.myEnumExtension, TypicalData.EnumType.VALUE1)
- .setExtension(Typical.myBytesExtension, ByteString.copyFrom("abc".getBytes()))
+ .setExtension(Typical.myBytesExtension, ByteString.copyFromUtf8("abc"))
.setExtension(Typical.myBoolExtension, Boolean.TRUE)
.setExtension(MsgWithNestedExtensions.intExt, 456);
checkGetExtensions(dataBuilder);
@@ -1274,7 +1276,7 @@ public class CompatibilityTest extends ProtobufTest {
}
public void testByteAt() throws Exception {
- ByteString bstr = ByteString.copyFrom("bar".getBytes());
+ ByteString bstr = ByteString.copyFromUtf8("bar");
assertEquals(98, bstr.byteAt(0));
assertEquals(97, bstr.byteAt(1));
assertEquals(114, bstr.byteAt(2));
