Commit 210de285 authored by Tamir Duberstein's avatar Tamir Duberstein

DRY: Use `Charset` statics to eliminate exceptions

parent e84893f6
...@@ -262,6 +262,18 @@ public abstract class ByteString implements Iterable<Byte>, Serializable { ...@@ -262,6 +262,18 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
return new LiteralByteString(text.getBytes(charsetName)); return new LiteralByteString(text.getBytes(charsetName));
} }
/**
* Encodes {@code text} into a sequence of bytes using the named charset
* and returns the result as a {@code ByteString}.
*
* @param text source string
* @param charset encode using this charset
* @return new {@code ByteString}
*/
public static ByteString copyFrom(String text, Charset charset) {
return new LiteralByteString(text.getBytes(charset));
}
/** /**
* Encodes {@code text} into a sequence of UTF-8 bytes and returns the * Encodes {@code text} into a sequence of UTF-8 bytes and returns the
* result as a {@code ByteString}. * result as a {@code ByteString}.
......
...@@ -373,14 +373,14 @@ public final class CodedInputStream { ...@@ -373,14 +373,14 @@ public final class CodedInputStream {
if (size <= (bufferSize - bufferPos) && size > 0) { if (size <= (bufferSize - bufferPos) && size > 0) {
// Fast path: We already have the bytes in a contiguous buffer, so // Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it. // just copy directly from it.
final String result = new String(buffer, bufferPos, size, "UTF-8"); final String result = new String(buffer, bufferPos, size, ByteString.UTF_8);
bufferPos += size; bufferPos += size;
return result; return result;
} else if (size == 0) { } else if (size == 0) {
return ""; return "";
} else { } else {
// Slow path: Build a byte array first then copy it. // Slow path: Build a byte array first then copy it.
return new String(readRawBytesSlowPath(size), "UTF-8"); return new String(readRawBytesSlowPath(size), ByteString.UTF_8);
} }
} }
...@@ -409,7 +409,7 @@ public final class CodedInputStream { ...@@ -409,7 +409,7 @@ public final class CodedInputStream {
if (!Utf8.isValidUtf8(bytes, pos, pos + size)) { if (!Utf8.isValidUtf8(bytes, pos, pos + size)) {
throw InvalidProtocolBufferException.invalidUtf8(); throw InvalidProtocolBufferException.invalidUtf8();
} }
return new String(bytes, pos, size, "UTF-8"); return new String(bytes, pos, size, ByteString.UTF_8);
} }
/** Read a {@code group} field value from the stream. */ /** Read a {@code group} field value from the stream. */
......
...@@ -420,7 +420,7 @@ public final class CodedOutputStream { ...@@ -420,7 +420,7 @@ public final class CodedOutputStream {
// 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("UTF-8"); final byte[] bytes = value.getBytes(ByteString.UTF_8);
writeRawVarint32(bytes.length); writeRawVarint32(bytes.length);
writeRawBytes(bytes); writeRawBytes(bytes);
} }
...@@ -827,13 +827,9 @@ public final class CodedOutputStream { ...@@ -827,13 +827,9 @@ public final class CodedOutputStream {
* {@code string} field. * {@code string} field.
*/ */
public static int computeStringSizeNoTag(final String value) { public static int computeStringSizeNoTag(final String value) {
try { final byte[] bytes = value.getBytes(ByteString.UTF_8);
final byte[] bytes = value.getBytes("UTF-8");
return computeRawVarint32Size(bytes.length) + return computeRawVarint32Size(bytes.length) +
bytes.length; bytes.length;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported.", e);
}
} }
/** /**
......
...@@ -319,12 +319,7 @@ public final class Descriptors { ...@@ -319,12 +319,7 @@ public final class Descriptors {
} }
final byte[] descriptorBytes; final byte[] descriptorBytes;
try { descriptorBytes = descriptorData.toString().getBytes(Internal.ISO_8859_1);
descriptorBytes = descriptorData.toString().getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(
"Standard encoding ISO-8859-1 not supported by JVM.", e);
}
FileDescriptorProto proto; FileDescriptorProto proto;
try { try {
......
...@@ -33,6 +33,7 @@ package com.google.protobuf; ...@@ -33,6 +33,7 @@ package com.google.protobuf;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
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;
...@@ -51,6 +52,9 @@ import java.util.Set; ...@@ -51,6 +52,9 @@ import java.util.Set;
* @author kenton@google.com (Kenton Varda) * @author kenton@google.com (Kenton Varda)
*/ */
public class Internal { public class Internal {
protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
/** /**
* Helper called by generated code to construct default values for string * Helper called by generated code to construct default values for string
* fields. * fields.
...@@ -80,14 +84,7 @@ public class Internal { ...@@ -80,14 +84,7 @@ public class Internal {
* generated code calls this automatically. * generated code calls this automatically.
*/ */
public static String stringDefaultValue(String bytes) { public static String stringDefaultValue(String bytes) {
try { return new String(bytes.getBytes(ISO_8859_1), ByteString.UTF_8);
return new String(bytes.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
// This should never happen since all JVMs are required to implement
// both of the above character sets.
throw new IllegalStateException(
"Java VM does not support a standard character set.", e);
}
} }
/** /**
...@@ -99,14 +96,7 @@ public class Internal { ...@@ -99,14 +96,7 @@ public class Internal {
* embed raw bytes as a string literal with ISO-8859-1 encoding. * embed raw bytes as a string literal with ISO-8859-1 encoding.
*/ */
public static ByteString bytesDefaultValue(String bytes) { public static ByteString bytesDefaultValue(String bytes) {
try { return ByteString.copyFrom(bytes.getBytes(ISO_8859_1));
return ByteString.copyFrom(bytes.getBytes("ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
// This should never happen since all JVMs are required to implement
// ISO-8859-1.
throw new IllegalStateException(
"Java VM does not support a standard character set.", e);
}
} }
/** /**
* Helper called by generated code to construct default values for bytes * Helper called by generated code to construct default values for bytes
...@@ -115,14 +105,7 @@ public class Internal { ...@@ -115,14 +105,7 @@ public class Internal {
* This is like {@link #bytesDefaultValue}, but returns a byte array. * This is like {@link #bytesDefaultValue}, but returns a byte array.
*/ */
public static byte[] byteArrayDefaultValue(String bytes) { public static byte[] byteArrayDefaultValue(String bytes) {
try { return bytes.getBytes(ISO_8859_1);
return bytes.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// This should never happen since all JVMs are required to implement
// ISO-8859-1.
throw new IllegalStateException(
"Java VM does not support a standard character set.", e);
}
} }
/** /**
...@@ -161,7 +144,7 @@ public class Internal { ...@@ -161,7 +144,7 @@ public class Internal {
* without loss. More precisely, returns {@code true} whenever: * without loss. More precisely, returns {@code true} whenever:
* <pre> {@code * <pre> {@code
* Arrays.equals(byteString.toByteArray(), * Arrays.equals(byteString.toByteArray(),
* new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8")) * new String(byteString.toByteArray(), ByteString.UTF_8).getBytes(ByteString.UTF_8))
* }</pre> * }</pre>
* *
* <p>This method rejects "overlong" byte sequences, as well as * <p>This method rejects "overlong" byte sequences, as well as
...@@ -197,22 +180,14 @@ public class Internal { ...@@ -197,22 +180,14 @@ public 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) {
try { return value.getBytes(ByteString.UTF_8);
return value.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?", e);
}
} }
/** /**
* 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) {
try { return new String(bytes, ByteString.UTF_8);
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?", e);
}
} }
/** /**
......
...@@ -46,7 +46,7 @@ package com.google.protobuf; ...@@ -46,7 +46,7 @@ package com.google.protobuf;
* <p>The byte sequences considered valid by this class are exactly * <p>The byte sequences considered valid by this class are exactly
* those that can be roundtrip converted to Strings and back to bytes * those that can be roundtrip converted to Strings and back to bytes
* using the UTF-8 charset, without loss: <pre> {@code * using the UTF-8 charset, without loss: <pre> {@code
* Arrays.equals(bytes, new String(bytes, "UTF-8").getBytes("UTF-8")) * Arrays.equals(bytes, new String(bytes, ByteString.UTF_8).getBytes(ByteString.UTF_8))
* }</pre> * }</pre>
* *
* <p>See the Unicode Standard,</br> * <p>See the Unicode Standard,</br>
......
...@@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { ...@@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Override @Override
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";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(UTF_8)); LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8));
ByteString chopped = unicode.substring(2, unicode.size() - 6); ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertEquals(classUnderTest + ".substring() must have the expected type", assertEquals(classUnderTest + ".substring() must have the expected type",
classUnderTest, getActualClassName(chopped)); classUnderTest, getActualClassName(chopped));
......
...@@ -36,11 +36,12 @@ import junit.framework.TestCase; ...@@ -36,11 +36,12 @@ import junit.framework.TestCase;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException;
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.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
...@@ -56,7 +57,7 @@ import java.util.Random; ...@@ -56,7 +57,7 @@ import java.util.Random;
*/ */
public class ByteStringTest extends TestCase { public class ByteStringTest extends TestCase {
private static final String UTF_16 = "UTF-16"; private static final Charset UTF_16 = Charset.forName("UTF-16");
static byte[] getTestBytes(int size, long seed) { static byte[] getTestBytes(int size, long seed) {
Random random = new Random(seed); Random random = new Random(seed);
...@@ -139,7 +140,7 @@ public class ByteStringTest extends TestCase { ...@@ -139,7 +140,7 @@ public class ByteStringTest extends TestCase {
public void testCopyFrom_Utf8() throws UnsupportedEncodingException { public void testCopyFrom_Utf8() throws UnsupportedEncodingException {
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("UTF-8"); byte[] testBytes = testString.getBytes(ByteString.UTF_8);
assertTrue("copyFromUtf8 string must respect the charset", assertTrue("copyFromUtf8 string must respect the charset",
isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length));
} }
...@@ -400,7 +401,7 @@ public class ByteStringTest extends TestCase { ...@@ -400,7 +401,7 @@ public class ByteStringTest extends TestCase {
public void testToStringUtf8() throws UnsupportedEncodingException { public void testToStringUtf8() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters"; String testString = "I love unicode \u1234\u5678 characters";
byte[] testBytes = testString.getBytes("UTF-8"); byte[] testBytes = testString.getBytes(ByteString.UTF_8);
ByteString byteString = ByteString.copyFrom(testBytes); ByteString byteString = ByteString.copyFrom(testBytes);
assertEquals("copyToStringUtf8 must respect the charset", assertEquals("copyToStringUtf8 must respect the charset",
testString, byteString.toStringUtf8()); testString, byteString.toStringUtf8());
......
...@@ -321,7 +321,7 @@ public class CodedOutputStreamTest extends TestCase { ...@@ -321,7 +321,7 @@ public class CodedOutputStreamTest extends TestCase {
final int BUFFER_SIZE = 4 * 1024; final int BUFFER_SIZE = 4 * 1024;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream);
byte[] value = "abcde".getBytes("UTF-8"); byte[] value = "abcde".getBytes(ByteString.UTF_8);
for (int i = 0; i < 1024; ++i) { for (int i = 0; i < 1024; ++i) {
codedStream.writeRawBytes(value, 0, value.length); codedStream.writeRawBytes(value, 0, value.length);
} }
...@@ -367,7 +367,7 @@ public class CodedOutputStreamTest extends TestCase { ...@@ -367,7 +367,7 @@ public class CodedOutputStreamTest extends TestCase {
} }
public void testWriteByteBuffer() throws Exception { public void testWriteByteBuffer() throws Exception {
byte[] value = "abcde".getBytes("UTF-8"); byte[] value = "abcde".getBytes(ByteString.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);
......
...@@ -286,7 +286,7 @@ public class DescriptorsTest extends TestCase { ...@@ -286,7 +286,7 @@ public class DescriptorsTest extends TestCase {
d = TestExtremeDefaultValues.getDescriptor(); d = TestExtremeDefaultValues.getDescriptor();
assertEquals( assertEquals(
ByteString.copyFrom( ByteString.copyFrom(
"\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes("ISO-8859-1")), "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.ISO_8859_1)),
d.findFieldByName("escaped_bytes").getDefaultValue()); d.findFieldByName("escaped_bytes").getDefaultValue());
assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue()); assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue());
assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue()); assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue());
......
...@@ -220,8 +220,8 @@ class IsValidUtf8TestUtil { ...@@ -220,8 +220,8 @@ class IsValidUtf8TestUtil {
} }
ByteString bs = ByteString.copyFrom(bytes); ByteString bs = ByteString.copyFrom(bytes);
boolean isRoundTrippable = bs.isValidUtf8(); boolean isRoundTrippable = bs.isValidUtf8();
String s = new String(bytes, "UTF-8"); String s = new String(bytes, ByteString.UTF_8);
byte[] bytesReencoded = s.getBytes("UTF-8"); byte[] bytesReencoded = s.getBytes(ByteString.UTF_8);
boolean bytesEqual = Arrays.equals(bytes, bytesReencoded); boolean bytesEqual = Arrays.equals(bytes, bytesReencoded);
if (bytesEqual != isRoundTrippable) { if (bytesEqual != isRoundTrippable) {
...@@ -313,10 +313,10 @@ class IsValidUtf8TestUtil { ...@@ -313,10 +313,10 @@ class IsValidUtf8TestUtil {
void testBytesUsingByteBuffers( void testBytesUsingByteBuffers(
int numBytes, long expectedCount, long start, long lim) int numBytes, long expectedCount, long start, long lim)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder() CharsetDecoder decoder = ByteString.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE); .onUnmappableCharacter(CodingErrorAction.REPLACE);
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder() CharsetEncoder encoder = ByteString.UTF_8.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE); .onUnmappableCharacter(CodingErrorAction.REPLACE);
byte[] bytes = new byte[numBytes]; byte[] bytes = new byte[numBytes];
......
...@@ -293,7 +293,7 @@ public class LiteralByteStringTest extends TestCase { ...@@ -293,7 +293,7 @@ public class LiteralByteStringTest extends TestCase {
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";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(UTF_8)); LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8));
String roundTripString = unicode.toString(UTF_8); String roundTripString = unicode.toString(UTF_8);
assertEquals(classUnderTest + " unicode must match", testString, roundTripString); assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
} }
...@@ -307,7 +307,7 @@ public class LiteralByteStringTest extends TestCase { ...@@ -307,7 +307,7 @@ public class LiteralByteStringTest extends TestCase {
public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{ public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{
assertSame(classUnderTest + " must be the same string references", assertSame(classUnderTest + " must be the same string references",
ByteString.EMPTY.toString(UTF_8), new LiteralByteString(new byte[]{}).toString(UTF_8)); ByteString.EMPTY.toString(ByteString.UTF_8), new LiteralByteString(new byte[]{}).toString(ByteString.UTF_8));
} }
public void testToString_raisesException() throws UnsupportedEncodingException{ public void testToString_raisesException() throws UnsupportedEncodingException{
......
...@@ -149,7 +149,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { ...@@ -149,7 +149,7 @@ public class RopeByteStringTest extends LiteralByteStringTest {
RopeByteString ropeByteString = RopeByteString ropeByteString =
RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY);
assertSame(classUnderTest + " must be the same string references", assertSame(classUnderTest + " must be the same string references",
ByteString.EMPTY.toString(UTF_8), ropeByteString.toString(UTF_8)); ByteString.EMPTY.toString(ByteString.UTF_8), ropeByteString.toString(ByteString.UTF_8));
} }
public void testToString_raisesException() throws UnsupportedEncodingException{ public void testToString_raisesException() throws UnsupportedEncodingException{
......
...@@ -276,11 +276,7 @@ public final class TestUtil { ...@@ -276,11 +276,7 @@ public final class TestUtil {
/** Helper to convert a String to ByteString. */ /** Helper to convert a String to ByteString. */
static ByteString toBytes(String str) { static ByteString toBytes(String str) {
try { return ByteString.copyFrom(str.getBytes(ByteString.UTF_8));
return ByteString.copyFrom(str.getBytes("UTF-8"));
} catch(java.io.UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported.", e);
}
} }
/** /**
......
...@@ -243,8 +243,8 @@ public class TextFormatTest extends TestCase { ...@@ -243,8 +243,8 @@ public class TextFormatTest extends TestCase {
* characters. The characters are converted directly to bytes, *not* * characters. The characters are converted directly to bytes, *not*
* encoded using UTF-8. * encoded using UTF-8.
*/ */
private ByteString bytes(String str) throws Exception { private ByteString bytes(String str) {
return ByteString.copyFrom(str.getBytes("ISO-8859-1")); return ByteString.copyFrom(str.getBytes(Internal.ISO_8859_1));
} }
/** /**
......
...@@ -229,7 +229,7 @@ public class UnknownFieldSetLiteTest extends TestCase { ...@@ -229,7 +229,7 @@ public class UnknownFieldSetLiteTest extends TestCase {
public void testMalformedBytes() throws Exception { public void testMalformedBytes() throws Exception {
try { try {
Foo.parseFrom("this is a malformed protocol buffer".getBytes("UTF-8")); Foo.parseFrom("this is a malformed protocol buffer".getBytes(ByteString.UTF_8));
fail(); fail();
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
// Expected. // Expected.
......
...@@ -190,12 +190,12 @@ public final class CodedInputByteBufferNano { ...@@ -190,12 +190,12 @@ public final class CodedInputByteBufferNano {
if (size <= (bufferSize - bufferPos) && size > 0) { if (size <= (bufferSize - bufferPos) && size > 0) {
// Fast path: We already have the bytes in a contiguous buffer, so // Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it. // just copy directly from it.
final String result = new String(buffer, bufferPos, size, "UTF-8"); final String result = new String(buffer, bufferPos, size, InternalNano.UTF_8);
bufferPos += size; bufferPos += size;
return result; return result;
} else { } else {
// Slow path: Build a byte array first then copy it. // Slow path: Build a byte array first then copy it.
return new String(readRawBytes(size), "UTF-8"); return new String(readRawBytes(size), InternalNano.UTF_8);
} }
} }
......
...@@ -291,7 +291,7 @@ public final class CodedOutputByteBufferNano { ...@@ -291,7 +291,7 @@ public final class CodedOutputByteBufferNano {
// 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("UTF-8"); final byte[] bytes = value.getBytes(InternalNano.UTF_8);
writeRawVarint32(bytes.length); writeRawVarint32(bytes.length);
writeRawBytes(bytes); writeRawBytes(bytes);
} }
...@@ -603,13 +603,9 @@ public final class CodedOutputByteBufferNano { ...@@ -603,13 +603,9 @@ public final class CodedOutputByteBufferNano {
* {@code string} field. * {@code string} field.
*/ */
public static int computeStringSizeNoTag(final String value) { public static int computeStringSizeNoTag(final String value) {
try { final byte[] bytes = value.getBytes(InternalNano.UTF_8);
final byte[] bytes = value.getBytes("UTF-8");
return computeRawVarint32Size(bytes.length) + return computeRawVarint32Size(bytes.length) +
bytes.length; bytes.length;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported.");
}
} }
/** /**
......
...@@ -34,9 +34,10 @@ import com.google.protobuf.nano.MapFactories.MapFactory; ...@@ -34,9 +34,10 @@ import com.google.protobuf.nano.MapFactories.MapFactory;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Map;
/** /**
* The classes contained within are used internally by the Protocol Buffer * The classes contained within are used internally by the Protocol Buffer
...@@ -67,6 +68,8 @@ public final class InternalNano { ...@@ -67,6 +68,8 @@ public final class InternalNano {
public static final int TYPE_SINT32 = 17; public static final int TYPE_SINT32 = 17;
public static final int TYPE_SINT64 = 18; public static final int TYPE_SINT64 = 18;
protected static final Charset UTF_8 = Charset.forName("UTF-8");
protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
private InternalNano() {} private InternalNano() {}
...@@ -111,14 +114,7 @@ public final class InternalNano { ...@@ -111,14 +114,7 @@ public final class InternalNano {
* generated code calls this automatically. * generated code calls this automatically.
*/ */
public static String stringDefaultValue(String bytes) { public static String stringDefaultValue(String bytes) {
try { return new String(bytes.getBytes(ISO_8859_1), InternalNano.UTF_8);
return new String(bytes.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
// This should never happen since all JVMs are required to implement
// both of the above character sets.
throw new IllegalStateException(
"Java VM does not support a standard character set.", e);
}
} }
/** /**
...@@ -130,14 +126,7 @@ public final class InternalNano { ...@@ -130,14 +126,7 @@ public final class InternalNano {
* embed raw bytes as a string literal with ISO-8859-1 encoding. * embed raw bytes as a string literal with ISO-8859-1 encoding.
*/ */
public static byte[] bytesDefaultValue(String bytes) { public static byte[] bytesDefaultValue(String bytes) {
try { return bytes.getBytes(ISO_8859_1);
return bytes.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// This should never happen since all JVMs are required to implement
// ISO-8859-1.
throw new IllegalStateException(
"Java VM does not support a standard character set.", e);
}
} }
/** /**
...@@ -145,11 +134,7 @@ public final class InternalNano { ...@@ -145,11 +134,7 @@ public final class InternalNano {
* UnsupportedEncodingException to a RuntimeException. * UnsupportedEncodingException to a RuntimeException.
*/ */
public static byte[] copyFromUtf8(final String text) { public static byte[] copyFromUtf8(final String text) {
try { return text.getBytes(InternalNano.UTF_8);
return text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?");
}
} }
/** /**
......
...@@ -458,7 +458,7 @@ public class NanoTest extends TestCase { ...@@ -458,7 +458,7 @@ public class NanoTest extends TestCase {
assertFalse(msg.optionalBytes.length > 0); assertFalse(msg.optionalBytes.length > 0);
msg.optionalBytes = InternalNano.copyFromUtf8("hello"); msg.optionalBytes = InternalNano.copyFromUtf8("hello");
assertTrue(msg.optionalBytes.length > 0); assertTrue(msg.optionalBytes.length > 0);
assertEquals("hello", new String(msg.optionalBytes, "UTF-8")); assertEquals("hello", new String(msg.optionalBytes, InternalNano.UTF_8));
msg.clear(); msg.clear();
assertFalse(msg.optionalBytes.length > 0); assertFalse(msg.optionalBytes.length > 0);
msg.clear() msg.clear()
...@@ -476,7 +476,7 @@ public class NanoTest extends TestCase { ...@@ -476,7 +476,7 @@ public class NanoTest extends TestCase {
TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
assertTrue(newMsg.optionalBytes.length > 0); assertTrue(newMsg.optionalBytes.length > 0);
assertEquals("bye", new String(newMsg.optionalBytes, "UTF-8")); assertEquals("bye", new String(newMsg.optionalBytes, InternalNano.UTF_8));
} }
public void testNanoOptionalGroup() throws Exception { public void testNanoOptionalGroup() throws Exception {
...@@ -1346,14 +1346,14 @@ public class NanoTest extends TestCase { ...@@ -1346,14 +1346,14 @@ public class NanoTest extends TestCase {
InternalNano.copyFromUtf8("bye"), InternalNano.copyFromUtf8("bye"),
InternalNano.copyFromUtf8("boo") InternalNano.copyFromUtf8("boo")
}; };
assertEquals("bye", new String(msg.repeatedBytes[1], "UTF-8")); assertEquals("bye", new String(msg.repeatedBytes[1], InternalNano.UTF_8));
assertEquals("boo", new String(msg.repeatedBytes[2], "UTF-8")); assertEquals("boo", new String(msg.repeatedBytes[2], InternalNano.UTF_8));
msg.clear(); msg.clear();
assertEquals(0, msg.repeatedBytes.length); assertEquals(0, msg.repeatedBytes.length);
msg.clear() msg.clear()
.repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") }; .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") };
assertEquals(1, msg.repeatedBytes.length); assertEquals(1, msg.repeatedBytes.length);
assertEquals("boo", new String(msg.repeatedBytes[0], "UTF-8")); assertEquals("boo", new String(msg.repeatedBytes[0], InternalNano.UTF_8));
msg.clear(); msg.clear();
assertEquals(0, msg.repeatedBytes.length); assertEquals(0, msg.repeatedBytes.length);
...@@ -1385,8 +1385,8 @@ public class NanoTest extends TestCase { ...@@ -1385,8 +1385,8 @@ public class NanoTest extends TestCase {
newMsg = TestAllTypesNano.parseFrom(result); newMsg = TestAllTypesNano.parseFrom(result);
assertEquals(2, newMsg.repeatedBytes.length); assertEquals(2, newMsg.repeatedBytes.length);
assertEquals("hello", new String(newMsg.repeatedBytes[0], "UTF-8")); assertEquals("hello", new String(newMsg.repeatedBytes[0], InternalNano.UTF_8));
assertEquals("world", new String(newMsg.repeatedBytes[1], "UTF-8")); assertEquals("world", new String(newMsg.repeatedBytes[1], InternalNano.UTF_8));
} }
public void testNanoRepeatedGroup() throws Exception { public void testNanoRepeatedGroup() throws Exception {
...@@ -2277,9 +2277,9 @@ public class NanoTest extends TestCase { ...@@ -2277,9 +2277,9 @@ public class NanoTest extends TestCase {
assertTrue(52.0e3 == msg.defaultDouble); assertTrue(52.0e3 == msg.defaultDouble);
assertEquals(true, msg.defaultBool); assertEquals(true, msg.defaultBool);
assertEquals("hello", msg.defaultString); assertEquals("hello", msg.defaultString);
assertEquals("world", new String(msg.defaultBytes, "UTF-8")); assertEquals("world", new String(msg.defaultBytes, InternalNano.UTF_8));
assertEquals("dünya", msg.defaultStringNonascii); assertEquals("dünya", msg.defaultStringNonascii);
assertEquals("dünyab", new String(msg.defaultBytesNonascii, "UTF-8")); assertEquals("dünyab", new String(msg.defaultBytesNonascii, InternalNano.UTF_8));
assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum); assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum);
assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum); assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum);
assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum); assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum);
...@@ -2385,7 +2385,7 @@ public class NanoTest extends TestCase { ...@@ -2385,7 +2385,7 @@ public class NanoTest extends TestCase {
assertEquals(TestAllTypesNanoHas.FOO, newMsg.optionalNestedEnum); assertEquals(TestAllTypesNanoHas.FOO, newMsg.optionalNestedEnum);
assertEquals(41, newMsg.defaultInt32); assertEquals(41, newMsg.defaultInt32);
assertEquals("hello", newMsg.defaultString); assertEquals("hello", newMsg.defaultString);
assertEquals("world", new String(newMsg.defaultBytes, "UTF-8")); assertEquals("world", new String(newMsg.defaultBytes, InternalNano.UTF_8));
assertEquals(TestAllTypesNanoHas.BAR, newMsg.defaultNestedEnum); assertEquals(TestAllTypesNanoHas.BAR, newMsg.defaultNestedEnum);
assertEquals(Float.NaN, newMsg.defaultFloatNan); assertEquals(Float.NaN, newMsg.defaultFloatNan);
assertEquals(0, newMsg.id); assertEquals(0, newMsg.id);
...@@ -2567,7 +2567,7 @@ public class NanoTest extends TestCase { ...@@ -2567,7 +2567,7 @@ public class NanoTest extends TestCase {
assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum()); assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum());
assertEquals(41, newMsg.getDefaultInt32()); assertEquals(41, newMsg.getDefaultInt32());
assertEquals("hello", newMsg.getDefaultString()); assertEquals("hello", newMsg.getDefaultString());
assertEquals("world", new String(newMsg.getDefaultBytes(), "UTF-8")); assertEquals("world", new String(newMsg.getDefaultBytes(), InternalNano.UTF_8));
assertEquals(TestNanoAccessors.BAR, newMsg.getDefaultNestedEnum()); assertEquals(TestNanoAccessors.BAR, newMsg.getDefaultNestedEnum());
assertEquals(Float.NaN, newMsg.getDefaultFloatNan()); assertEquals(Float.NaN, newMsg.getDefaultFloatNan());
assertEquals(0, newMsg.id); assertEquals(0, newMsg.id);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment