From badef1fc19566745e1a052430202bb34b6a6150f Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 2 Apr 2015 14:22:35 -0700 Subject: Move `UTF_8` to `Internal` --- java/src/main/java/com/google/protobuf/ByteString.java | 6 ++---- java/src/main/java/com/google/protobuf/CodedInputStream.java | 6 +++--- java/src/main/java/com/google/protobuf/CodedOutputStream.java | 4 ++-- java/src/main/java/com/google/protobuf/Internal.java | 9 +++++---- java/src/main/java/com/google/protobuf/Utf8.java | 2 +- .../src/test/java/com/google/protobuf/BoundedByteStringTest.java | 6 +++--- java/src/test/java/com/google/protobuf/ByteStringTest.java | 4 ++-- .../src/test/java/com/google/protobuf/CodedOutputStreamTest.java | 4 ++-- java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java | 8 ++++---- .../src/test/java/com/google/protobuf/LiteralByteStringTest.java | 8 ++++---- .../java/com/google/protobuf/RopeByteStringSubstringTest.java | 2 +- java/src/test/java/com/google/protobuf/RopeByteStringTest.java | 4 ++-- java/src/test/java/com/google/protobuf/TestUtil.java | 2 +- .../test/java/com/google/protobuf/UnknownFieldSetLiteTest.java | 2 +- 14 files changed, 33 insertions(+), 34 deletions(-) (limited to 'java/src') diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index 04ac7c9a..1d5d4e8a 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -78,8 +78,6 @@ public abstract class ByteString implements Iterable, Serializable { static final int MIN_READ_FROM_CHUNK_SIZE = 0x100; // 256b static final int MAX_READ_FROM_CHUNK_SIZE = 0x2000; // 8k - protected static final Charset UTF_8 = Charset.forName("UTF-8"); - /** * Empty {@code ByteString}. */ @@ -282,7 +280,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new {@code ByteString} */ public static ByteString copyFromUtf8(String text) { - return new LiteralByteString(text.getBytes(UTF_8)); + return new LiteralByteString(text.getBytes(Internal.UTF_8)); } // ================================================================= @@ -661,7 +659,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new string using UTF-8 encoding */ public String toStringUtf8() { - return toString(UTF_8); + return toString(Internal.UTF_8); } /** diff --git a/java/src/main/java/com/google/protobuf/CodedInputStream.java b/java/src/main/java/com/google/protobuf/CodedInputStream.java index b15c2731..d201f7c5 100644 --- a/java/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedInputStream.java @@ -373,14 +373,14 @@ public final class CodedInputStream { if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. - final String result = new String(buffer, bufferPos, size, ByteString.UTF_8); + final String result = new String(buffer, bufferPos, size, Internal.UTF_8); bufferPos += size; return result; } else if (size == 0) { return ""; } else { // Slow path: Build a byte array first then copy it. - return new String(readRawBytesSlowPath(size), ByteString.UTF_8); + return new String(readRawBytesSlowPath(size), Internal.UTF_8); } } @@ -409,7 +409,7 @@ public final class CodedInputStream { if (!Utf8.isValidUtf8(bytes, pos, pos + size)) { throw InvalidProtocolBufferException.invalidUtf8(); } - return new String(bytes, pos, size, ByteString.UTF_8); + return new String(bytes, pos, size, Internal.UTF_8); } /** Read a {@code group} field value from the stream. */ diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java index 9994fd3e..f5fd58f9 100644 --- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -420,7 +420,7 @@ public final class CodedOutputStream { // 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(ByteString.UTF_8); + final byte[] bytes = value.getBytes(Internal.UTF_8); writeRawVarint32(bytes.length); writeRawBytes(bytes); } @@ -827,7 +827,7 @@ public final class CodedOutputStream { * {@code string} field. */ public static int computeStringSizeNoTag(final String value) { - final byte[] bytes = value.getBytes(ByteString.UTF_8); + final byte[] bytes = value.getBytes(Internal.UTF_8); return computeRawVarint32Size(bytes.length) + bytes.length; } diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java index 8a8bcda1..b49318ec 100644 --- a/java/src/main/java/com/google/protobuf/Internal.java +++ b/java/src/main/java/com/google/protobuf/Internal.java @@ -53,6 +53,7 @@ import java.util.Set; */ public class Internal { + protected static final Charset UTF_8 = Charset.forName("UTF-8"); protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); /** @@ -84,7 +85,7 @@ public class Internal { * generated code calls this automatically. */ public static String stringDefaultValue(String bytes) { - return new String(bytes.getBytes(ISO_8859_1), ByteString.UTF_8); + return new String(bytes.getBytes(ISO_8859_1), UTF_8); } /** @@ -144,7 +145,7 @@ public class Internal { * without loss. More precisely, returns {@code true} whenever: *
   {@code
    * Arrays.equals(byteString.toByteArray(),
-   *     new String(byteString.toByteArray(), ByteString.UTF_8).getBytes(ByteString.UTF_8))
+   *     new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
    * }
* *

This method rejects "overlong" byte sequences, as well as @@ -180,14 +181,14 @@ public class Internal { * Helper method to get the UTF-8 bytes of a string. */ public static byte[] toByteArray(String value) { - return value.getBytes(ByteString.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, ByteString.UTF_8); + return new String(bytes, UTF_8); } /** diff --git a/java/src/main/java/com/google/protobuf/Utf8.java b/java/src/main/java/com/google/protobuf/Utf8.java index 50702410..4271b41b 100644 --- a/java/src/main/java/com/google/protobuf/Utf8.java +++ b/java/src/main/java/com/google/protobuf/Utf8.java @@ -46,7 +46,7 @@ package com.google.protobuf; *

The byte sequences considered valid by this class are exactly * those that can be roundtrip converted to Strings and back to bytes * using the UTF-8 charset, without loss:

 {@code
- * Arrays.equals(bytes, new String(bytes, ByteString.UTF_8).getBytes(ByteString.UTF_8))
+ * Arrays.equals(bytes, new String(bytes, Internal.UTF_8).getBytes(Internal.UTF_8))
  * }
* *

See the Unicode Standard,
diff --git a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java index 9c0ff925..1562a1a6 100644 --- a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Override public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertEquals(classUnderTest + ".substring() must have the expected type", classUnderTest, getActualClassName(chopped)); @@ -75,12 +75,12 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Override public void testCharsetToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertEquals(classUnderTest + ".substring() must have the expected type", classUnderTest, getActualClassName(chopped)); - String roundTripString = chopped.toString(ByteString.UTF_8); + String roundTripString = chopped.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode bytes must match", testString.substring(2, testString.length() - 6), roundTripString); } diff --git a/java/src/test/java/com/google/protobuf/ByteStringTest.java b/java/src/test/java/com/google/protobuf/ByteStringTest.java index 2eb890d5..46c229ad 100644 --- a/java/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/ByteStringTest.java @@ -140,7 +140,7 @@ public class ByteStringTest extends TestCase { public void testCopyFrom_Utf8() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFromUtf8(testString); - byte[] testBytes = testString.getBytes(ByteString.UTF_8); + byte[] testBytes = testString.getBytes(Internal.UTF_8); assertTrue("copyFromUtf8 string must respect the charset", isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); } @@ -401,7 +401,7 @@ public class ByteStringTest extends TestCase { public void testToStringUtf8() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - byte[] testBytes = testString.getBytes(ByteString.UTF_8); + byte[] testBytes = testString.getBytes(Internal.UTF_8); ByteString byteString = ByteString.copyFrom(testBytes); assertEquals("copyToStringUtf8 must respect the charset", testString, byteString.toStringUtf8()); diff --git a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index bb3b03d5..365789c0 100644 --- a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -321,7 +321,7 @@ public class CodedOutputStreamTest extends TestCase { final int BUFFER_SIZE = 4 * 1024; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); - byte[] value = "abcde".getBytes(ByteString.UTF_8); + byte[] value = "abcde".getBytes(Internal.UTF_8); for (int i = 0; i < 1024; ++i) { codedStream.writeRawBytes(value, 0, value.length); } @@ -367,7 +367,7 @@ public class CodedOutputStreamTest extends TestCase { } public void testWriteByteBuffer() throws Exception { - byte[] value = "abcde".getBytes(ByteString.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); diff --git a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java index d034c067..32037039 100644 --- a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -220,8 +220,8 @@ class IsValidUtf8TestUtil { } ByteString bs = ByteString.copyFrom(bytes); boolean isRoundTrippable = bs.isValidUtf8(); - String s = new String(bytes, ByteString.UTF_8); - byte[] bytesReencoded = s.getBytes(ByteString.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) { @@ -313,10 +313,10 @@ class IsValidUtf8TestUtil { void testBytesUsingByteBuffers( int numBytes, long expectedCount, long start, long lim) throws UnsupportedEncodingException { - CharsetDecoder decoder = ByteString.UTF_8.newDecoder() + CharsetDecoder decoder = Internal.UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); - CharsetEncoder encoder = ByteString.UTF_8.newEncoder() + CharsetEncoder encoder = Internal.UTF_8.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); byte[] bytes = new byte[numBytes]; diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 4974e9f7..7b201a9d 100644 --- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -293,21 +293,21 @@ public class LiteralByteStringTest extends TestCase { public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); String roundTripString = unicode.toString(UTF_8); assertEquals(classUnderTest + " unicode must match", testString, roundTripString); } public void testCharsetToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); - String roundTripString = unicode.toString(ByteString.UTF_8); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); + String roundTripString = unicode.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode must match", testString, roundTripString); } public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{ assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(ByteString.UTF_8), new LiteralByteString(new byte[]{}).toString(ByteString.UTF_8)); + ByteString.EMPTY.toString(Internal.UTF_8), new LiteralByteString(new byte[]{}).toString(Internal.UTF_8)); } public void testToString_raisesException() throws UnsupportedEncodingException{ diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java index 43872d1d..cc385599 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -116,7 +116,7 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " from string must have the expected type", classUnderTest, getActualClassName(unicode)); - String roundTripString = unicode.toString(ByteString.UTF_8); + String roundTripString = unicode.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java index e43df165..bd0d15e3 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -135,7 +135,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " from string must have the expected type", classUnderTest, getActualClassName(unicode)); - String roundTripString = unicode.toString(ByteString.UTF_8); + String roundTripString = unicode.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); @@ -149,7 +149,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { RopeByteString ropeByteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(ByteString.UTF_8), ropeByteString.toString(ByteString.UTF_8)); + ByteString.EMPTY.toString(Internal.UTF_8), ropeByteString.toString(Internal.UTF_8)); } public void testToString_raisesException() throws UnsupportedEncodingException{ diff --git a/java/src/test/java/com/google/protobuf/TestUtil.java b/java/src/test/java/com/google/protobuf/TestUtil.java index 0ce72b5e..19a96d0e 100644 --- a/java/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/src/test/java/com/google/protobuf/TestUtil.java @@ -276,7 +276,7 @@ public final class TestUtil { /** Helper to convert a String to ByteString. */ static ByteString toBytes(String str) { - return ByteString.copyFrom(str.getBytes(ByteString.UTF_8)); + return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); } /** diff --git a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java index 2f6bcd1a..e76b4a67 100644 --- a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java @@ -229,7 +229,7 @@ public class UnknownFieldSetLiteTest extends TestCase { public void testMalformedBytes() throws Exception { try { - Foo.parseFrom("this is a malformed protocol buffer".getBytes(ByteString.UTF_8)); + Foo.parseFrom("this is a malformed protocol buffer".getBytes(Internal.UTF_8)); fail(); } catch (InvalidProtocolBufferException e) { // Expected. -- cgit v1.2.3