diff options
Diffstat (limited to 'java')
4 files changed, 29 insertions, 20 deletions
diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index bd9c2b55..cff1ee51 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -503,9 +503,9 @@ public abstract class ByteString implements Iterable<Byte>, Serializable { /** * Internal (package private) implementation of - * @link{#copyTo(byte[],int,int,int}. + * {@link #copyTo(byte[],int,int,int)}. * It assumes that all error checking has already been performed and that - * @code{numberToCopy > 0}. + * {@code numberToCopy > 0}. */ protected abstract void copyToInternal(byte[] target, int sourceOffset, int targetOffset, int numberToCopy); @@ -700,7 +700,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable { * The {@link InputStream} returned by this method is guaranteed to be * completely non-blocking. The method {@link InputStream#available()} * returns the number of bytes remaining in the stream. The methods - * {@link InputStream#read(byte[]), {@link InputStream#read(byte[],int,int)} + * {@link InputStream#read(byte[])}, {@link InputStream#read(byte[],int,int)} * and {@link InputStream#skip(long)} will read/skip as many bytes as are * available. * <p> diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java index 5ef39418..5a0de6d1 100644 --- a/java/src/main/java/com/google/protobuf/Internal.java +++ b/java/src/main/java/com/google/protobuf/Internal.java @@ -236,7 +236,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for longs. + * Helper method for implementing {@link Message#hashCode()} for longs. * @see Long#hashCode() */ public static int hashLong(long n) { @@ -244,7 +244,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for + * Helper method for implementing {@link Message#hashCode()} for * booleans. * @see Boolean#hashCode() */ @@ -253,7 +253,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for enums. + * Helper method for implementing {@link Message#hashCode()} for enums. * <p> * This is needed because {@link java.lang.Enum#hashCode()} is final, but we * need to use the field number as the hash code to ensure compatibility @@ -264,7 +264,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for + * Helper method for implementing {@link Message#hashCode()} for * enum lists. */ public static int hashEnumList(List<? extends EnumLite> list) { @@ -276,7 +276,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#equals()} for bytes field. + * Helper method for implementing {@link Message#equals(Object)} for bytes field. */ public static boolean equals(List<byte[]> a, List<byte[]> b) { if (a.size() != b.size()) return false; @@ -289,7 +289,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for bytes field. + * Helper method for implementing {@link Message#hashCode()} for bytes field. */ public static int hashCode(List<byte[]> list) { int hash = 1; @@ -300,7 +300,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for bytes field. + * Helper method for implementing {@link Message#hashCode()} for bytes field. */ public static int hashCode(byte[] bytes) { // The hash code for a byte array should be the same as the hash code for a @@ -311,7 +311,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#equals()} for bytes + * Helper method for implementing {@link Message#equals(Object)} for bytes * field. */ public static boolean equalsByteBuffer(ByteBuffer a, ByteBuffer b) { @@ -324,7 +324,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#equals()} for bytes + * Helper method for implementing {@link Message#equals(Object)} for bytes * field. */ public static boolean equalsByteBuffer( @@ -341,7 +341,7 @@ public class Internal { } /** - * Helper method for implementing {@link MessageLite#hashCode()} for bytes + * Helper method for implementing {@link Message#hashCode()} for bytes * field. */ public static int hashCodeByteBuffer(List<ByteBuffer> list) { @@ -355,7 +355,7 @@ public class Internal { private static final int DEFAULT_BUFFER_SIZE = 4096; /** - * Helper method for implementing {@link MessageLite#hashCode()} for bytes + * Helper method for implementing {@link Message#hashCode()} for bytes * field. */ public static int hashCodeByteBuffer(ByteBuffer bytes) { diff --git a/java/src/main/java/com/google/protobuf/TextFormat.java b/java/src/main/java/com/google/protobuf/TextFormat.java index 63e62fc6..4f6756ed 100644 --- a/java/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/src/main/java/com/google/protobuf/TextFormat.java @@ -411,7 +411,8 @@ public final class TextFormat { generator.print("\""); generator.print(escapeNonAscii ? escapeText((String) value) : - escapeDoubleQuotesAndBackslashes((String) value)); + escapeDoubleQuotesAndBackslashes((String) value) + .replace("\n", "\\n")); generator.print("\""); break; diff --git a/java/src/test/java/com/google/protobuf/TextFormatTest.java b/java/src/test/java/com/google/protobuf/TextFormatTest.java index 82f9582f..eba06ca0 100644 --- a/java/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/src/test/java/com/google/protobuf/TextFormatTest.java @@ -864,15 +864,15 @@ public class TextFormatTest extends TestCase { assertEquals(message.getOptionalString(), builder.getOptionalString()); } - public void testPrintToUnicodeStringWithNewlines() { + public void testPrintToUnicodeStringWithNewlines() throws Exception { // No newlines at start and end - assertEquals("optional_string: \"test newlines\n\nin\nstring\"\n", + assertEquals("optional_string: \"test newlines\\n\\nin\\nstring\"\n", TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("test newlines\n\nin\nstring") .build())); // Newlines at start and end - assertEquals("optional_string: \"\ntest\nnewlines\n\nin\nstring\n\"\n", + assertEquals("optional_string: \"\\ntest\\nnewlines\\n\\nin\\nstring\\n\"\n", TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("\ntest\nnewlines\n\nin\nstring\n") .build())); @@ -882,14 +882,22 @@ public class TextFormatTest extends TestCase { TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("") .build())); - assertEquals("optional_string: \"\n\"\n", + assertEquals("optional_string: \"\\n\"\n", TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("\n") .build())); - assertEquals("optional_string: \"\n\n\"\n", + assertEquals("optional_string: \"\\n\\n\"\n", TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("\n\n") .build())); + + // Test escaping roundtrip + TestAllTypes message = TestAllTypes.newBuilder() + .setOptionalString("\ntest\nnewlines\n\nin\nstring\n") + .build(); + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + TextFormat.merge(TextFormat.printToUnicodeString(message), builder); + assertEquals(message.getOptionalString(), builder.getOptionalString()); } public void testPrintToUnicodeString_unknown() { |