Ver Fonte

Move `UTF_8` to `Internal`

Tamir Duberstein há 10 anos atrás
pai
commit
badef1fc19

+ 2 - 4
java/src/main/java/com/google/protobuf/ByteString.java

@@ -78,8 +78,6 @@ public abstract class ByteString implements Iterable<Byte>, 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<Byte>, 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<Byte>, Serializable {
    * @return new string using UTF-8 encoding
    */
   public String toStringUtf8() {
-    return toString(UTF_8);
+    return toString(Internal.UTF_8);
   }
 
   /**

+ 3 - 3
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. */

+ 2 - 2
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;
   }

+ 5 - 4
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:
    * <pre>   {@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"))
    * }</pre>
    *
    * <p>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);
   }
 
   /**

+ 1 - 1
java/src/main/java/com/google/protobuf/Utf8.java

@@ -46,7 +46,7 @@ package com.google.protobuf;
  * <p>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: <pre> {@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))
  * }</pre>
  *
  * <p>See the Unicode Standard,</br>

+ 3 - 3
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);
   }

+ 2 - 2
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());

+ 2 - 2
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);

+ 4 - 4
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];

+ 4 - 4
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{

+ 1 - 1
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);

+ 2 - 2
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{

+ 1 - 1
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));
   }
 
   /**

+ 1 - 1
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.