aboutsummaryrefslogtreecommitdiffhomepage
path: root/javanano
diff options
context:
space:
mode:
authorGravatar Tamir Duberstein <tamird@gmail.com>2015-03-31 20:01:43 -0700
committerGravatar Tamir Duberstein <tamird@gmail.com>2015-04-02 15:06:00 -0700
commit210de285d7ca5e4d4e96867c1c04308d68c5d4dd (patch)
tree983ab06e7e3a37e38a5d6c1768c15f50246df5d3 /javanano
parente84893f6768f136cc86e2db69fc1d40ff2be7e3b (diff)
DRY: Use `Charset` statics to eliminate exceptions
Diffstat (limited to 'javanano')
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java4
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java12
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/InternalNano.java29
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/NanoTest.java22
4 files changed, 24 insertions, 43 deletions
diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
index b4f20fde..4b45c6d2 100644
--- a/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
+++ b/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
@@ -190,12 +190,12 @@ public final class CodedInputByteBufferNano {
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, "UTF-8");
+ final String result = new String(buffer, bufferPos, size, InternalNano.UTF_8);
bufferPos += size;
return result;
} else {
// 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);
}
}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
index 2777f34c..1f0534b1 100644
--- a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
+++ b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
@@ -291,7 +291,7 @@ public final class CodedOutputByteBufferNano {
// 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("UTF-8");
+ final byte[] bytes = value.getBytes(InternalNano.UTF_8);
writeRawVarint32(bytes.length);
writeRawBytes(bytes);
}
@@ -603,13 +603,9 @@ public final class CodedOutputByteBufferNano {
* {@code string} field.
*/
public static int computeStringSizeNoTag(final String value) {
- try {
- final byte[] bytes = value.getBytes("UTF-8");
- return computeRawVarint32Size(bytes.length) +
- bytes.length;
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("UTF-8 not supported.");
- }
+ final byte[] bytes = value.getBytes(InternalNano.UTF_8);
+ return computeRawVarint32Size(bytes.length) +
+ bytes.length;
}
/**
diff --git a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java
index 4a08bb58..67404d27 100644
--- a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java
+++ b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java
@@ -34,9 +34,10 @@ import com.google.protobuf.nano.MapFactories.MapFactory;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.util.Arrays;
-import java.util.Map;
import java.util.Map.Entry;
+import java.util.Map;
/**
* The classes contained within are used internally by the Protocol Buffer
@@ -67,6 +68,8 @@ public final class InternalNano {
public static final int TYPE_SINT32 = 17;
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() {}
@@ -111,14 +114,7 @@ public final class InternalNano {
* generated code calls this automatically.
*/
public static String stringDefaultValue(String bytes) {
- try {
- 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);
- }
+ return new String(bytes.getBytes(ISO_8859_1), InternalNano.UTF_8);
}
/**
@@ -130,14 +126,7 @@ public final class InternalNano {
* embed raw bytes as a string literal with ISO-8859-1 encoding.
*/
public static byte[] bytesDefaultValue(String bytes) {
- try {
- 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);
- }
+ return bytes.getBytes(ISO_8859_1);
}
/**
@@ -145,11 +134,7 @@ public final class InternalNano {
* UnsupportedEncodingException to a RuntimeException.
*/
public static byte[] copyFromUtf8(final String text) {
- try {
- return text.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("UTF-8 not supported?");
- }
+ return text.getBytes(InternalNano.UTF_8);
}
/**
diff --git a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
index 4cc77226..7de84310 100644
--- a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
+++ b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
@@ -458,7 +458,7 @@ public class NanoTest extends TestCase {
assertFalse(msg.optionalBytes.length > 0);
msg.optionalBytes = InternalNano.copyFromUtf8("hello");
assertTrue(msg.optionalBytes.length > 0);
- assertEquals("hello", new String(msg.optionalBytes, "UTF-8"));
+ assertEquals("hello", new String(msg.optionalBytes, InternalNano.UTF_8));
msg.clear();
assertFalse(msg.optionalBytes.length > 0);
msg.clear()
@@ -476,7 +476,7 @@ public class NanoTest extends TestCase {
TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
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 {
@@ -1346,14 +1346,14 @@ public class NanoTest extends TestCase {
InternalNano.copyFromUtf8("bye"),
InternalNano.copyFromUtf8("boo")
};
- assertEquals("bye", new String(msg.repeatedBytes[1], "UTF-8"));
- assertEquals("boo", new String(msg.repeatedBytes[2], "UTF-8"));
+ assertEquals("bye", new String(msg.repeatedBytes[1], InternalNano.UTF_8));
+ assertEquals("boo", new String(msg.repeatedBytes[2], InternalNano.UTF_8));
msg.clear();
assertEquals(0, msg.repeatedBytes.length);
msg.clear()
.repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") };
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();
assertEquals(0, msg.repeatedBytes.length);
@@ -1385,8 +1385,8 @@ public class NanoTest extends TestCase {
newMsg = TestAllTypesNano.parseFrom(result);
assertEquals(2, newMsg.repeatedBytes.length);
- assertEquals("hello", new String(newMsg.repeatedBytes[0], "UTF-8"));
- assertEquals("world", new String(newMsg.repeatedBytes[1], "UTF-8"));
+ assertEquals("hello", new String(newMsg.repeatedBytes[0], InternalNano.UTF_8));
+ assertEquals("world", new String(newMsg.repeatedBytes[1], InternalNano.UTF_8));
}
public void testNanoRepeatedGroup() throws Exception {
@@ -2277,9 +2277,9 @@ public class NanoTest extends TestCase {
assertTrue(52.0e3 == msg.defaultDouble);
assertEquals(true, msg.defaultBool);
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ünyab", new String(msg.defaultBytesNonascii, "UTF-8"));
+ assertEquals("dünyab", new String(msg.defaultBytesNonascii, InternalNano.UTF_8));
assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum);
assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum);
assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum);
@@ -2385,7 +2385,7 @@ public class NanoTest extends TestCase {
assertEquals(TestAllTypesNanoHas.FOO, newMsg.optionalNestedEnum);
assertEquals(41, newMsg.defaultInt32);
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(Float.NaN, newMsg.defaultFloatNan);
assertEquals(0, newMsg.id);
@@ -2567,7 +2567,7 @@ public class NanoTest extends TestCase {
assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum());
assertEquals(41, newMsg.getDefaultInt32());
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(Float.NaN, newMsg.getDefaultFloatNan());
assertEquals(0, newMsg.id);